diff options
author | flywind <xzsflywind@gmail.com> | 2021-03-05 14:41:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-04 22:41:20 -0800 |
commit | cda443ee680d7a8347f00486a5aa860001fcd6de (patch) | |
tree | 3ec485f7330327f6067038ea0770931895818a60 /tests/stdlib/tjson.nim | |
parent | f28dc2c61ed2ee4452441c1a5508cb1e46a8f288 (diff) | |
download | Nim-cda443ee680d7a8347f00486a5aa860001fcd6de.tar.gz |
follow up #17165 (#17262)
Diffstat (limited to 'tests/stdlib/tjson.nim')
-rw-r--r-- | tests/stdlib/tjson.nim | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/stdlib/tjson.nim b/tests/stdlib/tjson.nim index b71961554..eb31fcef9 100644 --- a/tests/stdlib/tjson.nim +++ b/tests/stdlib/tjson.nim @@ -244,3 +244,43 @@ when defined(js): # xxx fixme doAssert x.kind == JInt else: doAssert x.kind == JString + +block: # bug #15835 + type + Foo = object + ii*: int + data*: JsonNode + + block: + const jt = """{"ii": 123, "data": ["some", "data"]}""" + let js = parseJson(jt) + discard js.to(Foo) + + block: + const jt = """{"ii": 123}""" + let js = parseJson(jt) + doAssertRaises(KeyError): + echo js.to(Foo) + +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) +]) + +doAssert $mynode == """(kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])""" + +let jsonNode = %*mynode +doAssert $jsonNode == """{"kind":"P","pChildren":[{"kind":"Text","textStr":"mychild"},{"kind":"Br"}]}""" +doAssert $jsonNode.to(ContentNode) == """(kind: P, pChildren: @[(kind: Text, textStr: "mychild"), (kind: Br)])""" + |