can someone explain what i need to put into that s...
# serialization
l
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
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
any other way to make this work? would it help to use the compaion object as the custom serializer ? or sth similar ?
e
as the kotlinx.coroutines docs say, you cannot write your own Compaion.serializer()
Copy code
val json = Json {
    serializersModule = SerializersModule {
        contextual(FruitAsIntSerializer)
    }
}
json.encodeToString(Fruit.Apple)
also works
l
Copy code
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 understand 1. 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
You cannot define your own function
serializer()
on a companion object of a serializable class
well apparently i can lol
e
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
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
it actually will, in future versions, but not yet
l
welp, guess im just gonna hope that
fun serializer() = FruitAsIntSerializer
keeps working until its getting autogenerated
l
oh it got merged already, guess we'll see it very soon then ?
e
in the meanwhile, I just realized that you just need public properties, so
Copy code
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
@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
err, nevermind the
@Serializer(forClass =)
suggestion, that doesn't get looked up the way I thought it did
l
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
it's in master so 1.5.20 or 1.5.30 (I haven't checked where the release train is)
l
then i should be fine using the workaround i found. ty 4 ur help
e
I don't see it in 1.5.20-RC so possibly 1.5.30