The to macro drops the seq items when I try to unmarshall a recursive type:
import sequtils, json
type
ContentNodeKind* = enum
P,
Br,
Text,
ContentNode* = object
case kind*: ContentNodeKind
of P: pChildren*: seq[ContentNode]
of Br: nil
of Text: textStr*: string
let mynode = ContentNode(kind: P, pChildren: @[
ContentNode(kind: Text, textStr: "mychild"),
ContentNode(kind: Br)
])
echo "Original: " & $mynode
let jsonNode = %*mynode
echo "jsonNode: " & $jsonNode
echo "Reversed: " & $jsonNode.to(ContentNode)
Original: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
jsonNode: {"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}
Reversed: (kind: P, pChildren: @[])
Original: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
jsonNode: {"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}
Reversed: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
$ nim --version
Nim Compiler Version 1.0.6 [Linux: amd64]
Compiled at 2020-01-23
Copyright (c) 2006-2019 by Andreas Rumpf
active boot switches: -d:release
I confirm the issue: https://play.nim-lang.org/#ix=1Kjl
Though it seems to happen when using case objects. When using normal objects, it works fine: https://play.nim-lang.org/#ix=1Kjm
I can't reproduce the issue on my computer, my output is:
Original: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
jsonNode: {"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}
Reversed: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
nim --version
Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2020-02-26
Copyright (c) 2006-2019 by Andreas Rumpf
git hash: e4ed19c12fcce046d990a918800d9031706e8b66
active boot switches: -d:release -d:danger
@krux02 It didn't occur to me to try it out on devel.. Indeed this issue cannot be reproduced on the devel branch. The exact same code as in https://play.nim-lang.org/#ix=1Kjl gives the below output on devel:
Hint: /home/kmodi/temp/foo [Exec]
Nim object: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
object->JsonNode: {"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}
JsonNode->object: (kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])
@[(kind: Text, textStr: "mychild"), (kind: Br)]
@[(kind: Text, textStr: "mychild"), (kind: Br)]
Then I am closing this as resolved.
Should this have been closed after adding a test in this category (case objects + JSON set/deser) if one doesn't exist?
Don't know. My guess would be, somebody actively fixed this bug and already added a test for it.
I agree, a test should be added before closing this (unless we can point here to an existing test)
@timotheecour ok you convinced me. Then please check the tests if there is a test case already existent. If not integrate a test in the existing test environment.