mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-03 03:32:32 +00:00
json: support tuple (#10010)
This commit is contained in:
committed by
Andreas Rumpf
parent
384e517f09
commit
b8454327c5
@@ -142,7 +142,8 @@ runnableExamples:
|
||||
doAssert $(%* Foo()) == """{"a1":0,"a2":0,"a0":0,"a3":0,"a4":0}"""
|
||||
|
||||
import
|
||||
hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson
|
||||
hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson,
|
||||
typetraits
|
||||
|
||||
export
|
||||
tables.`$`
|
||||
@@ -356,6 +357,20 @@ when false:
|
||||
assert false 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 =
|
||||
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
||||
result = newJObject()
|
||||
|
||||
@@ -516,3 +516,7 @@ when true:
|
||||
var w = u.to(MyDistRef)
|
||||
doAssert v.name == "smith"
|
||||
doAssert MyRef(w).name == "smith"
|
||||
|
||||
block test_tuple:
|
||||
doAssert $(%* (a1: 10, a2: "foo")) == """{"a1":10,"a2":"foo"}"""
|
||||
doAssert $(%* (10, "foo")) == """[10,"foo"]"""
|
||||
|
||||
Reference in New Issue
Block a user