jsonutils.toJson now serializes JsonNode as is by default (#18097)

* jsonutils.toJson now serializes JsonNode as is (without deep copy nor treating it as a regular ref object)

* JsonNodeMode
This commit is contained in:
Timothee Cour
2021-05-31 13:17:52 -07:00
committed by GitHub
parent 9559350e34
commit 369a7d1246
3 changed files with 41 additions and 5 deletions

View File

@@ -91,6 +91,28 @@ template fn() =
doAssert b2.ord == 1 # explains the `1`
testRoundtrip(a): """[1,2,3]"""
block: # JsonNode
let a = ((1, 2.5, "abc").toJson, (3, 4.5, "foo"))
testRoundtripVal(a): """[[1,2.5,"abc"],[3,4.5,"foo"]]"""
block:
template toInt(a): untyped = cast[int](a)
let a = 3.toJson
let b = (a, a)
let c1 = b.toJson
doAssert c1[0].toInt == a.toInt
doAssert c1[1].toInt == a.toInt
let c2 = b.toJson(ToJsonOptions(jsonNodeMode: joptJsonNodeAsCopy))
doAssert c2[0].toInt != a.toInt
doAssert c2[1].toInt != c2[0].toInt
doAssert c2[1] == c2[0]
let c3 = b.toJson(ToJsonOptions(jsonNodeMode: joptJsonNodeAsObject))
doAssert $c3 == """[{"isUnquoted":false,"kind":2,"num":3},{"isUnquoted":false,"kind":2,"num":3}]"""
block: # ToJsonOptions
let a = (me1, me2)
doAssert $a.toJson() == "[1,2]"