Gson 解析LocalDateTime

    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())

推荐阅读更多精彩内容