mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-21 18:36:50 +00:00
Merge pull request #3829 from def-/json-fix-empty
Fix json.`%*` for empty objects
This commit is contained in:
@@ -712,17 +712,21 @@ proc `%`*(elements: openArray[JsonNode]): JsonNode =
|
||||
|
||||
proc toJson(x: NimNode): NimNode {.compiletime.} =
|
||||
case x.kind
|
||||
of nnkBracket:
|
||||
of nnkBracket: # array
|
||||
result = newNimNode(nnkBracket)
|
||||
for i in 0 .. <x.len:
|
||||
result.add(toJson(x[i]))
|
||||
|
||||
of nnkTableConstr:
|
||||
of nnkTableConstr: # object
|
||||
result = newNimNode(nnkTableConstr)
|
||||
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])))
|
||||
|
||||
of nnkCurly: # empty object
|
||||
result = newNimNode(nnkTableConstr)
|
||||
x.expectLen(0)
|
||||
|
||||
else:
|
||||
result = x
|
||||
|
||||
|
||||
Reference in New Issue
Block a user