Merge branch 'devel' of github.com:nim-lang/Nim into devel
This commit is contained in:
commit
f9ddd00d3a
5 changed files with 315 additions and 29 deletions
|
|
@ -2,7 +2,7 @@ discard """
|
|||
file: "tjsonmacro.nim"
|
||||
output: ""
|
||||
"""
|
||||
import json, strutils
|
||||
import json, strutils, options, tables
|
||||
|
||||
when isMainModule:
|
||||
# Tests inspired by own use case (with some additional tests).
|
||||
|
|
@ -246,4 +246,138 @@ when isMainModule:
|
|||
var b = Bird(age: 3, height: 1.734, name: "bardo", colors: [red, blue])
|
||||
let jnode = %b
|
||||
let data = jnode.to(Bird)
|
||||
doAssert data == b
|
||||
doAssert data == b
|
||||
|
||||
block:
|
||||
type
|
||||
MsgBase = ref object of RootObj
|
||||
name*: string
|
||||
|
||||
MsgChallenge = ref object of MsgBase
|
||||
challenge*: string
|
||||
|
||||
let data = %*{"name": "foo", "challenge": "bar"}
|
||||
let msg = data.to(MsgChallenge)
|
||||
doAssert msg.name == "foo"
|
||||
doAssert msg.challenge == "bar"
|
||||
|
||||
block:
|
||||
type
|
||||
Color = enum Red, Brown
|
||||
Thing = object
|
||||
animal: tuple[fur: bool, legs: int]
|
||||
color: Color
|
||||
|
||||
var j = parseJson("""
|
||||
{"animal":{"fur":true,"legs":6},"color":"Red"}
|
||||
""")
|
||||
|
||||
let parsed = to(j, Thing)
|
||||
doAssert parsed.animal.fur
|
||||
doAssert parsed.animal.legs == 6
|
||||
doAssert parsed.color == Red
|
||||
|
||||
block:
|
||||
type
|
||||
Car = object
|
||||
engine: tuple[name: string, capacity: float]
|
||||
model: string
|
||||
|
||||
let j = """
|
||||
{"engine": {"name": "V8", "capacity": 5.5}, "model": "Skyline"}
|
||||
"""
|
||||
|
||||
var i = 0
|
||||
proc mulTest: JsonNode =
|
||||
i.inc()
|
||||
return parseJson(j)
|
||||
|
||||
let parsed = mulTest().to(Car)
|
||||
doAssert parsed.engine.name == "V8"
|
||||
|
||||
doAssert i == 1
|
||||
|
||||
block:
|
||||
# Option[T] support!
|
||||
type
|
||||
Car1 = object # TODO: Codegen bug when `Car`
|
||||
engine: tuple[name: string, capacity: Option[float]]
|
||||
model: string
|
||||
year: Option[int]
|
||||
|
||||
let noYear = """
|
||||
{"engine": {"name": "V8", "capacity": 5.5}, "model": "Skyline"}
|
||||
"""
|
||||
|
||||
let noYearParsed = parseJson(noYear)
|
||||
let noYearDeser = to(noYearParsed, Car1)
|
||||
doAssert noYearDeser.engine.capacity == some(5.5)
|
||||
doAssert noYearDeser.year.isNone
|
||||
doAssert noYearDeser.engine.name == "V8"
|
||||
|
||||
# Table[T, Y] support.
|
||||
block:
|
||||
type
|
||||
Friend = object
|
||||
name: string
|
||||
age: int
|
||||
|
||||
Dynamic = object
|
||||
name: string
|
||||
friends: Table[string, Friend]
|
||||
|
||||
let data = """
|
||||
{"friends": {
|
||||
"John": {"name": "John", "age": 35},
|
||||
"Elizabeth": {"name": "Elizabeth", "age": 23}
|
||||
}, "name": "Dominik"}
|
||||
"""
|
||||
|
||||
let dataParsed = parseJson(data)
|
||||
let dataDeser = to(dataParsed, Dynamic)
|
||||
doAssert dataDeser.name == "Dominik"
|
||||
doAssert dataDeser.friends["John"].age == 35
|
||||
doAssert dataDeser.friends["Elizabeth"].age == 23
|
||||
|
||||
# JsonNode support
|
||||
block:
|
||||
type
|
||||
Test = object
|
||||
name: string
|
||||
fallback: JsonNode
|
||||
|
||||
let data = """
|
||||
{"name": "FooBar", "fallback": 56.42}
|
||||
"""
|
||||
|
||||
let dataParsed = parseJson(data)
|
||||
let dataDeser = to(dataParsed, Test)
|
||||
doAssert dataDeser.name == "FooBar"
|
||||
doAssert dataDeser.fallback.kind == JFloat
|
||||
doAssert dataDeser.fallback.getFloat() == 56.42
|
||||
|
||||
# int64, float64 etc support.
|
||||
block:
|
||||
type
|
||||
Test1 = object
|
||||
a: int8
|
||||
b: int16
|
||||
c: int32
|
||||
d: int64
|
||||
e: uint8
|
||||
f: uint16
|
||||
g: uint32
|
||||
h: uint64
|
||||
i: float32
|
||||
j: float64
|
||||
|
||||
let data = """
|
||||
{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7,
|
||||
"h": 8, "i": 9.9, "j": 10.10}
|
||||
"""
|
||||
|
||||
let dataParsed = parseJson(data)
|
||||
let dataDeser = to(dataParsed, Test1)
|
||||
doAssert dataDeser.a == 1
|
||||
doAssert dataDeser.f == 6
|
||||
doAssert dataDeser.i == 9.9'f32
|
||||
18
tests/stdlib/tjsonmacro_reject.nim
Normal file
18
tests/stdlib/tjsonmacro_reject.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
discard """
|
||||
file: "tjsonmacro_reject.nim"
|
||||
line: 11
|
||||
errormsg: "Use a named tuple instead of: (string, float)"
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
type
|
||||
Car = object
|
||||
engine: (string, float)
|
||||
model: string
|
||||
|
||||
let j = """
|
||||
{"engine": {"name": "V8", "capacity": 5.5}, model: "Skyline"}
|
||||
"""
|
||||
let parsed = parseJson(j)
|
||||
echo(to(parsed, Car))
|
||||
21
tests/stdlib/tjsonmacro_reject2.nim
Normal file
21
tests/stdlib/tjsonmacro_reject2.nim
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
discard """
|
||||
file: "tjsonmacro_reject2.nim"
|
||||
line: 10
|
||||
errormsg: "The `to` macro does not support ref objects with cycles."
|
||||
"""
|
||||
import json
|
||||
|
||||
type
|
||||
Misdirection = object
|
||||
cycle: Cycle
|
||||
|
||||
Cycle = ref object
|
||||
foo: string
|
||||
cycle: Misdirection
|
||||
|
||||
let data = """
|
||||
{"cycle": null}
|
||||
"""
|
||||
|
||||
let dataParsed = parseJson(data)
|
||||
let dataDeser = to(dataParsed, Cycle)
|
||||
Loading…
Add table
Add a link
Reference in a new issue