Hi,
I'm using Moshi with Rfc3339DateJsonAdapter. It would be great if the Iso8601Utils.parse() method was less strict about a possible missing timezone indicator like in "2018-05-18T12:16:00".
It currently throws an Exception which forbids any further JSON parsing, and this is quite annoying.
What do you think about it?
maybe not what you are looking for but i have an API that sends back 4 different date formats, and here's how i'm handling it.
also using the java 8 java.time backport for android which can be found here
https://github.com/JakeWharton/ThreeTenABP
class InstantAdapter {
@FromJson
fun fromJson(jsonReader: JsonReader): Instant? {
if(jsonReader.peek() == JsonReader.Token.NULL) return jsonReader.nextNull()
val dateString = jsonReader.nextString()
if (dateString.isEmpty()) return null
val formatter = DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ofPattern("MM-dd-yyyy"))
.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yyyy"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
.appendOptional(DateTimeFormatter.ofPattern("EEE, dd MMM yyyy H:mm:ss z"))
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter()
.withZone(ZoneId.of("UTC"))
return try {
ZonedDateTime.parse(dateString, formatter).toInstant()
} catch (e: DateTimeParseException) {
e.printStackTrace()
null
}
}
@ToJson
fun toJson(date: Instant): String {
throw NotImplementedError()
}
}
We want the default format to be strict. Strict systems are simpler.
@huntj88 Thank you, I'll take a look.
@swankjesse For sure! But doesn't JsonAdapter have a setLenient() method? :smile:
It would be nice if we could set default timezone for Rfc3339DateJsonAdapter.
Most helpful comment
We want the default format to be strict. Strict systems are simpler.