(Not Kord) Deserialize JSON with unordered fields (Kotlin Serialization)
Hello
I have this deserialization function from my custom Serializer:
When the JSON is:
That works perfectly.
But when the json is:
The exception
The field 'type' is required
(from the loop) is thrown.
is there a way to support unordered fields?18 Replies
Yeah don't use a custom serializr, an auto generated serializer could handle that
You can also use a JsonContentPolymorphicSerializer (i think that was the name), or save everything into a map first
Yes and no
In the idea, I would to have something like that:
If I use JsonDiscriminator:
- The type is a name, but I would like to have a byte
- The type will be in the nested
data
object and not out with other metadatayeah sadly kx.ser cannot do that, the best way is to not use a byte or use the JsonContentPolymorphicSerializer
Yes but there is a problem again, the structure will be:
doesn't have to
you can make something like this
Solution
Yes that's what I did at the beginning
I found the syntax a little bit annoying (but probably less than my terrible serializer ah ah)
And also the thing that pushed me on my weird solution is for the byte for the type. I need to reduce the network latency and of course a byte is more quick to transfer than string
do you use json?
yes
well using a byte will make little difference then
especially if you use numbers
whether you encode
0
or "0"
won't make a differenceaah and the serialName should be the number instead of "hello" (for example)
that could work
I wil ltry with JsonContentPolymorphicSerializer too to see this thing
So sure there is no way for the number? :/
not without a custom serializer no
Well I imagine two extra bytes is acceptable for the gain of maintenability
if you're worried about traffic you shouldn't use json
use some binary format like protobuf
true
I will maybe check that. But need to check how handle that in front-end
Hum I think proto could be a better solution
Protobuf read sequentially we're agree no?
Because in a decoder, when I try to use
decodeSequentially
it returns false ..
According to the documentation, that's notNo idea tbh
Protobuf is definively the solution 👍
Thanks