json: support tuple (#10010)
This commit is contained in:
parent
384e517f09
commit
b8454327c5
2 changed files with 20 additions and 1 deletions
|
|
@ -142,7 +142,8 @@ 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, unicode, macros, parsejson
|
hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson,
|
||||||
|
typetraits
|
||||||
|
|
||||||
export
|
export
|
||||||
tables.`$`
|
tables.`$`
|
||||||
|
|
@ -356,6 +357,20 @@ when false:
|
||||||
assert false notin elements, "usage error: only empty sets allowed"
|
assert false notin elements, "usage error: only empty sets allowed"
|
||||||
assert true notin elements, "usage error: only empty sets allowed"
|
assert true notin elements, "usage error: only empty sets allowed"
|
||||||
|
|
||||||
|
#[
|
||||||
|
Note: could use simply:
|
||||||
|
proc `%`*(o: object|tuple): JsonNode
|
||||||
|
but blocked by https://github.com/nim-lang/Nim/issues/10019
|
||||||
|
]#
|
||||||
|
proc `%`*(o: tuple): JsonNode =
|
||||||
|
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
||||||
|
when isNamedTuple(type(o)):
|
||||||
|
result = newJObject()
|
||||||
|
for k, v in o.fieldPairs: result[k] = %v
|
||||||
|
else:
|
||||||
|
result = newJArray()
|
||||||
|
for a in o.fields: result.add(%a)
|
||||||
|
|
||||||
proc `%`*(o: object): JsonNode =
|
proc `%`*(o: object): JsonNode =
|
||||||
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
||||||
result = newJObject()
|
result = newJObject()
|
||||||
|
|
|
||||||
|
|
@ -516,3 +516,7 @@ when true:
|
||||||
var w = u.to(MyDistRef)
|
var w = u.to(MyDistRef)
|
||||||
doAssert v.name == "smith"
|
doAssert v.name == "smith"
|
||||||
doAssert MyRef(w).name == "smith"
|
doAssert MyRef(w).name == "smith"
|
||||||
|
|
||||||
|
block test_tuple:
|
||||||
|
doAssert $(%* (a1: 10, a2: "foo")) == """{"a1":10,"a2":"foo"}"""
|
||||||
|
doAssert $(%* (10, "foo")) == """[10,"foo"]"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue