tjson_unmarshall.nim 745 B

1234567891011121314151617181920212223242526272829303132
  1. discard """
  2. output: '''
  3. Original: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
  4. jsonNode: {"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}
  5. Reversed: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
  6. '''
  7. """
  8. import json
  9. type
  10. ContentNodeKind* = enum
  11. P,
  12. Br,
  13. Text,
  14. ContentNode* = object
  15. case kind*: ContentNodeKind
  16. of P: pChildren*: seq[ContentNode]
  17. of Br: nil
  18. of Text: textStr*: string
  19. let mynode = ContentNode(kind: P, pChildren: @[
  20. ContentNode(kind: Text, textStr: "mychild"),
  21. ContentNode(kind: Br)
  22. ])
  23. echo "Original: " & $mynode
  24. let jsonNode = %*mynode
  25. echo "jsonNode: " & $jsonNode
  26. echo "Reversed: " & $jsonNode.to(ContentNode)