Skip to content

Commit 9031c06

Browse files
authored
Merge pull request #90 from Anuken/master
Fix deserialization of objects with default field values
2 parents 0ce3952 + 4899985 commit 9031c06

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/jsony.nim

+1
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ proc fromJson*[T](s: string, x: typedesc[T]): T =
599599
## * Missing json fields keep their default values.
600600
## * `proc newHook(foo: var ...)` Can be used to populate default values.
601601
var i = 0
602+
result = default(T)
602603
s.parseHook(i, result)
603604
eatSpace(s, i)
604605
if i != s.len:

tests/test_objects.nim

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ block:
77
var v = s.fromJson(Entry1)
88
doAssert v.color == ""
99

10+
when NimMajor >= 2: # Default field values are only supported in Nim 2.0+
11+
block:
12+
type Frog = object
13+
legs: int = 4
14+
15+
var s = "{}"
16+
var f = s.fromJson(Frog)
17+
# Make sure the default value is deserialized correctly.
18+
doAssert f.legs == 4
19+
1020
block:
1121
type Foo2 = ref object
1222
field: string

0 commit comments

Comments
 (0)