Add operator %* to JSON
This commit is contained in:
parent
06ad80cc45
commit
f223e94ccd
1 changed files with 76 additions and 1 deletions
|
|
@ -30,9 +30,28 @@
|
||||||
##
|
##
|
||||||
## 1.3000000000000000e+00
|
## 1.3000000000000000e+00
|
||||||
## true
|
## true
|
||||||
|
##
|
||||||
|
## This module can also be used to comfortably create JSON using the `%*`
|
||||||
|
## operator:
|
||||||
|
##
|
||||||
|
## .. code-block:: nim
|
||||||
|
##
|
||||||
|
## var hisName = "John"
|
||||||
|
## let herAge = 31
|
||||||
|
## var j = %*
|
||||||
|
## [
|
||||||
|
## {
|
||||||
|
## "name": hisName,
|
||||||
|
## "age": 30
|
||||||
|
## },
|
||||||
|
## {
|
||||||
|
## "name": "Susan",
|
||||||
|
## "age": herAge
|
||||||
|
## }
|
||||||
|
## ]
|
||||||
|
|
||||||
import
|
import
|
||||||
hashes, strutils, lexbase, streams, unicode
|
hashes, strutils, lexbase, streams, unicode, macros
|
||||||
|
|
||||||
type
|
type
|
||||||
JsonEventKind* = enum ## enumeration of all events that may occur when parsing
|
JsonEventKind* = enum ## enumeration of all events that may occur when parsing
|
||||||
|
|
@ -625,6 +644,31 @@ proc `%`*(elements: openArray[JsonNode]): JsonNode =
|
||||||
newSeq(result.elems, elements.len)
|
newSeq(result.elems, elements.len)
|
||||||
for i, p in pairs(elements): result.elems[i] = p
|
for i, p in pairs(elements): result.elems[i] = p
|
||||||
|
|
||||||
|
proc toJson(x: expr): expr {.compiletime.} =
|
||||||
|
case x.kind
|
||||||
|
of nnkBracket:
|
||||||
|
result = newNimNode(nnkBracket)
|
||||||
|
for i in 0 .. <x.len:
|
||||||
|
result.add(toJson(x[i]))
|
||||||
|
|
||||||
|
of nnkTableConstr:
|
||||||
|
result = newNimNode(nnkTableConstr)
|
||||||
|
for i in 0 .. <x.len:
|
||||||
|
assert x[i].kind == nnkExprColonExpr
|
||||||
|
result.add(newNimNode(nnkExprColonExpr).add(x[i][0]).add(toJson(x[i][1])))
|
||||||
|
|
||||||
|
else:
|
||||||
|
result = x
|
||||||
|
|
||||||
|
result = prefix(result, "%")
|
||||||
|
|
||||||
|
macro `%*`*(x: expr): expr =
|
||||||
|
## Convert an expression to a JsonParser directly, without having to specify
|
||||||
|
## `%` for every element.
|
||||||
|
echo x.treeRepr
|
||||||
|
result = toJson(x)
|
||||||
|
echo result.treeRepr
|
||||||
|
|
||||||
proc `==`* (a,b: JsonNode): bool =
|
proc `==`* (a,b: JsonNode): bool =
|
||||||
## Check two nodes for equality
|
## Check two nodes for equality
|
||||||
if a.isNil:
|
if a.isNil:
|
||||||
|
|
@ -1087,6 +1131,37 @@ when isMainModule:
|
||||||
except:
|
except:
|
||||||
assert(false, "EInvalidIndex thrown for valid index")
|
assert(false, "EInvalidIndex thrown for valid index")
|
||||||
|
|
||||||
|
# Generator:
|
||||||
|
var j = %* [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]
|
||||||
|
assert j == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
||||||
|
|
||||||
|
var j2 = %*
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "John",
|
||||||
|
"age": 30
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Susan",
|
||||||
|
"age": 31
|
||||||
|
}
|
||||||
|
]
|
||||||
|
assert j2 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
||||||
|
|
||||||
|
var name = "John"
|
||||||
|
let herAge = 30
|
||||||
|
const hisAge = 31
|
||||||
|
|
||||||
|
var j3 = %*
|
||||||
|
[ { "name": "John"
|
||||||
|
, "age": herAge
|
||||||
|
}
|
||||||
|
, { "name": "Susan"
|
||||||
|
, "age": hisAge
|
||||||
|
}
|
||||||
|
]
|
||||||
|
assert j3 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
||||||
|
|
||||||
discard """
|
discard """
|
||||||
while true:
|
while true:
|
||||||
var json = stdin.readLine()
|
var json = stdin.readLine()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue