Merge pull request #10823 from narimiran/json-tuples
simplify json.% for tuples and objects
This commit is contained in:
commit
3e04afda9f
1 changed files with 7 additions and 13 deletions
|
|
@ -390,25 +390,19 @@ 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 `%`*[T: tuple|object](o: T): JsonNode =
|
||||||
Note: could use simply:
|
## Construct JsonNode from tuples and objects.
|
||||||
proc `%`*(o: object|tuple): JsonNode
|
##
|
||||||
but blocked by https://github.com/nim-lang/Nim/issues/10019
|
## If passed an anonymous tuple, creates `JArray JsonNode`,
|
||||||
]#
|
## otherwise (named tuples and objects) `JObject JsonNode`.
|
||||||
proc `%`*(o: tuple): JsonNode =
|
const isNamed = T is object or isNamedTuple(T)
|
||||||
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
when isNamed:
|
||||||
when isNamedTuple(type(o)):
|
|
||||||
result = newJObject()
|
result = newJObject()
|
||||||
for k, v in o.fieldPairs: result[k] = %v
|
for k, v in o.fieldPairs: result[k] = %v
|
||||||
else:
|
else:
|
||||||
result = newJArray()
|
result = newJArray()
|
||||||
for a in o.fields: result.add(%a)
|
for a in o.fields: result.add(%a)
|
||||||
|
|
||||||
proc `%`*(o: object): JsonNode =
|
|
||||||
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
|
||||||
result = newJObject()
|
|
||||||
for k, v in o.fieldPairs: result[k] = %v
|
|
||||||
|
|
||||||
proc `%`*(o: ref object): JsonNode =
|
proc `%`*(o: ref object): JsonNode =
|
||||||
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
## Generic constructor for JSON data. Creates a new `JObject JsonNode`
|
||||||
if o.isNil:
|
if o.isNil:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue