美文网首页
Gson 解析LocalDateTime

Gson 解析LocalDateTime

作者: 菜鸟考官 | 来源:发表于2021-04-12 16:33 被阅读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())

相关文章

网友评论

      本文标题:Gson 解析LocalDateTime

      本文链接:https://www.haomeiwen.com/subject/fapxlltx.html