Implement support for Option[T] in json.to macro. Fixes #5848.
This commit is contained in:
parent
d3c9b58c00
commit
8ca41ce637
2 changed files with 50 additions and 2 deletions
|
|
@ -2,7 +2,7 @@ discard """
|
|||
file: "tjsonmacro.nim"
|
||||
output: ""
|
||||
"""
|
||||
import json, strutils
|
||||
import json, strutils, options
|
||||
|
||||
when isMainModule:
|
||||
# Tests inspired by own use case (with some additional tests).
|
||||
|
|
@ -295,4 +295,25 @@ when isMainModule:
|
|||
let parsed = mulTest().to(Car)
|
||||
doAssert parsed.engine.name == "V8"
|
||||
|
||||
doAssert i == 1
|
||||
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"
|
||||
|
||||
# TODO: Table[T, Y] support.
|
||||
# TODO: JsonNode support
|
||||
Loading…
Add table
Add a link
Reference in a new issue