summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-05-11 12:09:17 -0700
committerGitHub <noreply@github.com>2021-05-11 21:09:17 +0200
commita770c98e272109968203403009bfb617a5f2d33a (patch)
tree7cbf12cc404db0855fbf6ac8142771eebf20331a
parente60672141a971da878dac6781fb907b9c520c219 (diff)
downloadNim-a770c98e272109968203403009bfb617a5f2d33a.tar.gz
jsonutils: support set (#17994)
-rw-r--r--changelog.md8
-rw-r--r--lib/std/jsonutils.nim6
-rw-r--r--tests/stdlib/tjsonutils.nim6
3 files changed, 15 insertions, 5 deletions
diff --git a/changelog.md b/changelog.md
index 88ab2494e..b114f5988 100644
--- a/changelog.md
+++ b/changelog.md
@@ -110,16 +110,16 @@
 
 - Added `BackwardsIndex` overload for `JsonNode`.
 
-- added `jsonutils.jsonTo` overload with `opt = Joptions()` param.
-
 - `json.%`,`json.to`, `jsonutils.formJson`,`jsonutils.toJson` now work with `uint|uint64`
   instead of raising (as in 1.4) or giving wrong results (as in 1.2).
 
+- added `jsonutils.jsonTo` overload with `opt = Joptions()` param.
+
+- `jsonutils` now handles `cstring` (including as Table key), and `set`.
+
 - Added an overload for the `collect` macro that inferes the container type based
   on the syntax of the last expression. Works with std seqs, tables and sets.
 
-- `jsonutils` now handles `cstring` (including as Table key).
-
 - Added `randState` template that exposes the default random number generator.
   Useful for library authors.
 
diff --git a/lib/std/jsonutils.nim b/lib/std/jsonutils.nim
index 4c27bc9f2..59c3b0f7d 100644
--- a/lib/std/jsonutils.nim
+++ b/lib/std/jsonutils.nim
@@ -214,6 +214,10 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
     for ai in mitems(a):
       fromJson(ai, b[i], opt)
       i.inc
+  elif T is set:
+    type E = typeof(for ai in a: ai)
+    for val in b.getElems:
+      incl a, jsonTo(val, E)
   elif T is seq:
     a.setLen b.len
     for i, val in b.getElems:
@@ -268,7 +272,7 @@ proc toJson*[T](a: T): JsonNode =
   elif T is ref | ptr:
     if system.`==`(a, nil): result = newJNull()
     else: result = toJson(a[])
-  elif T is array | seq:
+  elif T is array | seq | set:
     result = newJArray()
     for ai in a: result.add toJson(ai)
   elif T is pointer: result = toJson(cast[int](a))
diff --git a/tests/stdlib/tjsonutils.nim b/tests/stdlib/tjsonutils.nim
index eaf5d68f9..c17c486b9 100644
--- a/tests/stdlib/tjsonutils.nim
+++ b/tests/stdlib/tjsonutils.nim
@@ -70,6 +70,12 @@ template fn() =
     doAssert b2.ord == 1 # explains the `1`
     testRoundtrip(a): """[1,2,3]"""
 
+  block: # set
+    type Foo = enum f1, f2, f3, f4, f5
+    # type Goo = enum g1 = 10, g2 = 15, g3 = 17, g4 # in future PR, elements for holey enum should be treated as enum, not string
+    let a = ({f1, f3}, {1'u8, 7'u8}, {'0'..'9'}, {123'u16, 456, 789, 1121, 1122, 1542})
+    testRoundtrip(a): """[[0,2],[1,7],[48,49,50,51,52,53,54,55,56,57],[123,456,789,1121,1122,1542]]"""
+
   block: # bug #17383
     block:
       let a = (int32.high, uint32.high)