fixes #6438
This commit is contained in:
parent
7083c8df57
commit
d453bc1d4d
1 changed files with 25 additions and 10 deletions
|
|
@ -769,6 +769,19 @@ proc `%`*[T](elements: openArray[T]): JsonNode =
|
||||||
result = newJArray()
|
result = newJArray()
|
||||||
for elem in elements: result.add(%elem)
|
for elem in elements: result.add(%elem)
|
||||||
|
|
||||||
|
when false:
|
||||||
|
# For 'consistency' we could do this, but that only pushes people further
|
||||||
|
# into that evil comfort zone where they can use Nim without understanding it
|
||||||
|
# causing problems later on.
|
||||||
|
proc `%`*(elements: set[bool]): JsonNode =
|
||||||
|
## Generic constructor for JSON data. Creates a new `JObject JsonNode`.
|
||||||
|
## This can only be used with the empty set ``{}`` and is supported
|
||||||
|
## to prevent the gotcha ``%*{}`` which used to produce an empty
|
||||||
|
## JSON array.
|
||||||
|
result = newJObject()
|
||||||
|
assert false notin elements, "usage error: only empty sets allowed"
|
||||||
|
assert true notin elements, "usage error: only empty sets allowed"
|
||||||
|
|
||||||
proc `%`*(o: object): JsonNode =
|
proc `%`*(o: object): JsonNode =
|
||||||
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
||||||
result = newJObject()
|
result = newJObject()
|
||||||
|
|
@ -789,27 +802,25 @@ proc `%`*(o: enum): JsonNode =
|
||||||
proc toJson(x: NimNode): NimNode {.compiletime.} =
|
proc toJson(x: NimNode): NimNode {.compiletime.} =
|
||||||
case x.kind
|
case x.kind
|
||||||
of nnkBracket: # array
|
of nnkBracket: # array
|
||||||
|
if x.len == 0: return newCall(bindSym"newJArray")
|
||||||
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]))
|
||||||
|
result = newCall(bindSym"%", result)
|
||||||
of nnkTableConstr: # object
|
of nnkTableConstr: # object
|
||||||
|
if x.len == 0: return newCall(bindSym"newJObject")
|
||||||
result = newNimNode(nnkTableConstr)
|
result = newNimNode(nnkTableConstr)
|
||||||
for i in 0 .. <x.len:
|
for i in 0 .. <x.len:
|
||||||
x[i].expectKind nnkExprColonExpr
|
x[i].expectKind nnkExprColonExpr
|
||||||
result.add(newNimNode(nnkExprColonExpr).add(x[i][0]).add(toJson(x[i][1])))
|
result.add newTree(nnkExprColonExpr, x[i][0], toJson(x[i][1]))
|
||||||
|
result = newCall(bindSym"%", result)
|
||||||
of nnkCurly: # empty object
|
of nnkCurly: # empty object
|
||||||
result = newNimNode(nnkTableConstr)
|
|
||||||
x.expectLen(0)
|
x.expectLen(0)
|
||||||
|
result = newCall(bindSym"newJObject")
|
||||||
of nnkNilLit:
|
of nnkNilLit:
|
||||||
result = newCall("newJNull")
|
result = newCall(bindSym"newJNull")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
result = x
|
result = newCall(bindSym"%", x)
|
||||||
|
|
||||||
result = prefix(result, "%")
|
|
||||||
|
|
||||||
macro `%*`*(x: untyped): untyped =
|
macro `%*`*(x: untyped): untyped =
|
||||||
## Convert an expression to a JsonNode directly, without having to specify
|
## Convert an expression to a JsonNode directly, without having to specify
|
||||||
|
|
@ -1934,4 +1945,8 @@ when isMainModule:
|
||||||
except JsonParsingError:
|
except JsonParsingError:
|
||||||
doAssert getCurrentExceptionMsg().contains(errorMessages[errEofExpected])
|
doAssert getCurrentExceptionMsg().contains(errorMessages[errEofExpected])
|
||||||
|
|
||||||
|
# bug #6438
|
||||||
|
doAssert($ %*[] == "[]")
|
||||||
|
doAssert($ %*{} == "{}")
|
||||||
|
|
||||||
echo("Tests succeeded!")
|
echo("Tests succeeded!")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue