Skip to content

Commit

Permalink
also test for vm
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jun 7, 2020
1 parent 6ec9a58 commit 5a6f594
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
7 changes: 6 additions & 1 deletion lib/std/jsonutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ proc fromJson*[T](a: var T, b: JsonNode) =
else: checkJson false, $($T, " ", b)
elif T is Ordinal: a = T(to(b, int))
elif T is pointer: a = cast[pointer](to(b, int))
elif T is distinct: a.distinctBase.fromJson(b)
elif T is distinct:
when nimvm:
# bug, potentially related to https://github.com/nim-lang/Nim/issues/12282
a = T(jsonTo(b, distinctBase(T)))
else:
a.distinctBase.fromJson(b)
elif T is string|SomeNumber: a = to(b,T)
elif T is JsonNode: a = b
elif T is ref | ptr:
Expand Down
35 changes: 21 additions & 14 deletions tests/stdlib/tjsonutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,35 @@ proc testRoundtrip[T](t: T, expected: string) =
doAssert $j == expected, $j
doAssert j.jsonTo(T).toJson == j

block: # toJson, jsonTo
type Foo = distinct float
testRoundtrip('x', """120""")
when not defined(js):
testRoundtrip(cast[pointer](12345)): """12345"""
testRoundtrip(Foo(1.5)): """1.5"""
import tables
import strtabs

template fn() =
block: # toJson, jsonTo
type Foo = distinct float
testRoundtrip('x', """120""")
when not defined(js):
testRoundtrip(cast[pointer](12345)): """12345"""

import tables
block:
testRoundtrip({"z": "Z", "y": "Y"}.toOrderedTable): """{"z":"Z","y":"Y"}"""
when not defined(js): # pending https://github.com/nim-lang/Nim/issues/14574
testRoundtrip({"z": (f1: 'f'), }.toTable): """{"z":{"f1":102}}"""
# causes workaround in `fromJson` potentially related to
# https://github.com/nim-lang/Nim/issues/12282
testRoundtrip(Foo(1.5)): """1.5"""

import strtabs
block:
testRoundtrip({"z": "Z", "y": "Y"}.toOrderedTable): """{"z":"Z","y":"Y"}"""
when not defined(js): # pending https://github.com/nim-lang/Nim/issues/14574
testRoundtrip({"z": (f1: 'f'), }.toTable): """{"z":{"f1":102}}"""

block:
testRoundtrip({"name": "John", "city": "Monaco"}.newStringTable): """{"mode":"modeCaseSensitive","table":{"city":"Monaco","name":"John"}}"""

block:
testRoundtrip({"name": "John", "city": "Monaco"}.newStringTable): """{"mode":"modeCaseSensitive","table":{"city":"Monaco","name":"John"}}"""
block: # complex example
let t = {"z": "Z", "y": "Y"}.newStringTable
type A = ref object
a1: string
let a = (1.1, "fo", 'x', @[10,11], [true, false], [t,newStringTable()], [0'i8,3'i8], -4'i16, (foo: 0.5'f32, bar: A(a1: "abc"), bar2: A.default))
testRoundtrip(a):
"""[1.1,"fo",120,[10,11],[true,false],[{"mode":"modeCaseSensitive","table":{"y":"Y","z":"Z"}},{"mode":"modeCaseSensitive","table":{}}],[0,3],-4,{"foo":0.5,"bar":{"a1":"abc"},"bar2":null}]"""

static: fn()
fn()

0 comments on commit 5a6f594

Please sign in to comment.