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

info.baseinsight.core.json.EntitySimpleJsonSerializer Maven / Gradle / Ivy

The newest version!
package info.baseinsight.core.json;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import info.baseinsight.core.SpringUtils;
import info.baseinsight.core.annotation.EntitySimpleJsonFormat;
import jakarta.persistence.Entity;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by @Author:xiaopeng
 *自定义的faster JSON输出类 ,读取entity类的注解EntitySimpleJsonFormat的设置
 *
 */
public class EntitySimpleJsonSerializer extends JsonSerializer {

    @Override
    public void serialize(Object obj, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException {
        Class aclass=jsonGenerator.getCurrentValue().getClass();
        EntitySimpleJsonFormat annotation= null;
        try {
            annotation = aclass.getDeclaredField(jsonGenerator.getOutputContext().getCurrentName()).getAnnotation(EntitySimpleJsonFormat.class);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        if(annotation!=null){
            //自定义设置domainjsonformat
            List jsonFieldList=new ArrayList();
            Class objClass=obj.getClass();
            Field[] allFields = objClass.getDeclaredFields();
            List needFields=Arrays.stream(annotation.value()).toList();
            List ignoreFields=Arrays.stream(annotation.ignores()).toList();
            Arrays.stream(allFields).toList().forEach(field->{
                if(needFields.contains(field.getName())){
                    jsonFieldList.add(field);
                }
            });
            Arrays.stream(allFields).toList().forEach(field->{
                if(ignoreFields.contains(field.getName())){
                    jsonFieldList.remove(field);
                }
            });
            if(jsonFieldList.size()>0){
                for(Field field : jsonFieldList){
                    jsonGenerator.writeStartObject();
                    String methodName="get"+ StringUtils.uncapitalize(field.getName());
                    Object fieldValue= SpringUtils.doMethod(obj,methodName,new ArrayList>());
                    Class fieldClass=field.getClass();
                    Entity fieldAnnotation=(Entity)fieldClass.getAnnotation(Entity.class);
                    //@根据类型判断日期
                    if(fieldValue instanceof Date || fieldValue instanceof java.sql.Date || fieldValue instanceof Time || fieldValue instanceof Timestamp) {
                        JsonFormat formatAnnotation = field.getAnnotation(JsonFormat.class);
                        String format = "yyyy-MM-dd";
                        if (fieldValue instanceof Time || fieldValue instanceof Timestamp) {
                            format = "yyyy-MM-dd hh:mm sss";
                        }
                        if (formatAnnotation!=null) {
                            format = formatAnnotation.pattern();
                        }
                        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
                        jsonGenerator.writeStringField(field.getName(),simpleDateFormat.format(fieldValue));
                    }else if(fieldAnnotation!=null){
                        jsonGenerator.writeFieldName(field.getName());
                        jsonGenerator.setCurrentValue(obj);
                        this.serialize(fieldValue,jsonGenerator,serializers);
                    }else{
                        jsonGenerator.writeStringField(field.getName(),fieldValue.toString());
                    }
                    jsonGenerator.writeEndObject();
                }
            }
        }else{
            jsonGenerator.writeStartObject();
            String id = (String) SpringUtils.doMethod(obj,"getId",new ArrayList>());
            jsonGenerator.writeStringField("id", id);
            jsonGenerator.writeStringField("label",obj.toString());
            jsonGenerator.writeStringField("class",obj.getClass().getName());
            jsonGenerator.writeEndObject();
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy