Jsonutils: pass opt for object in object (#16615)

* jsonutils: fromJson forward opt param fix

* jsonutils: object in object test + fix: opt pass
This commit is contained in:
inv2004 2021-01-07 05:09:02 +03:00 • committed by GitHub
commit 04b765c16d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View file

@ -119,7 +119,7 @@ template fromJsonFields(newObj, oldObj, json, discKeys, opt) =
when key notin discKeys: when key notin discKeys:
if json.hasKey key: if json.hasKey key:
numMatched.inc numMatched.inc
fromJson(val, json[key]) fromJson(val, json[key], opt)
elif opt.allowMissingKeys: elif opt.allowMissingKeys:
# if there are no discriminant keys the `oldObj` must always have the # if there are no discriminant keys the `oldObj` must always have the
# same keys as the new one. Otherwise we must check, because they could # same keys as the new one. Otherwise we must check, because they could

View file

@ -123,6 +123,9 @@ template fn() =
a: int a: int
b: string b: string
c: float c: float
type Bar = object
foo: Foo
boo: string
var f: seq[Foo] var f: seq[Foo]
try: try:
fromJson(f, parseJson """[{"b": "bbb"}]""") fromJson(f, parseJson """[{"b": "bbb"}]""")
@ -131,6 +134,9 @@ template fn() =
doAssert true doAssert true
fromJson(f, parseJson """[{"b": "bbb"}]""", Joptions(allowExtraKeys: true, allowMissingKeys: true)) fromJson(f, parseJson """[{"b": "bbb"}]""", Joptions(allowExtraKeys: true, allowMissingKeys: true))
doAssert f == @[Foo(a: 0, b: "bbb", c: 0.0)] doAssert f == @[Foo(a: 0, b: "bbb", c: 0.0)]
var b: Bar
fromJson(b, parseJson """{"foo": {"b": "bbb"}}""", Joptions(allowExtraKeys: true, allowMissingKeys: true))
doAssert b == Bar(foo: Foo(a: 0, b: "bbb", c: 0.0))
block testHashSet: block testHashSet:
testRoundtrip(HashSet[string]()): "[]" testRoundtrip(HashSet[string]()): "[]"