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())
上一篇 下一篇

猜你喜欢

热点阅读