1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
discard """
file: "tjsonmacro.nim"
output: ""
"""
import json, strutils
when isMainModule:
# Tests inspired by own use case (with some additional tests).
# This should succeed.
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]
test: int
test2: string
test3: bool
testNil: string
var x = Replay(
events: @[
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 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.isNil
# Test for custom object variants (without an enum) and with an else branch.
block:
type
TestVariant = object
name: string
case age: uint8
of 2:
preSchool: string
of 8:
primarySchool: string
else:
other: int
var node = %{
"name": %"Nim",
"age": %8,
"primarySchool": %"Sandtown"
}
var result = to(node, TestVariant)
doAssert result.age == 8
doAssert result.name == "Nim"
doAssert result.primarySchool == "Sandtown"
node = %{
"name": %"⚔️Foo☢️",
"age": %25,
"other": %98
}
result = to(node, TestVariant)
doAssert result.name == node["name"].getStr()
doAssert result.age == node["age"].getNum().uint8
doAssert result.other == node["other"].getNum()
# TODO: Test object variant with set in of branch.
# TODO: Should we support heterogenous arrays?
# Tests that verify the error messages for invalid data.
block:
type
Person = object
name: string
age: int
var node = %{
"name": %"Dominik"
}
try:
discard to(node, Person)
doAssert false
except KeyError as exc:
doAssert("age" in exc.msg)
except:
doAssert false
node["age"] = %false
try:
discard to(node, Person)
doAssert false
except JsonKindError as exc:
doAssert("age" in exc.msg)
except:
doAssert false
type
PersonAge = enum
Fifteen, Sixteen
PersonCase = object
name: string
case age: PersonAge
of Fifteen:
discard
of Sixteen:
id: string
try:
discard to(node, PersonCase)
doAssert false
except JsonKindError as exc:
doAssert("age" in exc.msg)
except:
doAssert false
# Test the example in json module.
block:
let jsonNode = parseJson("""
{
"person": {
"name": "Nimmer",
"age": 21
},
"list": [1, 2, 3, 4]
}
""")
type
Person = object
name: string
age: int
Data1 = object # TODO: Codegen bug when changed to ``Data``.
person: Person
list: seq[int]
var data = to(jsonNode, Data1)
doAssert data.person.name == "Nimmer"
doAssert data.person.age == 21
doAssert data.list == @[1, 2, 3, 4]
# Test non-variant enum fields.
block:
type
EnumType = enum
Foo, Bar
TestEnum = object
field: EnumType
var node = %{
"field": %"Bar"
}
var result = to(node, TestEnum)
doAssert result.field == Bar
# Test ref type in field.
block:
var jsonNode = parseJson("""
{
"person": {
"name": "Nimmer",
"age": 21
},
"list": [1, 2, 3, 4]
}
""")
type
Person = ref object
name: string
age: int
Data = object
person: Person
list: seq[int]
var data = to(jsonNode, Data)
doAssert data.person.name == "Nimmer"
doAssert data.person.age == 21
doAssert data.list == @[1, 2, 3, 4]
jsonNode = parseJson("""
{
"person": null,
"list": [1, 2, 3, 4]
}
""")
data = to(jsonNode, Data)
doAssert data.person.isNil
block:
type
FooBar = object
field: float
let x = parseJson("""{ "field": 5}""")
let data = to(x, FooBar)
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
|