java - Gson 中是否有类似 JsonFormat 的内容?
问题描述
我的问题真的很小:
我更喜欢 Gson,而不是 fastrxml.jackson。我想在 Gson 中看到的一个可能的功能是:
//some code
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
private Date endDate;
//some code
我发现在 Gson 中做同样事情的唯一方法是:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
我认为注释比初始化更容易理解。有什么方法可以注释或制作一些属性,以便代码
gson.fromJson("\"{\\\"Id\\\": 703,\\\"StartDate\\\": \\\"2019-10-01T00:00:00\\\"," +
" \\\"EndDate\\\": \\\"2019-10-25T00:00:00\\\",\\\"Title\\\": \\\"exmample title\\\"}\"",
MyObj.class)
将产生 MyObj 类的对象:
public class MyObj{
@SerializedName("Id")
private Long id;
@SerializedName("StartDate")
//analogue of JsonFormat????
private Date startDate;
@SerializedName("EndDate")
//analogue of JsonFormat????
private Date endDate;
@SerializedName("Title")
private String title;
}
解决方案
反序列化为使用标记为的JSON
类。要处理额外的注释,您必须实现类似的东西并创建新的注释并在反序列化逻辑中处理它。现在,您可以实现允许解析给定日期的自定义。POJO
Gson
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory
final
com.google.gson.JsonDeserializer
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class GsonApp {
public static void main(String[] args) {
Gson gson = new GsonBuilder().create();
MyObj myObj = gson.fromJson(
"{\"Id\": 703,\"StartDate\": \"2019-10-01T00:00:00\",\"EndDate\": \"2019-10-25T00:00:00\",\"Title\": \"exmample title\"}",
MyObj.class);
System.out.println(myObj);
}
}
class IsoDateTimeJsonDeserializer implements JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
LocalDateTime localDateTime = LocalDateTime.parse(json.getAsString(), DateTimeFormatter.ISO_DATE_TIME);
return Date.from(localDateTime.atZone(ZoneOffset.systemDefault()).toInstant());
}
}
class MyObj {
@SerializedName("Id")
private Long id;
@SerializedName("StartDate")
@JsonAdapter(IsoDateTimeJsonDeserializer.class)
private Date startDate;
@SerializedName("EndDate")
@JsonAdapter(IsoDateTimeJsonDeserializer.class)
private Date endDate;
@SerializedName("Title")
private String title;
// getters, setters, toString
}
上面的代码打印:
MyObj{id=703, startDate=Tue Oct 01 00:00:00 CEST 2019, endDate=Fri Oct 25 00:00:00 CEST 2019, title='exmample title'}
推荐阅读
- java - 是否可以从 .xhtml 重定向到 .html,反之亦然?
- macos - 如何修复工具实例无法启动,将进行另一次尝试 - Anypoint Studio 7.3 - MacOsX
- wordpress - 带有 Timber (Twig) 和 Wordpress 的数组中的随机值
- restore - 从备份中恢复重复配置
- typescript - 胖箭头函数和嵌套的装饰器?
- java - 邮件即使很小也会被剪裁,元音中的重音问题(a、e、i、o、u 到 á、é、í、ó、ú)
- python - 将excel读入pandas数据框时如何区分excel中两行不同颜色的颜色?
- javascript - 如何管理多个导航项状态?
- java - 无法在 Visual Studio Code 中键入“点”
- node.js - knex 的事务回滚问题