Is there a built-in serializer to "inline" singula...
# serialization
f
Is there a built-in serializer to "inline" singular-field classes? Example:
Copy code
@Serializable(InlineSerializer::class)
    class Inlined(val str :String)

   fun main(){
        val obj = Inlined("test")
        val str = json.stringify(Inlined.serializer(),obj)
        assertEquals("\"test\"",str) // And not {"str":"test"}
   }
t
The tricky part there is when you have to deserialize it, how do you know that it's an
Inlined
and not just a
String
.
But to answer your question, I haven't seen one, but it's relatively easy to write. You just do
encoder.encodeString(obj.str)
in a custom serializer.
p
Inline classes serialization is not supported at the moment. The compiler throws an exception if you annotate it with
Serializable
.
Cause: org.jetbrains.kotlin.codegen.ErasedInlineClassBodyCodegen cannot be cast to org.jetbrains.kotlin.codegen.ImplementationBodyCodegen
f
@Toddobryan That's what I did 🙂 I made it generic though so It's a bit more complicated @Paulius Ruminas Not quite what I meant
p
Oh you did not use the
inline
modifier I did not see that.