From c13353fa091f0754307f80d42819271c8789a2ad Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 23 Jan 2021 19:01:06 -0700 Subject: [PATCH 1/3] added enum indexed array support to json --- lib/pure/json.nim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index a9d0ed4cb08b..c3a67832e70c 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -1103,7 +1103,10 @@ when defined(nimFixedForwardGeneric): jsonPath.add '[' jsonPath.addInt i jsonPath.add ']' - initFromJson(dst[i], jsonNode[i], jsonPath) + when S is enum: # To support serializing enum indexed arrays + initFromJson(dst[i.S], jsonNode[i], jsonPath) + else: + initFromJson(dst[i], jsonNode[i], jsonPath) jsonPath.setLen originalJsonPathLen proc initFromJson[T](dst: var Table[string,T]; jsonNode: JsonNode; jsonPath: var string) = From d4834a9ea871211f3e4b23800a6e531da52b409a Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 23 Jan 2021 21:58:46 -0700 Subject: [PATCH 2/3] Added json test --- tests/stdlib/tjson_enum_index.nim | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/stdlib/tjson_enum_index.nim diff --git a/tests/stdlib/tjson_enum_index.nim b/tests/stdlib/tjson_enum_index.nim new file mode 100644 index 000000000000..2c24f4f36b2f --- /dev/null +++ b/tests/stdlib/tjson_enum_index.nim @@ -0,0 +1,11 @@ +import json +type Test = enum + one, two, three, four, five +let a = [ + one: 300, + two: 20, + three: 10, + four: 0, + five: -10 +] +doAssert (%* a).to(a.typeof) == a \ No newline at end of file From 560ef3a44f9c8f9ae0a0212baf5ae9567ee9238e Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 25 Jan 2021 14:46:04 -0700 Subject: [PATCH 3/3] Removed when statement for enum indexed arrays --- lib/pure/json.nim | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index c3a67832e70c..ac3c3b19430d 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -1103,10 +1103,7 @@ when defined(nimFixedForwardGeneric): jsonPath.add '[' jsonPath.addInt i jsonPath.add ']' - when S is enum: # To support serializing enum indexed arrays - initFromJson(dst[i.S], jsonNode[i], jsonPath) - else: - initFromJson(dst[i], jsonNode[i], jsonPath) + initFromJson(dst[i.S], jsonNode[i], jsonPath) # `.S` for enum indexed arrays jsonPath.setLen originalJsonPathLen proc initFromJson[T](dst: var Table[string,T]; jsonNode: JsonNode; jsonPath: var string) =