【发布时间】:2021-10-13 11:08:41
【问题描述】:
有一个带有 LocalDateTime 属性的 java 对象,我想用 gson 对其进行序列化和反序列化。 我在 json 输出中使用 ISO_INSTANT 作为日期时间的格式
已尝试在这篇文章的帮助下为 GSON 创建一个 TypeAdaptor
但得到“不支持的字段:InstantSeconds”
也许我搞砸了日期转换?
class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> {
@Override
public JsonElement serialize(LocalDateTime date, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(
DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.systemDefault()).withLocale(Locale.getDefault()).format(date)); // "yyyy-mm-ddThhMMssZ"
}
}
【问题讨论】:
-
请包含完整的堆栈跟踪。