Also is there a way to automatically serialize typ...
# serialization
p
Also is there a way to automatically serialize types in Kotlin Native? It seems very hard to automatically parse primitive types to the correct Kotlin type. For example ["foo", 1, 1.0] should automatically be parsed to kotlin.String, kotlin.Int, kotlin.Float.
k
Kotlin always needs to be told what the type is explicitly.
p
Can I do something like this:
Copy code
override fun deserialize(decoder: Decoder): Arg {

    val valueString = decoder.decodeString()

    try {
        return Arg(valueString.toInt())
    } catch (t: NumberFormatException) {

    }

    try {
        return Arg(valueString.toFloat())
    } catch (t: NumberFormatException) {

    }

    return Arg(valueString)
}
This is for example if I know that the value can either be an Int, Float or String
It seems strange because the JSON specification should account for this, right?
k
That might depend on what decoder you are using. Whether they support reading integers as strings. Json and similar decoders might support that but if you have a decoder handling with binary data it probably wouldn't.
Yeah, but serialization isn't json specific
p
Reading Integers as Strings works if you use isLenient=true. This surprised me because I thought the JSON specification allows Integers and Floats in arrays
But then I run into a bunch of other errors for some reason. I think my approach should work but somehow it doesn't
k
Yes json allows mixed arrays.
What other problems?
p
Like the problem in the post above this one
I can't get it to work even though it should. All I am trying to do is re-implement what ContextualSerialization already does
k
I think you need to make
AnySerializer
extend
KSerializer<Any>
p
I also don't really understand the error message. It says "argument-less serializer". What arguments?
I'll try that
k
An argument-less serializer is a serializer that doesn't take any type params (and serializers for those params). I don't see why it's causing that msg to show
p
The funny thing is now I am getting this error:
Uncaught Kotlin exception: kotlinx.serialization.json.JsonDecodingException: Unexpected JSON token at offset 50: Expected string or non-null literal.
k
I'm guessing it doesn't like that there is a number there instead of a string
Do you have control over the json format?
p
Yep, that's correct. It works if I dont say
List<Any>
but
List<String>
k
Or is the structure of the array always the same by any chance?
p
No I get this JSON from another application
k
wait,
List<String>
works but not
List<Any>
?
p
Exactly, it parses the Int values as strings though
I guess I could write my own converter after the values have already been imported as Strings
That wouldn't be pretty, but it would work I guess
Unless someone wants to explicitly provide for example "1" as a String
k
try
Copy code
decoder.decodeNotNullMark()
val valueString = decoder.decodeString()
I doubt that's gonna change anything but it might.
p
IT WORKS HAHA
k
Mmmh, I feel like that doesn't follow the specification but it's hard to tell with such minimal documentation
p
Yeah I guess so. There's no documentation about how to build your own decoders and encoders, right?
All I could find were the code comments
k
No, I've been building one and it took me way to long to figure out everything.
https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/custom_serializers.md#representing-classes-as-a-single-value This shows just a call to
decodeString()
so it isn't normal to require a call to
decodeNotNullMark()
p
So it's undocumented bahavior?
k
I think so
If i search for issues with
json
it gives me 132
50 with
bug
label
p
Yeah well unfortunately most of the Kotlin ecosystem is still experimental
Especially Kotlin Native
Do you have an idea how to check if the value was supplied as a string in the first place? I think if I can check for that it I would have covered every case
I don't want to parse "1" as an Int for example
k
No, the serialization api doesn't provide any help with that afaik
p
Ok thanks anyway you've already helped me out a lot
I guess we have to just be vocal about these issues and they will be fixed eventually
k
Having a quick look at the code I don't understand how that worked. I know where the error is thrown, but I don't see how calling
NotNullMark
changed anything
p
🤷‍♂️
k
Is the code for native different from jvm by any chance?
p
It's not different at the moment and the tests pass for jvm and native. But native does not have reflection so there can always be differences I need to be aware of. And JS is a whole other monster.