Merge pull request #3829 from def-/json-fix-empty
Fix json.`%*` for empty objects
This commit is contained in:
commit
86b233dff9
1 changed files with 7 additions and 3 deletions
|
|
@ -712,17 +712,21 @@ proc `%`*(elements: openArray[JsonNode]): JsonNode =
|
||||||
|
|
||||||
proc toJson(x: NimNode): NimNode {.compiletime.} =
|
proc toJson(x: NimNode): NimNode {.compiletime.} =
|
||||||
case x.kind
|
case x.kind
|
||||||
of nnkBracket:
|
of nnkBracket: # array
|
||||||
result = newNimNode(nnkBracket)
|
result = newNimNode(nnkBracket)
|
||||||
for i in 0 .. <x.len:
|
for i in 0 .. <x.len:
|
||||||
result.add(toJson(x[i]))
|
result.add(toJson(x[i]))
|
||||||
|
|
||||||
of nnkTableConstr:
|
of nnkTableConstr: # object
|
||||||
result = newNimNode(nnkTableConstr)
|
result = newNimNode(nnkTableConstr)
|
||||||
for i in 0 .. <x.len:
|
for i in 0 .. <x.len:
|
||||||
assert x[i].kind == nnkExprColonExpr
|
x[i].expectKind nnkExprColonExpr
|
||||||
result.add(newNimNode(nnkExprColonExpr).add(x[i][0]).add(toJson(x[i][1])))
|
result.add(newNimNode(nnkExprColonExpr).add(x[i][0]).add(toJson(x[i][1])))
|
||||||
|
|
||||||
|
of nnkCurly: # empty object
|
||||||
|
result = newNimNode(nnkTableConstr)
|
||||||
|
x.expectLen(0)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
result = x
|
result = x
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue