From 4fa689664f40ce59d4bff5372682a4bc2dd064f5 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Fri, 5 Jun 2020 18:49:37 -0700 Subject: [PATCH] Revert "Improve JSON serialisation of strtabs (#14549)" This reverts commit 7cb4ef26addb3bb5ce2405d8396df6fd41664dae. --- lib/pure/json.nim | 35 ++++++----------------------------- lib/pure/strtabs.nim | 2 +- 2 files changed, 7 insertions(+), 30 deletions(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 9d6f04687e169..b875af7dd4428 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -149,7 +149,7 @@ runnableExamples: doAssert $(%* Foo()) == """{"a1":0,"a2":0,"a0":0,"a3":0,"a4":0}""" import - hashes, tables, strtabs, strutils, lexbase, streams, macros, parsejson, + hashes, tables, strutils, lexbase, streams, macros, parsejson, options import std/private/since @@ -354,14 +354,6 @@ proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} = assert(obj.kind == JObject) obj.fields[key] = val -proc `%`*(table: StringTableRef): JsonNode = - ## Generic constructor for JSON data. Creates a new ``JObject JsonNode``. - result = newJObject() - result["mode"] = %($table.mode) - var data = newJObject() - for k, v in table: data[k] = %v - result["data"] = data - proc `%`*[T: object](o: T): JsonNode = ## Construct JsonNode from tuples and objects. result = newJObject() @@ -991,12 +983,11 @@ when defined(nimFixedForwardGeneric): proc initFromJson[T: enum](dst: var T; jsonNode: JsonNode; jsonPath: var string) proc initFromJson[T](dst: var seq[T]; jsonNode: JsonNode; jsonPath: var string) proc initFromJson[S,T](dst: var array[S,T]; jsonNode: JsonNode; jsonPath: var string) - proc initFromJson[T](dst: var Table[string,T]; jsonNode: JsonNode; jsonPath: var string) - proc initFromJson[T](dst: var OrderedTable[string,T]; jsonNode: JsonNode; jsonPath: var string) - proc initFromJson(dst: var StringTableRef; jsonNode: JsonNode; jsonPath: var string) + proc initFromJson[T](dst: var Table[string,T];jsonNode: JsonNode; jsonPath: var string) + proc initFromJson[T](dst: var OrderedTable[string,T];jsonNode: JsonNode; jsonPath: var string) proc initFromJson[T](dst: var ref T; jsonNode: JsonNode; jsonPath: var string) proc initFromJson[T](dst: var Option[T]; jsonNode: JsonNode; jsonPath: var string) - proc initFromJson[T: distinct](dst: var T; jsonNode: JsonNode; jsonPath: var string) + proc initFromJson[T: distinct](dst: var T;jsonNode: JsonNode; jsonPath: var string) proc initFromJson[T: object|tuple](dst: var T; jsonNode: JsonNode; jsonPath: var string) # initFromJson definitions @@ -1054,7 +1045,7 @@ when defined(nimFixedForwardGeneric): initFromJson(dst[i], jsonNode[i], jsonPath) jsonPath.setLen originalJsonPathLen - proc initFromJson[T](dst: var Table[string,T]; jsonNode: JsonNode; jsonPath: var string) = + proc initFromJson[T](dst: var Table[string,T];jsonNode: JsonNode; jsonPath: var string) = dst = initTable[string, T]() verifyJsonKind(jsonNode, {JObject}, jsonPath) let originalJsonPathLen = jsonPath.len @@ -1064,7 +1055,7 @@ when defined(nimFixedForwardGeneric): initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath) jsonPath.setLen originalJsonPathLen - proc initFromJson[T](dst: var OrderedTable[string,T]; jsonNode: JsonNode; jsonPath: var string) = + proc initFromJson[T](dst: var OrderedTable[string,T];jsonNode: JsonNode; jsonPath: var string) = dst = initOrderedTable[string,T]() verifyJsonKind(jsonNode, {JObject}, jsonPath) let originalJsonPathLen = jsonPath.len @@ -1074,20 +1065,6 @@ when defined(nimFixedForwardGeneric): initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath) jsonPath.setLen originalJsonPathLen - proc mgetOrPut(tab: var StringTableRef, key: string): var string = - if not tab.hasKey(key): tab[key] = "" - result = tab[key] - - proc initFromJson(dst: var StringTableRef; jsonNode: JsonNode; jsonPath: var string) = - dst = newStringTable(parseEnum[StringTableMode](jsonNode["mode"].getStr)) - verifyJsonKind(jsonNode, {JObject}, jsonPath) - let originalJsonPathLen = jsonPath.len - for key in keys(jsonNode["data"].fields): - jsonPath.add '.' - jsonPath.add key - initFromJson(mgetOrPut(dst, key), jsonNode[key], jsonPath) - jsonPath.setLen originalJsonPathLen - proc initFromJson[T](dst: var ref T; jsonNode: JsonNode; jsonPath: var string) = verifyJsonKind(jsonNode, {JObject, JNull}, jsonPath) if jsonNode.kind == JNull: diff --git a/lib/pure/strtabs.nim b/lib/pure/strtabs.nim index 8ab3a1aa7b776..7e814a585c33e 100644 --- a/lib/pure/strtabs.nim +++ b/lib/pure/strtabs.nim @@ -71,7 +71,7 @@ type StringTableObj* = object of RootObj counter: int data: KeyValuePairSeq - mode*: StringTableMode + mode: StringTableMode StringTableRef* = ref StringTableObj