Gson 解析LocalDateTime
2021-04-12 本文已影响0人
菜鸟考官
val gsonBuilder = GsonFactory.getSingletonGson().newBuilder()
gsonBuilder.registerTypeAdapter(
LocalDateTime::class.java,
JsonDeserializer { json: JsonElement, type: Type?, context: JsonDeserializationContext? ->
val instant = json.asJsonPrimitive.asLong
LocalDateTime(instant)
} as JsonDeserializer<LocalDateTime>
).registerTypeAdapter(
LocalDateTime::class.java,
JsonSerializer { time: LocalDateTime, type: Type?, context: JsonSerializationContext? ->
JsonPrimitive(time.toDateTime(DateTimeZone.UTC).millis)
} as JsonSerializer<LocalDateTime?>).create()
GsonUtils.setGsonDelegate(gsonBuilder.create())