Skip to content

Commit efa5977

Browse files
authored
Merge pull request #43 from SolitudeSF/discriminant
Don't require discriminant field to be present in object variants
2 parents c7de3dd + 36596f6 commit efa5977

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/jsony.nim

+9-4
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ proc parseHook*[T: object|ref object](s: string, i: var int, v: var T) =
405405
elif compiles(new(v)):
406406
new(v)
407407
else:
408-
# Look for the discriminatorFieldName, then parse as normal object.
408+
# Try looking for the discriminatorFieldName, then parse as normal object.
409409
eatSpace(s, i)
410410
var saveI = i
411411
while i < s.len:
@@ -422,9 +422,14 @@ proc parseHook*[T: object|ref object](s: string, i: var int, v: var T) =
422422
newHook(v)
423423
break
424424
skipValue(s, i)
425-
if i < s.len and s[i] == '}':
426-
error("No discriminator field.", i)
427-
eatChar(s, i, ',')
425+
if i < s.len and s[i] != '}':
426+
eatChar(s, i, ',')
427+
else:
428+
when compiles(newHook(v)):
429+
newHook(v)
430+
elif compiles(new(v)):
431+
new(v)
432+
break
428433
i = saveI
429434
parseObjectInner(s, i, v)
430435
eatChar(s, i, '}')

tests/test_objects.nim

+11-7
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ block:
201201

202202
block:
203203
# Test discriminator field name not being there.
204-
doAssertRaises JsonError:
205-
let
206-
a = """{"active":true,"floatVal":3.14}""".fromJson(RefNode)
204+
let
205+
a = """{"active":true,"intVal":42}""".fromJson(RefNode)
206+
doAssert a.kind == nkInt
207207

208208
block:
209209
var nodeNum = ValueNode(kind: nkFloat, active: true, floatVal: 3.14)
@@ -225,9 +225,9 @@ block:
225225

226226
block:
227227
# Test discriminator field name not being there.
228-
doAssertRaises JsonError:
229-
let
230-
a = """{"active":true,"floatVal":3.14}""".fromJson(ValueNode)
228+
let
229+
a = """{"active":true,"intVal":42}""".fromJson(ValueNode)
230+
doAssert a.kind == nkInt
231231

232232
type
233233
NodeNumKind = enum # the different node types
@@ -249,24 +249,28 @@ proc renameHook*(v: var RefNode|ValueNode, fieldName: var string) =
249249
if fieldName == "type":
250250
fieldName = "kind"
251251

252-
# Test renameHook and discriminator Field Name not being first.
252+
# Test renameHook and discriminator Field Name not being first/missing.
253253
block:
254254
let
255255
a = """{"active":true,"type":"nkFloat","floatVal":3.14}""".fromJson(RefNode)
256256
b = """{"floatVal":3.14,"active":true,"type":"nkFloat"}""".fromJson(RefNode)
257257
c = """{"type":"nkFloat","floatVal":3.14,"active":true}""".fromJson(RefNode)
258+
d = """{"active":true,"intVal":42}""".fromJson(RefNode)
258259
doAssert a.kind == nkFloat
259260
doAssert b.kind == nkFloat
260261
doAssert c.kind == nkFloat
262+
doAssert d.kind == nkInt
261263

262264
block:
263265
let
264266
a = """{"active":true,"type":"nkFloat","floatVal":3.14}""".fromJson(ValueNode)
265267
b = """{"floatVal":3.14,"active":true,"type":"nkFloat"}""".fromJson(ValueNode)
266268
c = """{"type":"nkFloat","floatVal":3.14,"active":true}""".fromJson(ValueNode)
269+
d = """{"active":true,"intVal":42}""".fromJson(ValueNode)
267270
doAssert a.kind == nkFloat
268271
doAssert b.kind == nkFloat
269272
doAssert c.kind == nkFloat
273+
doAssert d.kind == nkInt
270274

271275

272276
# test https://forum.nim-lang.org/t/7619

0 commit comments

Comments
 (0)