SpringBoot自定义json解析器

import com.google.gson.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @auth Qbit
 * @date 17-5-25.
 */
@Configuration
@ConditionalOnClass({Gson.class})
public class GsonConfiguration {
    private String format(Date date){
        if(null==date)
            return StringUtils.EMPTY;
        String out=new SimpleDateFormat(Constant.DATE_PATTERN).format(date);
        if("2517-04-24 00:00:00".equals(out))
            return StringUtils.EMPTY;
        return out;
    }
    private final GsonBuilder GSON_BUILDER;
    public GsonConfiguration() {
        GSON_BUILDER=new GsonBuilder();
        class DateTypeAdapter implements JsonSerializer<Date>,
                JsonDeserializer<Date> {
            @Override
            public Date deserialize(JsonElement json, Type t,
                                    JsonDeserializationContext jsc) throws JsonParseException {
                if (!(json instanceof JsonPrimitive))
                    throw new JsonParseException(
                            "The data should be a string value");
                try {
                    String string = json.getAsString();
                    return new SimpleDateFormat(Constant.DATE_PATTERN).parse(string);
                } catch (Exception e) {
                    throw new JsonParseException(e);
                }
            }

            @Override
            public JsonElement serialize(Date date, Type arg1,
                                         JsonSerializationContext arg2) {
                return new JsonPrimitive(format(date));
            }

        }
        DateTypeAdapter dta = new DateTypeAdapter();
        GSON_BUILDER.registerTypeAdapter(Date.class, dta);
        class OperationAdapter implements JsonSerializer<Where.Operation>,JsonDeserializer<Where.Operation>{

            @Override
            public Where.Operation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                String string=json.getAsString();
                for(Where.Operation operation: Where.Operation.values()){
                    if(operation.getCode().equalsIgnoreCase(string)||operation.toString().equalsIgnoreCase(string))
                        return operation;
                }
                throw new JsonParseException("can not parse '"+string+"' to Where.Operation");
            }

            @Override
            public JsonElement serialize(Where.Operation src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(src.getCode());
            }
        }
        OperationAdapter oa=new OperationAdapter();
        GSON_BUILDER.registerTypeAdapter(Where.Operation.class,oa);
    }
    @Bean
    public Gson gson(){
        return GSON_BUILDER.create();
    }
}
需要注意要把该类放到spring扫描的路径里.提供TypeAdapter这种方法根本不行...

猜你喜欢

转载自blog.csdn.net/u012220365/article/details/77839507