I'm trying to serialize a tree of expressions that...
# serialization
s
I'm trying to serialize a tree of expressions that must match a format that will be sent to an api. The expressions are a set of sealed classes that may or may not contain other expression classes. For instance the root is always an expression class that contains a list of other expression object as one of it's properties. Each node in the tree has it's own discriminator property called type. Right now I don't need to deserialize this json but I may need to in the future. The important part is that my output match the api. An example looks like:
Copy code
{
  "type": "V",
  "expressions": [
    {
      "type": "M",
      "expr": "5+6"
    }
  ],
  "resultType": "success"
}
I started to go down the route of using the polymorphic serializer but I didn't know how to customize the
type
output. Should I write my own custom serializer for this type? If so is there a good example of how to do this?
d
You don't need a custom serializer.
Just annotate the parent class with
@Serializer(PolymorphicSerializer::class)
.
Then the children with
@SerialName("M")
or
@SerialName("V")
.
s
That worked out perfectly. Thank you for the help.