Support int, string and bool fields in unmarshal json macro.
This commit is contained in:
parent
12aafb25cc
commit
cc223ff7d8
2 changed files with 55 additions and 17 deletions
|
|
@ -1442,6 +1442,24 @@ proc processType(typeName: NimNode, obj: NimNode,
|
||||||
assert `jsonNode`.kind == JFloat;
|
assert `jsonNode`.kind == JFloat;
|
||||||
`jsonNode`.fnum
|
`jsonNode`.fnum
|
||||||
)
|
)
|
||||||
|
of "string":
|
||||||
|
result = quote do:
|
||||||
|
(
|
||||||
|
assert `jsonNode`.kind in {JString, JNull};
|
||||||
|
if `jsonNode`.kind == JNull: nil else: `jsonNode`.str
|
||||||
|
)
|
||||||
|
of "int":
|
||||||
|
result = quote do:
|
||||||
|
(
|
||||||
|
assert `jsonNode`.kind == JInt;
|
||||||
|
`jsonNode`.num.int
|
||||||
|
)
|
||||||
|
of "bool":
|
||||||
|
result = quote do:
|
||||||
|
(
|
||||||
|
assert `jsonNode`.kind == JBool;
|
||||||
|
`jsonNode`.bval
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
assert false, "Unable to process nnkSym " & $typeName
|
assert false, "Unable to process nnkSym " & $typeName
|
||||||
else:
|
else:
|
||||||
|
|
@ -1620,6 +1638,7 @@ when false:
|
||||||
# To get that we shall use, obj["json"]
|
# To get that we shall use, obj["json"]
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
# Note: Macro tests are in tests/stdlib/tjsonmacro.nim
|
||||||
|
|
||||||
let testJson = parseJson"""{ "a": [1, 2, 3, 4], "b": "asd", "c": "\ud83c\udf83", "d": "\u00E6"}"""
|
let testJson = parseJson"""{ "a": [1, 2, 3, 4], "b": "asd", "c": "\ud83c\udf83", "d": "\u00E6"}"""
|
||||||
# nil passthrough
|
# nil passthrough
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
discard """
|
||||||
|
file: "tjsonmacro.nim"
|
||||||
|
output: ""
|
||||||
|
"""
|
||||||
import json, macros, strutils
|
import json, macros, strutils
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
@ -17,24 +21,39 @@ type
|
||||||
|
|
||||||
Replay* = ref object
|
Replay* = ref object
|
||||||
events*: seq[ReplayEvent]
|
events*: seq[ReplayEvent]
|
||||||
|
test: int
|
||||||
|
test2: string
|
||||||
|
test3: bool
|
||||||
|
testNil: string
|
||||||
|
|
||||||
var x = Replay(
|
when isMainModule:
|
||||||
events: @[
|
# Tests inspired by own use case (with some additional tests).
|
||||||
ReplayEvent(
|
# This should succeed.
|
||||||
time: 1.2345,
|
var x = Replay(
|
||||||
kind: FoodEaten,
|
events: @[
|
||||||
foodPos: Point[float](x: 5.0, y: 1.0)
|
ReplayEvent(
|
||||||
)
|
time: 1.2345,
|
||||||
]
|
kind: FoodEaten,
|
||||||
)
|
foodPos: Point[float](x: 5.0, y: 1.0)
|
||||||
|
)
|
||||||
|
],
|
||||||
|
test: 18827361,
|
||||||
|
test2: "hello world",
|
||||||
|
test3: true,
|
||||||
|
testNil: nil
|
||||||
|
)
|
||||||
|
|
||||||
let node = %x
|
let node = %x
|
||||||
|
|
||||||
echo(node)
|
let y = to(node, Replay)
|
||||||
|
doAssert y.events[0].time == 1.2345
|
||||||
|
doAssert y.events[0].kind == FoodEaten
|
||||||
|
doAssert y.events[0].foodPos.x == 5.0
|
||||||
|
doAssert y.events[0].foodPos.y == 1.0
|
||||||
|
doAssert y.test == 18827361
|
||||||
|
doAssert y.test2 == "hello world"
|
||||||
|
doAssert y.test3
|
||||||
|
doAssert y.testNil == nil
|
||||||
|
|
||||||
let y = to(node, Replay)
|
# Tests that verify the error messages for invalid data.
|
||||||
doAssert y.events[0].time == 1.2345
|
# TODO:
|
||||||
doAssert y.events[0].kind == FoodEaten
|
|
||||||
doAssert y.events[0].foodPos.x == 5.0
|
|
||||||
doAssert y.events[0].foodPos.y == 1.0
|
|
||||||
echo(y.repr)
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue