GSON 特殊类型支持序列化和反序列化,如LocalDateTime

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Gson gson = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() {
@Override
public LocalDateTime deserialize(JsonElement json, Type type,
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
String asString = json.getAsJsonPrimitive().getAsString();
LocalDateTime parse = LocalDateTime.parse(asString, dateTimeFormatter);
return parse;
}
}).registerTypeAdapter(LocalDateTime.class, new JsonSerializer<LocalDateTime>() {
@Override
public JsonElement serialize(LocalDateTime localDateTime, Type srcType,
JsonSerializationContext context) {
return new JsonPrimitive(dateTimeFormatter.format(localDateTime));
}
}).create(); Result<DbTradeInfo> result = (Result<DbTradeInfo>) gson.fromJson(
"{\"createTime\":\"2022-04-19 00:00:32\"}"
,
new TypeToken<Result<DbTradeInfo>>() {
}.getType()); System.out.println(result.getData().getCreateTime().getClass()); System.out.println(gson.toJson(result));

  

最新文章

  1. 【JSP错误总结】无法访问jsp页面
  2. GridControl读取xml和保存xml
  3. SqlBulkCopy批量添加数据
  4. GCD 大中枢派发 简单应用实例
  5. div模仿select效果二:带搜索框
  6. [置顶] 2013 Multi-University Training Contest 8
  7. 我的博客已搬迁到http://www.lsworks.net
  8. (七)Hibernate 映射继承
  9. POJ 1013 Counterfeit Dollar 集合上的位运算
  10. AOP实现方法
  11. WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘
  12. 2017-3-5 C#基础 函数
  13. Python 3 读写文件的简单方法!
  14. RChain节点通信机制(上)
  15. jmeter 新手使用教程
  16. 【转】windows 控制台cmd乱码的解决办法
  17. 目标检测——IoU 计算
  18. Type Operators instanceof is used to determine whether a PHP variable is an instantiated object of a certain class/a class that implements an interface
  19. spring启动后立即执行方法
  20. 后端调用接口在通过webService发布 解决跨域问题

热门文章

  1. wibu软授权(五)
  2. org.elasticsearch.ElasticsearchException: not all primary shards of [.geoip_databases] index are active解决办法
  3. DRF全局总结
  4. MySQL-存储引擎-索引
  5. charles证书安装-客户端证书
  6. python 非阻塞线程对话框,非qt(解决qt MessageBox使用线程时候卡死问题)
  7. iOS开发之各机型屏幕大小与键盘高度整理
  8. mysql正则替换 正宗!
  9. LAN8720 调试笔记
  10. WPF检测设备变化