Skip to content

Commit

Permalink
Merge pull request #90 from Anuken/master
Browse files Browse the repository at this point in the history
Fix deserialization of objects with default field values
  • Loading branch information
treeform committed Jun 18, 2024
2 parents 0ce3952 + 4899985 commit 9031c06
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/jsony.nim
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ proc fromJson*[T](s: string, x: typedesc[T]): T =
## * Missing json fields keep their default values.
## * `proc newHook(foo: var ...)` Can be used to populate default values.
var i = 0
result = default(T)
s.parseHook(i, result)
eatSpace(s, i)
if i != s.len:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_objects.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ block:
var v = s.fromJson(Entry1)
doAssert v.color == ""

when NimMajor >= 2: # Default field values are only supported in Nim 2.0+
block:
type Frog = object
legs: int = 4

var s = "{}"
var f = s.fromJson(Frog)
# Make sure the default value is deserialized correctly.
doAssert f.legs == 4

block:
type Foo2 = ref object
field: string
Expand Down

0 comments on commit 9031c06

Please sign in to comment.