jsonutils: support cstring (including as Table key); improve docs (#16062)
* jsonutils: support cstring (including as Table key); improve docs * changelog * un-disable a test now that #16061 was fixed
This commit is contained in:
parent
65efa727ff
commit
9b67e5c61b
4 changed files with 28 additions and 10 deletions
|
|
@ -11,7 +11,7 @@ runnableExamples:
|
|||
z1: int8
|
||||
let a = (1.5'f32, (b: "b2", a: "a2"), 'x', @[Foo(t: true, z1: -3), nil], [{"name": "John"}.newStringTable])
|
||||
let j = a.toJson
|
||||
doAssert j.jsonTo(typeof(a)).toJson == j
|
||||
assert j.jsonTo(typeof(a)).toJson == j
|
||||
|
||||
import std/[json,strutils,tables,sets,strtabs,options]
|
||||
|
||||
|
|
@ -197,6 +197,11 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
|
|||
else:
|
||||
a.distinctBase.fromJson(b)
|
||||
elif T is string|SomeNumber: a = to(b,T)
|
||||
elif T is cstring:
|
||||
case b.kind
|
||||
of JNull: a = nil
|
||||
of JString: a = b.str
|
||||
else: checkJson false, $($T, " ", b)
|
||||
elif T is JsonNode: a = b
|
||||
elif T is ref | ptr:
|
||||
if b.kind == JNull: a = nil
|
||||
|
|
@ -273,9 +278,10 @@ proc toJson*[T](a: T): JsonNode =
|
|||
elif T is bool: result = %(a)
|
||||
elif T is SomeInteger: result = %a
|
||||
elif T is Ordinal: result = %(a.ord)
|
||||
elif T is cstring: (if a == nil: result = newJNull() else: result = % $a)
|
||||
else: result = %a
|
||||
|
||||
proc fromJsonHook*[K, V](t: var (Table[K, V] | OrderedTable[K, V]),
|
||||
proc fromJsonHook*[K: string|cstring, V](t: var (Table[K, V] | OrderedTable[K, V]),
|
||||
jsonNode: JsonNode) =
|
||||
## Enables `fromJson` for `Table` and `OrderedTable` types.
|
||||
##
|
||||
|
|
@ -296,21 +302,27 @@ proc fromJsonHook*[K, V](t: var (Table[K, V] | OrderedTable[K, V]),
|
|||
for k, v in jsonNode:
|
||||
t[k] = jsonTo(v, V)
|
||||
|
||||
proc toJsonHook*[K, V](t: (Table[K, V] | OrderedTable[K, V])): JsonNode =
|
||||
proc toJsonHook*[K: string|cstring, V](t: (Table[K, V] | OrderedTable[K, V])): JsonNode =
|
||||
## Enables `toJson` for `Table` and `OrderedTable` types.
|
||||
##
|
||||
## See also:
|
||||
## * `fromJsonHook proc<#fromJsonHook,,JsonNode>`_
|
||||
# pending PR #9217 use: toSeq(a) instead of `collect` in `runnableExamples`.
|
||||
runnableExamples:
|
||||
import std/[tables, json]
|
||||
import std/[tables, json, sugar]
|
||||
let foo = (
|
||||
t: [("two", 2)].toTable,
|
||||
ot: [("one", 1), ("three", 3)].toOrderedTable)
|
||||
assert $toJson(foo) == """{"t":{"two":2},"ot":{"one":1,"three":3}}"""
|
||||
# if keys are not string|cstring, you can use this:
|
||||
let a = {10: "foo", 11: "bar"}.newOrderedTable
|
||||
let a2 = collect: (for k,v in a: (k,v))
|
||||
assert $toJson(a2) == """[[10,"foo"],[11,"bar"]]"""
|
||||
|
||||
result = newJObject()
|
||||
for k, v in pairs(t):
|
||||
result[k] = toJson(v)
|
||||
# not sure if $k has overhead for string
|
||||
result[(when K is string: k else: $k)] = toJson(v)
|
||||
|
||||
proc fromJsonHook*[A](s: var SomeSet[A], jsonNode: JsonNode) =
|
||||
## Enables `fromJson` for `HashSet` and `OrderedSet` types.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue