Thread
#serialization
    p

    Patrick

    2 years ago
    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.
    Kroppeb

    Kroppeb

    2 years ago
    Kotlin always needs to be told what the type is explicitly.
    p

    Patrick

    2 years ago
    Can I do something like this:
    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?
    Kroppeb

    Kroppeb

    2 years ago
    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

    Patrick

    2 years ago
    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
    Kroppeb

    Kroppeb

    2 years ago
    Yes json allows mixed arrays.
    What other problems?
    p

    Patrick

    2 years ago
    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
    Kroppeb

    Kroppeb

    2 years ago
    I think you need to make
    AnySerializer
    extend
    KSerializer<Any>
    p

    Patrick

    2 years ago
    I also don't really understand the error message. It says "argument-less serializer". What arguments?
    I'll try that
    Kroppeb

    Kroppeb

    2 years ago
    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

    Patrick

    2 years ago
    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.
    Kroppeb

    Kroppeb

    2 years ago
    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

    Patrick

    2 years ago
    Yep, that's correct. It works if I dont say
    List<Any>
    but
    List<String>
    Kroppeb

    Kroppeb

    2 years ago
    Or is the structure of the array always the same by any chance?
    p

    Patrick

    2 years ago
    No I get this JSON from another application
    Kroppeb

    Kroppeb

    2 years ago
    wait,
    List<String>
    works but not
    List<Any>
    ?
    p

    Patrick

    2 years ago
    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
    Kroppeb

    Kroppeb

    2 years ago
    try
    decoder.decodeNotNullMark()
    val valueString = decoder.decodeString()
    I doubt that's gonna change anything but it might.
    p

    Patrick

    2 years ago
    IT WORKS HAHA
    Kroppeb

    Kroppeb

    2 years ago
    Mmmh, I feel like that doesn't follow the specification but it's hard to tell with such minimal documentation
    p

    Patrick

    2 years ago
    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
    Kroppeb

    Kroppeb

    2 years ago
    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

    Patrick

    2 years ago
    So it's undocumented bahavior?
    Kroppeb

    Kroppeb

    2 years ago
    I think so
    If i search for issues with
    json
    it gives me 132
    50 with
    bug
    label
    p

    Patrick

    2 years ago
    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
    Kroppeb

    Kroppeb

    2 years ago
    No, the serialization api doesn't provide any help with that afaik
    p

    Patrick

    2 years ago
    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
    Kroppeb

    Kroppeb

    2 years ago
    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

    Patrick

    2 years ago
    🤷‍♂️
    Kroppeb

    Kroppeb

    2 years ago
    Is the code for native different from jvm by any chance?
    p

    Patrick

    2 years ago
    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.