Should avoid `@Serializer` in companion objects?
# serialization
j
Should avoid
@Serializer
in companion objects?
This let me avoiding to add to the serialization module the class and it works out of the box, but docs says
You cannot define your own function 
serializer()
 on a companion object of a serializable class
But I don't know if it is only about override that function
Well I found an example in some tests, I don't know if there was an example about this previously when I created similar classes some months ago https://github.com/Kotlin/kotlinx.serialization/blob/e2e764a132c8eebd31208120774baf9a71ec23c7/formats/json/jvmTest/src/kotlinx/serialization/features/SerializerByTypeTest.kt
d
I tend to use
object
instead of
companion object
.
b
By doing it on the companion object like you did there, it becomes the default serializer for that class. If that's the the behavior you want, then that looks good!
👍 1
j
@Dominaezzz in that way you have to add the serializer to the SerializationModule or explicit it in Serializable
d
Yeah, I like to be explicit with this sorta thing.