Fixes #5761.
This commit is contained in:
parent
427aa2d395
commit
d1daccac2f
2 changed files with 38 additions and 5 deletions
|
|
@ -1608,7 +1608,7 @@ proc createConstructor(typeSym, jsonNode: NimNode): NimNode =
|
||||||
result = processType(newIdentNode(typeName), obj, jsonNode, true)
|
result = processType(newIdentNode(typeName), obj, jsonNode, true)
|
||||||
of "seq":
|
of "seq":
|
||||||
let seqT = typeSym[1]
|
let seqT = typeSym[1]
|
||||||
let forLoopI = newIdentNode("i")
|
let forLoopI = genSym(nskForVar, "i")
|
||||||
let indexerNode = createJsonIndexer(jsonNode, forLoopI)
|
let indexerNode = createJsonIndexer(jsonNode, forLoopI)
|
||||||
let constructorNode = createConstructor(seqT, indexerNode)
|
let constructorNode = createConstructor(seqT, indexerNode)
|
||||||
|
|
||||||
|
|
@ -1616,12 +1616,25 @@ proc createConstructor(typeSym, jsonNode: NimNode): NimNode =
|
||||||
result = quote do:
|
result = quote do:
|
||||||
(
|
(
|
||||||
var list: `typeSym` = @[];
|
var list: `typeSym` = @[];
|
||||||
# if `jsonNode`.kind != JArray:
|
verifyJsonKind(`jsonNode`, {JArray}, astToStr(`jsonNode`));
|
||||||
# # TODO: Improve error message.
|
|
||||||
# raise newException(ValueError, "Expected a list")
|
|
||||||
for `forLoopI` in 0 .. <`jsonNode`.len: list.add(`constructorNode`);
|
for `forLoopI` in 0 .. <`jsonNode`.len: list.add(`constructorNode`);
|
||||||
list
|
list
|
||||||
)
|
)
|
||||||
|
of "array":
|
||||||
|
let arrayT = typeSym[2]
|
||||||
|
let forLoopI = genSym(nskForVar, "i")
|
||||||
|
let indexerNode = createJsonIndexer(jsonNode, forLoopI)
|
||||||
|
let constructorNode = createConstructor(arrayT, indexerNode)
|
||||||
|
|
||||||
|
# Create a statement expression containing a for loop.
|
||||||
|
result = quote do:
|
||||||
|
(
|
||||||
|
var list: `typeSym`;
|
||||||
|
verifyJsonKind(`jsonNode`, {JArray}, astToStr(`jsonNode`));
|
||||||
|
for `forLoopI` in 0 .. <`jsonNode`.len: list[`forLoopI`] =`constructorNode`;
|
||||||
|
list
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Generic type.
|
# Generic type.
|
||||||
let obj = getType(typeSym)
|
let obj = getType(typeSym)
|
||||||
|
|
|
||||||
|
|
@ -226,4 +226,24 @@ when isMainModule:
|
||||||
|
|
||||||
let x = parseJson("""{ "field": 5}""")
|
let x = parseJson("""{ "field": 5}""")
|
||||||
let data = to(x, FooBar)
|
let data = to(x, FooBar)
|
||||||
doAssert data.field == 5.0
|
doAssert data.field == 5.0
|
||||||
|
|
||||||
|
block:
|
||||||
|
type
|
||||||
|
BirdColor = object
|
||||||
|
name: string
|
||||||
|
rgb: array[3, float]
|
||||||
|
|
||||||
|
type
|
||||||
|
Bird = object
|
||||||
|
age: int
|
||||||
|
height: float
|
||||||
|
name: string
|
||||||
|
colors: array[2, BirdColor]
|
||||||
|
|
||||||
|
var red = BirdColor(name: "red", rgb: [1.0, 0.0, 0.0])
|
||||||
|
var blue = Birdcolor(name: "blue", rgb: [0.0, 0.0, 1.0])
|
||||||
|
var b = Bird(age: 3, height: 1.734, name: "bardo", colors: [red, blue])
|
||||||
|
let jnode = %b
|
||||||
|
let data = jnode.to(Bird)
|
||||||
|
doAssert data == b
|
||||||
Loading…
Add table
Add a link
Reference in a new issue