Hi there, this is a thing that's a little bit harder in Nim than some other 
languages, but here's how you can do it without switching to object variants.
    
    
    import json
    
    type
      Base = ref object of RootObj
        a: int
      Child = ref object of Base
        b: int
    
    method serialise*(self: Base): JsonNode {.base.} =
      result = newJObject()
      for k,v in self[].fieldPairs:
        result[k] = %* v
    
    method serialise*(self: Child): JsonNode =
      result = newJObject()
      for k,v in self[].fieldPairs:
        result[k] = %* v
    
    var s = newSeq[Base]()
    s.add(Base(a: 10))
    s.add(Child(a: 12, b: 15))
    
    var j = newJArray()
    for b in s:
      j.add(b.serialise())
    
    echo $j
    
    
    Run

the downside is you need to create methods for each child type, you could 
generate these with a macro or template though.

Reply via email to