diff options
-rw-r--r-- | lib/std/jsonutils.nim | 27 | ||||
-rw-r--r-- | tests/stdlib/tjsonutils.nim | 5 |
2 files changed, 21 insertions, 11 deletions
diff --git a/lib/std/jsonutils.nim b/lib/std/jsonutils.nim index 17d08e02e..71f438905 100644 --- a/lib/std/jsonutils.nim +++ b/lib/std/jsonutils.nim @@ -78,19 +78,24 @@ macro getDiscriminants(a: typedesc): seq[string] = let sym = a[1] let t = sym.getTypeImpl let t2 = t[2] - doAssert t2.kind == nnkRecList - result = newTree(nnkBracket) - for ti in t2: - if ti.kind == nnkRecCase: - let key = ti[0][0] - let typ = ti[0][1] - result.add newLit key.strVal - if result.len > 0: + case t2.kind + of nnkEmpty: # allow empty objects result = quote do: - @`result` + seq[string].default + of nnkRecList: + result = newTree(nnkBracket) + for ti in t2: + if ti.kind == nnkRecCase: + let key = ti[0][0] + result.add newLit key.strVal + if result.len > 0: + result = quote do: + @`result` + else: + result = quote do: + seq[string].default else: - result = quote do: - seq[string].default + doAssert false, "unexpected kind: " & $t2.kind macro initCaseObject(T: typedesc, fun: untyped): untyped = ## does the minimum to construct a valid case object, only initializing diff --git a/tests/stdlib/tjsonutils.nim b/tests/stdlib/tjsonutils.nim index 1681f4a09..e81470091 100644 --- a/tests/stdlib/tjsonutils.nim +++ b/tests/stdlib/tjsonutils.nim @@ -438,6 +438,11 @@ template fn() = let json = inner.toJson(ToJsonOptions(enumMode: joptEnumSymbol)) doAssert $json == """{"x":"hello","y":"A"}""" + block: # bug #21638 + type Something = object + + doAssert "{}".parseJson.jsonTo(Something) == Something() + when false: ## TODO: Implement support for nested variant objects allowing the tests ## bellow to pass. |