Skip to content

Commit

Permalink
1.0.5: Fix issue with null json in json.
Browse files Browse the repository at this point in the history
  • Loading branch information
treeform committed Sep 27, 2021
1 parent 4913f36 commit f5a2925
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion jsony.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "1.0.4"
version = "1.0.5"
author = "Andre von Houck"
description = "A loose direct to object json parser with hooks."
license = "MIT"
Expand Down
63 changes: 33 additions & 30 deletions src/jsony.nim
Original file line number Diff line number Diff line change
Expand Up @@ -768,37 +768,40 @@ proc dumpHook*[T](s: var string, v: HashSet[T]|OrderedSet[T]) =

proc dumpHook*(s: var string, v: JsonNode) =
## Dumps a regular json node.
case v.kind:
of JObject:
s.add '{'
var i = 0
for k, e in v.pairs:
if i != 0:
s.add ","
s.dumpHook(k)
s.add ':'
s.dumpHook(e)
inc i
s.add '}'
of JArray:
s.add '['
var i = 0
for e in v:
if i != 0:
s.add ","
s.dumpHook(e)
inc i
s.add ']'
of JNull:
if v == nil:
s.add "null"
of JInt:
s.dumpHook(v.getInt)
of JFloat:
s.dumpHook(v.getFloat)
of JString:
s.dumpHook(v.getStr)
of JBool:
s.dumpHook(v.getBool)
else:
case v.kind:
of JObject:
s.add '{'
var i = 0
for k, e in v.pairs:
if i != 0:
s.add ","
s.dumpHook(k)
s.add ':'
s.dumpHook(e)
inc i
s.add '}'
of JArray:
s.add '['
var i = 0
for e in v:
if i != 0:
s.add ","
s.dumpHook(e)
inc i
s.add ']'
of JNull:
s.add "null"
of JInt:
s.dumpHook(v.getInt)
of JFloat:
s.dumpHook(v.getFloat)
of JString:
s.dumpHook(v.getStr)
of JBool:
s.dumpHook(v.getBool)

proc toJson*[T](v: T): string =
dumpHook(result, v)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_json_in_json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ block:

let ns = """[123, +123, -123, 123.456, +123.456, -123.456, 123.456E9, +123.456E9, -123.456E9]"""
doAssert $ns.fromJson() == """[123,123,-123,123.456,123.456,-123.456,123456000000.0,123456000000.0,-123456000000.0]"""

var foo = Entry()
doAssert toJson(foo) == """{"name":"","data":null}"""

0 comments on commit f5a2925

Please sign in to comment.