Hey guys! How are you enforcing required parameter...
# ktor
y
Hey guys! How are you enforcing required parameters when receiving JSON objects? I'm using Gson content negotation, and initially expected
ContentTransformationException
when receiving my custom parameters class with missing fields, but instead Gson is assigning `0`/`null` to missing fields, so no exception is thrown. Jackson seems to have a
@JsonProperty(required = true)
annotation, but I'd rather not repeat that everywhere if there's a better solution.
g
Gson knows nothing about nullability, default types or Kotlin You can fix some problems with custom adapters or using libraries that generate adapters for you In general Gson has worst support of Kotlin among popular Json libraries
y
Yeah I didn't expect it to support strict nullability or default values since it's Java, but my use cases don't include those, so it's worked for me thus far. I just need it to fail when a property is missing. That's interesting. Why is Gson among the 2 officially supported ktor libraries? Convenience for people coming from Java? I'm just trying to get a feel for how people are solving this before jumping into custom stuff. Klaxon? Anything else?
g
Jackson, Moshi have better Kotlin support with additional libraries
👍 3
And yeah, Klaxon is originally Kotlin library
I hope that we will have stable kotlinx.serialization soon and it solves most of problems
Actually, even with Gson you can use default values if you have default constructor (for example if all constructor arguments have default value or you use default constructor plugin) But this works only if you don’t have
null
as value of some field, in this case Gson will set null there
y
Yeah I noticed kotlinx.serialization is still in incubation 😞 And didn't know that about Gson, interesting!