Newer
Older
newfiber-data-adapter / src / main / java / org / springnewfiber / dataadapter / sswj / util / RealTimeSerializer.java
package org.springnewfiber.dataadapter.sswj.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Filter;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.google.common.collect.Maps;
import org.springnewfiber.dataadapter.entity.MqNodeData;
import org.springnewfiber.dataadapter.entity.PtReceiveBaseModel;
import org.springnewfiber.dataadapter.sswj.entity.BzLive;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Map;

/**
 * @program: newfiber-data-adapter
 * @description:
 * @author: djt
 * @create: 2022-04-21 15:48
 **/
public class RealTimeSerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();

        Class clazz = value.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            for (Annotation annotation : field.getAnnotations()) {
                Class annotationType = annotation.annotationType();

                System.out.println("属性【" + field + "】的注释类型有: " + annotationType);
            }
        }
//            ReflectUtil.get
        gen.writeEndObject();
    }

    public static void main(String[] args) {
        BzLive bzLive = new BzLive();
        bzLive.setDataUpStatus("1");
        bzLive.setDataUpUuid("111111");
        System.out.println();
        System.out.println(JSONObject.toJSONString(bzLive));
    }

    public static PtReceiveBaseModel objectToRealMap(Object object) {
        if(null==object){
            return null;
        }
        Map<String, Object> map = BeanUtil.beanToMap(JSONObject.parse(JSONObject.toJSONString(object)));
        Map<String, MqNodeData> dataMap = Maps.newHashMap();
        PtReceiveBaseModel model = new PtReceiveBaseModel();
        CollUtil.filter(map, new Filter<Map.Entry<String, Object>>() {
            @Override
            public boolean accept(Map.Entry<String, Object> stringObjectEntry) {
                if (PtReceiveBaseModel.constantSet.contains(stringObjectEntry.getKey())) {
                    return false;
                }
                return true;
            }
        }).forEach((key, value) -> {
            MqNodeData data = new MqNodeData();
            data.setSn(key);
            data.setKey(key);
            data.setValue(value);
            dataMap.put(key, data);
        });
        model.setSt(map.get(PtReceiveBaseModel.RealTimeConsstant.st).toString());
        model.setTt(DateUtil.parse(map.get(PtReceiveBaseModel.RealTimeConsstant.tt).toString(), DatePattern.NORM_DATETIME_PATTERN));
        model.setUt(DateUtil.parse(map.get(PtReceiveBaseModel.RealTimeConsstant.ut)==null?DateUtil.format(new Date(),DatePattern.NORM_DATETIME_PATTERN):map.get(PtReceiveBaseModel.RealTimeConsstant.ut).toString(), DatePattern.NORM_DATETIME_PATTERN));
        model.setDataMap(dataMap);
        return model;
    }
}