jsonutils: fromJson forward opt param fix (#16612)
This commit is contained in:
parent
58b9191354
commit
0d5cab77f6
2 changed files with 18 additions and 4 deletions
|
|
@ -201,17 +201,17 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
|
||||||
if b.kind == JNull: a = nil
|
if b.kind == JNull: a = nil
|
||||||
else:
|
else:
|
||||||
a = T()
|
a = T()
|
||||||
fromJson(a[], b)
|
fromJson(a[], b, opt)
|
||||||
elif T is array:
|
elif T is array:
|
||||||
checkJson a.len == b.len, $(a.len, b.len, $T)
|
checkJson a.len == b.len, $(a.len, b.len, $T)
|
||||||
var i = 0
|
var i = 0
|
||||||
for ai in mitems(a):
|
for ai in mitems(a):
|
||||||
fromJson(ai, b[i])
|
fromJson(ai, b[i], opt)
|
||||||
i.inc
|
i.inc
|
||||||
elif T is seq:
|
elif T is seq:
|
||||||
a.setLen b.len
|
a.setLen b.len
|
||||||
for i, val in b.getElems:
|
for i, val in b.getElems:
|
||||||
fromJson(a[i], val)
|
fromJson(a[i], val, opt)
|
||||||
elif T is object:
|
elif T is object:
|
||||||
template fun(key, typ): untyped {.used.} =
|
template fun(key, typ): untyped {.used.} =
|
||||||
if b.hasKey key:
|
if b.hasKey key:
|
||||||
|
|
@ -237,7 +237,7 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
|
||||||
checkJson b.kind == JArray, $(b.kind) # we could customize whether to allow JNull
|
checkJson b.kind == JArray, $(b.kind) # we could customize whether to allow JNull
|
||||||
var i = 0
|
var i = 0
|
||||||
for val in fields(a):
|
for val in fields(a):
|
||||||
fromJson(val, b[i])
|
fromJson(val, b[i], opt)
|
||||||
i.inc
|
i.inc
|
||||||
checkJson b.len == i, $(b.len, i, $T, b) # could customize
|
checkJson b.len == i, $(b.len, i, $T, b) # could customize
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,20 @@ template fn() =
|
||||||
testRoundtrip(Foo[int](t1: false, z2: 7)): """{"t1":false,"z2":7}"""
|
testRoundtrip(Foo[int](t1: false, z2: 7)): """{"t1":false,"z2":7}"""
|
||||||
# pending https://github.com/nim-lang/Nim/issues/14698, test with `type Foo[T] = ref object`
|
# pending https://github.com/nim-lang/Nim/issues/14698, test with `type Foo[T] = ref object`
|
||||||
|
|
||||||
|
block: # bug: pass opt params in fromJson
|
||||||
|
type Foo = object
|
||||||
|
a: int
|
||||||
|
b: string
|
||||||
|
c: float
|
||||||
|
var f: seq[Foo]
|
||||||
|
try:
|
||||||
|
fromJson(f, parseJson """[{"b": "bbb"}]""")
|
||||||
|
doAssert false
|
||||||
|
except ValueError:
|
||||||
|
doAssert true
|
||||||
|
fromJson(f, parseJson """[{"b": "bbb"}]""", Joptions(allowExtraKeys: true, allowMissingKeys: true))
|
||||||
|
doAssert f == @[Foo(a: 0, b: "bbb", c: 0.0)]
|
||||||
|
|
||||||
block testHashSet:
|
block testHashSet:
|
||||||
testRoundtrip(HashSet[string]()): "[]"
|
testRoundtrip(HashSet[string]()): "[]"
|
||||||
testRoundtrip([""].toHashSet): """[""]"""
|
testRoundtrip([""].toHashSet): """[""]"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue