On that note - what is the reason that the discrim...
# serialization
m
On that note - what is the reason that the discriminator field name is set globally rather than on a per-type/serializer basis? Depending on what model you consume (or produce) it may be necessary to have it named differently for different types.
s
I have a feeling that this is usually a some API property. So if you have to different APIs with different settings, use different Json instances
👍 1
m
not different API - different types within the same API
I'm not talking about consuming Kotlin Serialization-based APIs but just normal JSON APIs of which there are thousands. They can use whatever property for discriminating different types in different parts of the JSON response. Like
type
in one case,
groupType
in another case,
shape
in another case etc.
s
Oh. That doesn't look like convenient and consistent API.. Are there really so many APIs like that?
👍 1
m
Then take
type
for GeoJSON,
@type
for JSON-LD. Different properties for discrimination. Can all be part of the same response. What makes a good name for a discriminator should depend on the thing which the type represents, not some generic property name.
And we're talking about public web APIs. In reality they all look very different and may not always be best practice. Yet Kotlin Serialization cannot parse them (fully) because the discriminators vary (which is not necessarily bad practice).
s
Ok, I see your point. Some extension mechanism for JSON can be handy, I'll think about it then.
m
Another example: For
Animal
for example someone could use
species
instead of
type
because they denote different things. yet the whole API may be bound to use
type
for discrimination. The API consumer would be confused. IMO it really depends on each case what makes a sensible name for a discriminator.
type
happens to be a good default but it's also a very "technical" point of view.
v
Hi, I am also struggling with this. Did you end up solving this? Any pointers to how to go about it? https://kotlinlang.slack.com/archives/C7A1U5PTM/p1580122388012100