Json serialization for openarrays and objects
This commit is contained in:
parent
299989f3aa
commit
6e135468f8
2 changed files with 40 additions and 9 deletions
|
|
@ -707,17 +707,27 @@ proc `%`*(b: bool): JsonNode =
|
||||||
|
|
||||||
proc `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode =
|
proc `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode =
|
||||||
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
||||||
new(result)
|
result = newJObject()
|
||||||
result.kind = JObject
|
|
||||||
result.fields = initTable[string, JsonNode](4)
|
|
||||||
for key, val in items(keyVals): result.fields[key] = val
|
for key, val in items(keyVals): result.fields[key] = val
|
||||||
|
|
||||||
proc `%`*(elements: openArray[JsonNode]): JsonNode =
|
template `%`*(j: JsonNode): JsonNode = j
|
||||||
|
|
||||||
|
proc `%`*[T](elements: openArray[T]): JsonNode =
|
||||||
## Generic constructor for JSON data. Creates a new `JArray JsonNode`
|
## Generic constructor for JSON data. Creates a new `JArray JsonNode`
|
||||||
new(result)
|
result = newJArray()
|
||||||
result.kind = JArray
|
for elem in elements: result.add(%elem)
|
||||||
newSeq(result.elems, elements.len)
|
|
||||||
for i, p in pairs(elements): result.elems[i] = p
|
proc `%`*(o: object): JsonNode =
|
||||||
|
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
||||||
|
result = newJObject()
|
||||||
|
for k, v in o.fieldPairs: result[k] = %v
|
||||||
|
|
||||||
|
proc `%`*(o: ref object): JsonNode =
|
||||||
|
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
||||||
|
if o.isNil:
|
||||||
|
result = newJNull()
|
||||||
|
else:
|
||||||
|
result = %(o[])
|
||||||
|
|
||||||
proc toJson(x: NimNode): NimNode {.compiletime.} =
|
proc toJson(x: NimNode): NimNode {.compiletime.} =
|
||||||
case x.kind
|
case x.kind
|
||||||
|
|
@ -736,6 +746,9 @@ proc toJson(x: NimNode): NimNode {.compiletime.} =
|
||||||
result = newNimNode(nnkTableConstr)
|
result = newNimNode(nnkTableConstr)
|
||||||
x.expectLen(0)
|
x.expectLen(0)
|
||||||
|
|
||||||
|
of nnkNilLit:
|
||||||
|
result = newCall("newJNull")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
result = x
|
result = x
|
||||||
|
|
||||||
|
|
@ -1325,10 +1338,26 @@ when isMainModule:
|
||||||
var j4 = %*{"test": nil}
|
var j4 = %*{"test": nil}
|
||||||
doAssert j4 == %{"test": newJNull()}
|
doAssert j4 == %{"test": newJNull()}
|
||||||
|
|
||||||
echo("99% of tests finished. Going to try loading file.")
|
let seqOfNodes = @[%1, %2]
|
||||||
|
let jSeqOfNodes = %seqOfNodes
|
||||||
|
doAssert(jSeqOfNodes[1].num == 2)
|
||||||
|
|
||||||
|
type MyObj = object
|
||||||
|
a, b: int
|
||||||
|
s: string
|
||||||
|
f32: float32
|
||||||
|
f64: float64
|
||||||
|
next: ref MyObj
|
||||||
|
var m: MyObj
|
||||||
|
m.s = "hi"
|
||||||
|
m.a = 5
|
||||||
|
let jMyObj = %m
|
||||||
|
doAssert(jMyObj["a"].num == 5)
|
||||||
|
doAssert(jMyObj["s"].str == "hi")
|
||||||
|
|
||||||
# Test loading of file.
|
# Test loading of file.
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
|
echo("99% of tests finished. Going to try loading file.")
|
||||||
var parsed = parseFile("tests/testdata/jsontest.json")
|
var parsed = parseFile("tests/testdata/jsontest.json")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,8 @@ Changes affecting backwards compatibility
|
||||||
by the language. With this change, ``alloc`` and ``dealloc`` are no longer
|
by the language. With this change, ``alloc`` and ``dealloc`` are no longer
|
||||||
aliases for ``malloc`` and ``free`` - use ``c_malloc`` and ``c_free`` if
|
aliases for ``malloc`` and ``free`` - use ``c_malloc`` and ``c_free`` if
|
||||||
you need that.
|
you need that.
|
||||||
|
- The ``json.%`` operator is now overloaded for ``object``, ``ref object`` and
|
||||||
|
``openarray[T]``.
|
||||||
|
|
||||||
|
|
||||||
Library Additions
|
Library Additions
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue