summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-04-11 12:38:30 +0800
committerGitHub <noreply@github.com>2023-04-11 06:38:30 +0200
commit5e016e4466151ddb86a72a46cc2bbf83dfd6dfe3 (patch)
tree07689f1e4e9de6bf8f24c1dd6ca210ce6341bcf8 /lib
parent75205fee9389747378d3b75d1a8e570056ffb260 (diff)
downloadNim-5e016e4466151ddb86a72a46cc2bbf83dfd6dfe3.tar.gz
fixes #21638; `fromJson` should support empty objects (#21641)
* fixes #21638; `fromJson` should supports empty objects

* complete the logic
Diffstat (limited to 'lib')
-rw-r--r--lib/std/jsonutils.nim27
1 files changed, 16 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