All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.nacos.naming.cluster.transport.FastJsonSerializer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.nacos.naming.cluster.transport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.pojo.Record;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * Use FastJSON to serialize data
 *
 * @author nkorange
 * @since 1.0.0
 */
@Component
public class FastJsonSerializer implements Serializer {

    @Override
    public  byte[] serialize(T data) {
        return JSON.toJSONBytes(data);
    }

    @Override
    public  T deserialize(byte[] data, Class clazz) {
        return JSON.parseObject(new String(data, StandardCharsets.UTF_8), clazz);
    }

    @Override
    public  T deserialize(byte[] data, TypeReference clazz) {
        try {
            String dataString = new String(data, StandardCharsets.UTF_8);
            return JSON.parseObject(dataString, clazz);
        } catch (Exception e) {
            Loggers.SRV_LOG.error("deserialize data failed.", e);
        }
        return null;
    }

    @Override
    public  Map> deserializeMap(byte[] data, Class clazz) {
        try {
            String dataString = new String(data, StandardCharsets.UTF_8);
            Map dataMap = JSON.parseObject(dataString, new TypeReference>() {
            });

            Map> resultMap = new HashMap<>(dataMap.size());
            for (Map.Entry entry : dataMap.entrySet()) {

                Datum datum = new Datum<>();
                datum.timestamp.set(entry.getValue().getLongValue("timestamp"));
                datum.key = entry.getValue().getString("key");
                datum.value = JSON.parseObject(entry.getValue().getJSONObject("value").toJSONString(), clazz);
                resultMap.put(entry.getKey(), datum);
            }

            return resultMap;
        } catch (Exception e) {
            Loggers.SRV_LOG.error("deserialize data failed.", e);
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy