Thread
#serialization
    l

    LastExceed

    1 year ago
    can someone explain what i need to put into that string? the docs say
    a unique name of the type that is being serialized
    but i dont understand what that means also why does Line 2 work but Line 3 break ?
    e

    ephemient

    1 year ago
    because you've defined an external serializer, there is no
    Fruit.Companion.serializer()
    , so the reflection-based serializer lookup doesn't work
    Box works because it is statically referring to the serializer
    Json.encodeToString(FruitAsIntSerializer, Fruit.Apple)
    will work
    l

    LastExceed

    1 year ago
    any other way to make this work? would it help to use the compaion object as the custom serializer ? or sth similar ?
    e

    ephemient

    1 year ago
    as the kotlinx.coroutines docs say, you cannot write your own Compaion.serializer()
    val json = Json {
        serializersModule = SerializersModule {
            contextual(FruitAsIntSerializer)
        }
    }
    json.encodeToString(Fruit.Apple)
    also works
    l

    LastExceed

    1 year ago
    companion object {
    	fun serializer() = FruitAsIntSerializer
    }
    this seems to work as well, though im not sure if i should rely on that
    there are still so many things i dont understand1. what is the string in the primitive descriptor for ? 2. why is there a reflection based lookup when the library advertises itself as reflectionless ? 3. what is the point of specifying the serializer in the annotation if it cant be looked up from there?
    seems to indicate to me that defining your own
    fun serializer()
    is explicitly not supported
    l

    LastExceed

    1 year ago
    You cannot define your own function
    serializer()
    on a companion object of a serializable class
    well apparently i can lol
    e

    ephemient

    1 year ago
    1. there are formats that will use the serialName 2. that function you're calling uses reflection. there are others that don't. 3. it gets automatically used as the serializer by other
    @Serializable
    -generated serializers
    well apparently i can lol
    for now… no guarantees.
    l

    LastExceed

    1 year ago
    so if i understand correctly, the
    @Serializable
    annotation generates the function in the companion object. then shouldnt it also generate
    fun serializer() = FruitAsIntSerializer
    when i use
    @Serializable(FruitAsIntSerializer::class
    ?
    e

    ephemient

    1 year ago
    it actually will, in future versions, but not yet
    l

    LastExceed

    1 year ago
    welp, guess im just gonna hope that
    fun serializer() = FruitAsIntSerializer
    keeps working until its getting autogenerated
    e

    ephemient

    1 year ago
    l

    LastExceed

    1 year ago
    oh it got merged already, guess we'll see it very soon then ?
    e

    ephemient

    1 year ago
    in the meanwhile, I just realized that you just need public properties, so
    enum class Fruit constructor(val serializedForm: Int) {
        Apple(1),
        Banana(2)
    }
    
    @Serializer(forClass = Fruit::class)
    object FruitAsIntSerializer : KSerializer<Fruit>
    will work too
    plugin.serialization is versioned and released with the Kotlin compiler itself (not with kotlinx.serialization), so... next version of Kotlin, I expect
    BTW, your
    deserialize
    function is bad:
    Json.decodeFromString<Fruit>("2")
    will fail
    you need to make sure to call
    decodeInt()
    exactly once
    l

    LastExceed

    1 year ago
    @Serializer(forClass = Fruit::class)
    didnt work
    next version of Kotlin
    darn
    Json.decodeFromString<Fruit>("2")
     will fail
    as its supposed to, since
    2
    is an invalid value
    no wait you're right, ty for the hint
    e

    ephemient

    1 year ago
    err, nevermind the
    @Serializer(forClass =)
    suggestion, that doesn't get looked up the way I thought it did
    l

    LastExceed

    1 year ago
    do you think it will be added to the next minor version of kotlin, or do i have to wait for 1.6 ?
    ...guess its in the
    master
    branch already, dont think they'd do that if it's for 1.6
    e

    ephemient

    1 year ago
    it's in master so 1.5.20 or 1.5.30 (I haven't checked where the release train is)
    l

    LastExceed

    1 year ago
    then i should be fine using the workaround i found. ty 4 ur help
    e

    ephemient

    1 year ago
    I don't see it in 1.5.20-RC so possibly 1.5.30