First implementation of JSON unmarshal macro.

This commit is contained in:
Dominik Picheta
2017-04-08 20:55:32 +02:00
parent cdfcc12529
commit 12aafb25cc
2 changed files with 360 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
import json, macros, strutils
type
Point[T] = object
x, y: T
ReplayEventKind* = enum
FoodAppeared, FoodEaten, DirectionChanged
ReplayEvent* = object
time*: float
case kind*: ReplayEventKind
of FoodAppeared, FoodEaten:
foodPos*: Point[float]
of DirectionChanged:
playerPos*: float
Replay* = ref object
events*: seq[ReplayEvent]
var x = Replay(
events: @[
ReplayEvent(
time: 1.2345,
kind: FoodEaten,
foodPos: Point[float](x: 5.0, y: 1.0)
)
]
)
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
echo(y.repr)