Hi does Kotlin serialisation have an equivalent fe...
# serialization
t
Hi does Kotlin serialisation have an equivalent feature to Jackson Json's
Copy code
@JsonAnyGetter

    @JsonAnySetter
that can be employed to cope with any unexpected json message fields?
e
Are you interested in the unexpected fields? What do you want to do with them?
👍 1
t
i am interested in unexpected fields i want to stop them crashing my mobile application when json parsing fails! 😄
e
You could write your own wrapper for
decodeFromString
which prints the value if decoding fails.
Copy code
inline fun <reified T> Json.decodeOrThrow(s: String): T = runCatching {
  decodeFromString(d)
}.onFailure { t ->
  logger.error("Failed to parse <$s> to ${T::class.simpleName}", t)
}.getOrThrow()
c
You can just configure the serializer to ignore everything it doesn't expect:
Copy code
Json {
    ignoreUnknownKeys = true
}
☝️ 1
t
You can just configure the serializer to ignore everything it doesn't expect: @CLOVIS I do not want to do that, or atleast not for all my api calls my application has to conditionally fail fast when it encounters unexpected json fields
c
I don't understand your problem then, "conditionally fail fast when it encounters unexpected json fields" is what
ignoreUnknownKeys = false
does. You don't need to do anything more.
t
my application retrieves data from multiple remote servers i use retrofit configured in hilt modules for all these calls some of the remote services i need to fail fast the remaining remore services need to be lenient and allow unexpected json fields im looking for a simple solution to configuring this difference in behaviour
c
Have a different
Json
instance for each remote server, with the configuration you want for that remote server.