How could I build observable hierarchy from XML in code and avoid boilerplate using Kotlin and Jackson?
I see some common parts in XML I want to parse and want to reflect it with kind of inheritance in code. For example, (1)
// @JacksonXml... annotations omitted open class FlatCoords( val x: Int, val y: Int ) class FilmCoords( val x: Int, val y: Int, val frame: Int ) : FlatCoords(x, y) class 3DCoords( val x: Int, val y: Int, val z: Int ) : FlatCoords(x, y)
Here I need to repeat properties again and again that almost nullifies benefit from such inheritance.
Also (2), I can set default values for all properties and avoid passing it through by using empty parent constructor. But I can’t create such derived objects by code, although Jackson will parse it normally.
class FilmCoords( val frame: Int ) : FlatCoords()
I decided I can use composition and unwrapping (3):
class FlatCoords( val x: Int, val y: Int ) class FilmCoords( @JsonUnwrapped val flat: FlatCoords val frame: Int )
But Jackson currently doesn’t support unwrapping in Creator (like a constructor).
So, which another way will be the most elegant?