SpringBoot自定义json解析器
来源:互联网 发布:沙宝亮的唱功 知乎 编辑:程序博客网 时间:2024/05/02 00:57
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这种方法根本不行...
阅读全文
0 0
- SpringBoot自定义json解析器
- 自定义json解析
- SpringBoot使用FastJson解析Json数据
- SpringBoot 自定义拦截器
- springboot视图解析器
- JSON 串 自定义解析字段
- 解析自定义的json数据
- C# 自定义Json解析工具
- springboot自定义拦截器Interceptor
- SpringBoot 之 第三方Json解析框架FastJson的使用
- springboot使用第三方的json解析框架fastjson
- Springboot学习笔记(一)FastJson解析JSON数据
- 微信自定义菜单json字符串解析
- mysql5.7以下自定义函数解析json
- JSON的自定义序列化和解析
- Json数据 解析的自定义方法
- 难呐~JSON解析器
- Symbian JSON解析器
- ubuntu14下 安装msyql
- 20170904
- Windows远程桌面连接不上问题
- HTML基础部分
- 解决中文乱码
- SpringBoot自定义json解析器
- ->的使用及堆,栈的概念
- 在IIS服务器上未能加载文件或程序集“System.Web.Http, Version=4.0.0.0, Culture=neutral
- Java集合及concurrent并发包总结(转)
- ST依托MCU产品线优势,将扩大LPWAN市场
- sdut 暑假结训赛 巨斧砍大树
- 初识Hibernate
- #10048导弹防御系统
- C# HMACMD5加密