Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/std/jsonutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ macro initCaseObject(T: typedesc, fun: untyped): untyped =
else: raiseAssert $t.kind # xxx `nnkPtrTy` could be handled too
doAssert t2.kind == nnkRecList
result = newTree(nnkObjConstr)
result.add sym
result.add T
for ti in t2:
if ti.kind == nnkRecCase:
let key = ti[0][0]
Expand Down
13 changes: 13 additions & 0 deletions tests/stdlib/tjsonutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ template fn() =
# sanity check: nesting inside a tuple
testRoundtrip((Foo[int](x0: 1.5, t1: false, z2: 6), "foo")): """[{"x0":1.5,"t1":false,"z2":6,"x1":""},"foo"]"""

block: # generic case object using generic type
type Foo[T] = ref object
x0: float
case t1: bool
of true: z1: int8
of false: z2: uint16
x1: string
x2: T
testRoundtrip(Foo[float](t1: true, z1: 5, x1: "bar", x2: 2.5)): """{"x0":0.0,"t1":true,"z1":5,"x1":"bar","x2":2.5}"""
testRoundtrip(Foo[int](x0: 1.5, t1: false, z2: 6, x2: 2)): """{"x0":1.5,"t1":false,"z2":6,"x1":"","x2":2}"""
# sanity check: nesting inside a tuple
testRoundtrip((Foo[int](x0: 1.5, t1: false, z2: 6, x2: 2), "foo")): """[{"x0":1.5,"t1":false,"z2":6,"x1":"","x2":2},"foo"]"""

block: # case object: 2 discriminants, `when` branch, range discriminant
type Foo[T] = object
case t1: bool
Expand Down