fluidsonic / fluid-json

A JSON library written in pure Kotlin
Apache License 2.0
31 stars 7 forks source link

Polymorphism support #32

Closed raderio closed 5 years ago

raderio commented 5 years ago
sealed class Shape
class Circle(val radius: Double) : Shape
class Rectangle(val width: Double) : Shape

There are 2 types of polymorphism:

  1. Polymorphism by field value, aka discriminator 1.1 Discriminator embedded in object
    [ { "type": "CIRCLE", "radius": 10.0 }, { "type": "RECTANGLE", "width": 20.0 } ]

    1.2 Discriminator external

    [ { "type": "CIRCLE", "data": { "radius": 10.0 } }, { "type": "RECTANGLE", "data": { "width": 20.0 } } ]
  2. Polymorphism by filed name, aka union, or anyOf from Swagger
    [ { "radius": 10.0 }, { "width": 20.0 } ]

    Because only Circle class has radius field, first object from list will be deserialized in Circle class.

Also should be possible to do nested deserialization(multi level).

Please add support for all this cases.

fluidsonic commented 5 years ago

All cases can already be implemented manually using JSONDecoderCodecs. What would simplify this for you? Or is it just that you need some examples? Are you actually using codecs or what exactly is the context here?