Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
inv2004 committed Mar 17, 2023
1 parent ea811be commit 8770d7b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
27 changes: 8 additions & 19 deletions src/jsony.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type
proc parseHook*[T](s: string, i: var int, v: var seq[T])
proc parseHook*[T: enum](s: string, i: var int, v: var T)
proc parseHook*[T: object|ref object](s: string, i: var int, v: var T)
proc parseHook*[T](s: string, i: var int, v: var SomeTable[string, T])
proc parseHook*[T, U](s: string, i: var int, v: var SomeTable[U, T])
proc parseHook*[T](s: string, i: var int, v: var (SomeSet[T]|set[T]))
proc parseHook*[T: tuple](s: string, i: var int, v: var T)
proc parseHook*[T: array](s: string, i: var int, v: var T)
Expand Down Expand Up @@ -482,7 +482,7 @@ proc parseHook*[T](s: string, i: var int, v: var Option[T]) =
parseHook(s, i, e)
v = some(e)

proc parseHook*[T](s: string, i: var int, v: var SomeTable[string, T]) =
proc parseHook*[T, U](s: string, i: var int, v: var SomeTable[U, T]) =
## Parse an object.
when compiles(new(v)):
new(v)
Expand All @@ -491,8 +491,12 @@ proc parseHook*[T](s: string, i: var int, v: var SomeTable[string, T]) =
eatSpace(s, i)
if i < s.len and s[i] == '}':
break
var key: string
var key: U
when U isnot string:
eatChar(s, i, '"')
parseHook(s, i, key)
when U isnot string:
eatChar(s, i, '"')
eatChar(s, i, ':')
var element: T
parseHook(s, i, element)
Expand Down Expand Up @@ -602,8 +606,6 @@ proc dumpHook*(s: var string, v: string)
proc dumpHook*(s: var string, v: char)
proc dumpHook*(s: var string, v: tuple)
proc dumpHook*(s: var string, v: enum)
type t[T] = tuple[a: string, b: T]
proc dumpHook*[N, T](s: var string, v: array[N, t[T]])
proc dumpHook*[N, T](s: var string, v: array[N, T])
proc dumpHook*[T](s: var string, v: seq[T])
proc dumpHook*(s: var string, v: object)
Expand Down Expand Up @@ -789,7 +791,7 @@ proc dumpHook*(s: var string, v: object) =
for k, e in v.pairs:
if i > 0:
s.add ','
s.dumpHook(k)
s.dumpHook($k)
s.add ':'
s.dumpHook(e)
inc i
Expand All @@ -803,19 +805,6 @@ proc dumpHook*(s: var string, v: object) =
inc i
s.add '}'

proc dumpHook*[N, T](s: var string, v: array[N, t[T]]) =
s.add '{'
var i = 0
# Normal objects.
for (k, e) in v:
if i > 0:
s.add ','
s.dumpHook(k)
s.add ':'
s.dumpHook(e)
inc i
s.add '}'

proc dumpHook*(s: var string, v: ref) =
if v == nil:
s.add "null"
Expand Down
13 changes: 13 additions & 0 deletions tests/test_arrays.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ block:
var s = "null"
var v = s.fromJson(ref array[2, string])
doAssert v == nil

block:
doAssert {"j": 10, "s": 20, "o": 100, "n": 5000}.toJson() ==
"""[["j",10],["s",20],["o",100],["n",5000]]"""

doAssert {"j": "a", "s": "b", "o": "c", "n": "d"}.toJson() ==
"""[["j","a"],["s","b"],["o","c"],["n","d"]]"""

doAssert [{"j": "a", "j": "b"}].toJson() ==
"""[[["j","a"],["j","b"]]]"""

doAssert {10: "a"}.toJson() ==
"""[[10,"a"]]"""
11 changes: 3 additions & 8 deletions tests/test_tables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ block:
doAssert v["c"].color == "blue"

block:
doAssert {"j": 10, "s": 20, "o": 100, "n": 5000}.toJson() ==
"""{"j":10,"s":20,"o":100,"n":5000}"""

doAssert {"j": "a", "s": "b", "o": "c", "n": "d"}.toJson() ==
"""{"j":"a","s":"b","o":"c","n":"d"}"""

doAssert [{"j": "a"}].toJson() ==
"""[{"j":"a"}]"""
let t = {10: "a", 100: "b"}.toTable
doAssert t.toJson() == """{"10":"a","100":"b"}"""
doAssert t.toJson().fromJson(typeof t) == t

0 comments on commit 8770d7b

Please sign in to comment.