summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/json.nim35
-rw-r--r--lib/pure/strtabs.nim2
2 files changed, 30 insertions, 7 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim
index ab03370b5..04e169d30 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, strutils, lexbase, streams, macros, parsejson,
+  hashes, tables, strtabs, strutils, lexbase, streams, macros, parsejson,
   options
 
 export
@@ -353,6 +353,14 @@ 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()
@@ -981,11 +989,12 @@ 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[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 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
@@ -1043,7 +1052,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
@@ -1053,7 +1062,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
@@ -1063,6 +1072,20 @@ 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 26811cc65..cfafdf018 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