例えばJSON→ZonedDateTimeを持つPOJOへの変換の確認など。
デシリアライザにはGsonを使う想定です。
build.gradle
android {
// (省略)
testVariants.all { variant ->
variant.getCompileConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
variant.getRuntimeConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
}
}
dependencies {
// (省略)
implementation "com.jakewharton.threetenabp:threetenabp:$threetenabp_version"
androidTestImplementation("org.threeten:threetenbp:$threetenbp_version") {
exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
}
}
String→ZonedDateTimeのJsonDeserializerの例
class IsoZonedDateTimeDeserializer : JsonDeserializer<ZonedDateTime> {
@Throws(JsonParseException::class)
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): ZonedDateTime {
val jsonPrimitive = json?.asJsonPrimitive
try {
if (jsonPrimitive is JsonPrimitive && jsonPrimitive.isString) {
return ZonedDateTime.parse(jsonPrimitive.asString, DateTimeFormatter.ISO_ZONED_DATE_TIME)
}
} catch (e: RuntimeException) {
throw JsonParseException("Unable to parse ZonedDateTime", e)
}
throw JsonParseException("Unable to parse ZonedDateTime")
}
}