toJson, jsonTo, json (de)serialization for custom types; remove dependency on strtabs thanks to a hooking mechanism (#14563)
* json custom serialization; application for strtabs
* serialize using nesting
* make toJson more feature complete
* add since
* Revert "Improve JSON serialisation of strtabs (#14549)"
This reverts commit 7cb4ef26ad.
* better approach via mixin
* toJson, jsonTo
* fix test
* address comments
* move to jsonutils
* doc
* cleanups
* also test for js
* also test for vm
This commit is contained in:
parent
733bd76f6b
commit
c7a1a7b8bf
5 changed files with 203 additions and 31 deletions
|
|
@ -71,7 +71,7 @@ type
|
|||
StringTableObj* = object of RootObj
|
||||
counter: int
|
||||
data: KeyValuePairSeq
|
||||
mode*: StringTableMode
|
||||
mode: StringTableMode
|
||||
|
||||
StringTableRef* = ref StringTableObj
|
||||
|
||||
|
|
@ -419,6 +419,24 @@ proc `%`*(f: string, t: StringTableRef, flags: set[FormatFlag] = {}): string {.
|
|||
add(result, f[i])
|
||||
inc(i)
|
||||
|
||||
since (1,3,5):
|
||||
proc fromJsonHook*[T](a: var StringTableRef, b: T) =
|
||||
## for json.fromJson
|
||||
mixin jsonTo
|
||||
var mode = jsonTo(b["mode"], StringTableMode)
|
||||
a = newStringTable(mode)
|
||||
let b2 = b["table"]
|
||||
for k,v in b2: a[k] = jsonTo(v, string)
|
||||
|
||||
proc toJsonHook*[](a: StringTableRef): auto =
|
||||
## for json.toJson
|
||||
mixin newJObject
|
||||
mixin toJson
|
||||
result = newJObject()
|
||||
result["mode"] = toJson($a.mode)
|
||||
let t = newJObject()
|
||||
for k,v in a: t[k] = toJson(v)
|
||||
result["table"] = t
|
||||
|
||||
when isMainModule:
|
||||
var x = {"k": "v", "11": "22", "565": "67"}.newStringTable
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue