@@ -164,37 +164,67 @@ var sizer = """{"size":10}""".fromJson(Sizer)
164164doAssert sizer.size == 10
165165doAssert sizer.originalSize == 10
166166
167- # block:
168-
169- # type
170- # NodeNumKind = enum # the different node types
171- # nkInt, # a leaf with an integer value
172- # nkFloat, # a leaf with a float value
173- # RefNode = ref object
174- # active: bool
175- # case kind: NodeNumKind # the ``kind`` field is the discriminator
176- # of nkInt: intVal: int
177- # of nkFloat: floatVal: float
178- # ValueNode = object
179- # active: bool
180- # case kind: NodeNumKind # the ``kind`` field is the discriminator
181- # of nkInt: intVal: int
182- # of nkFloat: floatVal: float
183-
184- # block:
185- # var nodeNum = RefNode(kind: nkFloat, active: true, floatVal: 3.14)
186- # var nodeNum2 = RefNode(kind: nkInt, active: false, intVal: 42)
187-
188- # doAssert nodeNum.toJson.fromJson(type(nodeNum)).floatVal == nodeNum.floatVal
189- # doAssert nodeNum2.toJson.fromJson(type(nodeNum2)).intVal == nodeNum2.intVal
190- # doAssert nodeNum.toJson.fromJson(type(nodeNum)).active == nodeNum.active
191- # doAssert nodeNum2.toJson.fromJson(type(nodeNum2)).active == nodeNum2.active
192-
193- # block:
194- # var nodeNum = ValueNode(kind: nkFloat, active: true, floatVal: 3.14)
195- # var nodeNum2 = ValueNode(kind: nkInt, active: false, intVal: 42)
196-
197- # doAssert nodeNum.toJson.fromJson(type(nodeNum)).floatVal == nodeNum.floatVal
198- # doAssert nodeNum2.toJson.fromJson(type(nodeNum2)).intVal == nodeNum2.intVal
199- # doAssert nodeNum.toJson.fromJson(type(nodeNum)).active == nodeNum.active
200- # doAssert nodeNum2.toJson.fromJson(type(nodeNum2)).active == nodeNum2.active
167+ block :
168+
169+ type
170+ NodeNumKind = enum # the different node types
171+ nkInt, # a leaf with an integer value
172+ nkFloat, # a leaf with a float value
173+ RefNode = ref object
174+ active: bool
175+ case kind: NodeNumKind # the ``kind`` field is the discriminator
176+ of nkInt: intVal: int
177+ of nkFloat: floatVal: float
178+ ValueNode = object
179+ active: bool
180+ case kind: NodeNumKind # the ``kind`` field is the discriminator
181+ of nkInt: intVal: int
182+ of nkFloat: floatVal: float
183+
184+ block :
185+ var nodeNum = RefNode (kind: nkFloat, active: true , floatVal: 3.14 )
186+ var nodeNum2 = RefNode (kind: nkInt, active: false , intVal: 42 )
187+ doAssert nodeNum.toJson.fromJson (type (nodeNum)).floatVal == nodeNum.floatVal
188+ doAssert nodeNum2.toJson.fromJson (type (nodeNum2)).intVal == nodeNum2.intVal
189+ doAssert nodeNum.toJson.fromJson (type (nodeNum)).active == nodeNum.active
190+ doAssert nodeNum2.toJson.fromJson (type (nodeNum2)).active == nodeNum2.active
191+
192+ block :
193+ # Test discriminator Field Name not being first.
194+ let
195+ a = """ {"active":true,"kind":"nkFloat","floatVal":3.14} """ .fromJson (RefNode )
196+ b = """ {"floatVal":3.14,"active":true,"kind":"nkFloat"} """ .fromJson (RefNode )
197+ c = """ {"kind":"nkFloat","floatVal":3.14,"active":true} """ .fromJson (RefNode )
198+ doAssert a.kind == nkFloat
199+ doAssert b.kind == nkFloat
200+ doAssert c.kind == nkFloat
201+
202+ block :
203+ # Test discriminator field name not being there.
204+ doAssertRaises JsonError :
205+ let
206+ a = """ {"active":true,"floatVal":3.14} """ .fromJson (RefNode )
207+
208+ block :
209+ var nodeNum = ValueNode (kind: nkFloat, active: true , floatVal: 3.14 )
210+ var nodeNum2 = ValueNode (kind: nkInt, active: false , intVal: 42 )
211+ doAssert nodeNum.toJson.fromJson (type (nodeNum)).floatVal == nodeNum.floatVal
212+ doAssert nodeNum2.toJson.fromJson (type (nodeNum2)).intVal == nodeNum2.intVal
213+ doAssert nodeNum.toJson.fromJson (type (nodeNum)).active == nodeNum.active
214+ doAssert nodeNum2.toJson.fromJson (type (nodeNum2)).active == nodeNum2.active
215+
216+ block :
217+ # Test discriminator Field Name not being first.
218+ let
219+ a = """ {"active":true,"kind":"nkFloat","floatVal":3.14} """ .fromJson (ValueNode )
220+ b = """ {"floatVal":3.14,"active":true,"kind":"nkFloat"} """ .fromJson (ValueNode )
221+ c = """ {"kind":"nkFloat","floatVal":3.14,"active":true} """ .fromJson (ValueNode )
222+ doAssert a.kind == nkFloat
223+ doAssert b.kind == nkFloat
224+ doAssert c.kind == nkFloat
225+
226+ block :
227+ # Test discriminator field name not being there.
228+ doAssertRaises JsonError :
229+ let
230+ a = """ {"active":true,"floatVal":3.14} """ .fromJson (ValueNode )
0 commit comments