Improve JSON serialisation of strtabs (#14549)

This creates a more compact serialisation of strtabs that is more in
line with the normal tables.
This commit is contained in:
PMunch 2020-06-05 10:54:38 +02:00 • committed by GitHub
commit 7cb4ef26ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View file

@ -149,7 +149,7 @@ runnableExamples:
doAssert $(%* Foo()) == """{"a1":0,"a2":0,"a0":0,"a3":0,"a4":0}""" doAssert $(%* Foo()) == """{"a1":0,"a2":0,"a0":0,"a3":0,"a4":0}"""
import import
hashes, tables, strutils, lexbase, streams, macros, parsejson, hashes, tables, strtabs, strutils, lexbase, streams, macros, parsejson,
options options
export export
@ -353,6 +353,14 @@ proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} =
assert(obj.kind == JObject) assert(obj.kind == JObject)
obj.fields[key] = val 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 = proc `%`*[T: object](o: T): JsonNode =
## Construct JsonNode from tuples and objects. ## Construct JsonNode from tuples and objects.
result = newJObject() result = newJObject()
@ -981,11 +989,12 @@ when defined(nimFixedForwardGeneric):
proc initFromJson[T: enum](dst: var T; jsonNode: JsonNode; jsonPath: var string) 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[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[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 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 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 ref T; jsonNode: JsonNode; jsonPath: var string)
proc initFromJson[T](dst: var Option[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) proc initFromJson[T: object|tuple](dst: var T; jsonNode: JsonNode; jsonPath: var string)
# initFromJson definitions # initFromJson definitions
@ -1043,7 +1052,7 @@ when defined(nimFixedForwardGeneric):
initFromJson(dst[i], jsonNode[i], jsonPath) initFromJson(dst[i], jsonNode[i], jsonPath)
jsonPath.setLen originalJsonPathLen 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]() dst = initTable[string, T]()
verifyJsonKind(jsonNode, {JObject}, jsonPath) verifyJsonKind(jsonNode, {JObject}, jsonPath)
let originalJsonPathLen = jsonPath.len let originalJsonPathLen = jsonPath.len
@ -1053,7 +1062,7 @@ when defined(nimFixedForwardGeneric):
initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath) initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath)
jsonPath.setLen originalJsonPathLen 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]() dst = initOrderedTable[string,T]()
verifyJsonKind(jsonNode, {JObject}, jsonPath) verifyJsonKind(jsonNode, {JObject}, jsonPath)
let originalJsonPathLen = jsonPath.len let originalJsonPathLen = jsonPath.len
@ -1063,6 +1072,20 @@ when defined(nimFixedForwardGeneric):
initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath) initFromJson(mgetOrPut(dst, key, default(T)), jsonNode[key], jsonPath)
jsonPath.setLen originalJsonPathLen 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) = proc initFromJson[T](dst: var ref T; jsonNode: JsonNode; jsonPath: var string) =
verifyJsonKind(jsonNode, {JObject, JNull}, jsonPath) verifyJsonKind(jsonNode, {JObject, JNull}, jsonPath)
if jsonNode.kind == JNull: if jsonNode.kind == JNull:

View file

@ -71,7 +71,7 @@ type
StringTableObj* = object of RootObj StringTableObj* = object of RootObj
counter: int counter: int
data: KeyValuePairSeq data: KeyValuePairSeq
mode: StringTableMode mode*: StringTableMode
StringTableRef* = ref StringTableObj StringTableRef* = ref StringTableObj