GSON 特殊类型支持序列化和反序列化,如LocalDateTime
2024-09-07 05:12:38
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));
最新文章
- 【JSP错误总结】无法访问jsp页面
- GridControl读取xml和保存xml
- SqlBulkCopy批量添加数据
- GCD 大中枢派发 简单应用实例
- div模仿select效果二:带搜索框
- [置顶] 2013 Multi-University Training Contest 8
- 我的博客已搬迁到http://www.lsworks.net
- (七)Hibernate 映射继承
- POJ 1013 Counterfeit Dollar 集合上的位运算
- AOP实现方法
- WCF技术剖析之四:基于IIS的WCF服务寄宿(Hosting)实现揭秘
- 2017-3-5 C#基础 函数
- Python 3 读写文件的简单方法!
- RChain节点通信机制(上)
- jmeter 新手使用教程
- 【转】windows 控制台cmd乱码的解决办法
- 目标检测——IoU 计算
- 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
- spring启动后立即执行方法
- 后端调用接口在通过webService发布 解决跨域问题
热门文章
- wibu软授权(五)
- org.elasticsearch.ElasticsearchException: not all primary shards of [.geoip_databases] index are active解决办法
- DRF全局总结
- MySQL-存储引擎-索引
- charles证书安装-客户端证书
- python 非阻塞线程对话框,非qt(解决qt MessageBox使用线程时候卡死问题)
- iOS开发之各机型屏幕大小与键盘高度整理
- mysql正则替换 正宗!
- LAN8720 调试笔记
- WPF检测设备变化