Implement json.% for tables and options
This commit is contained in:
parent
0c4d1fd10b
commit
f1ae0ed6ea
1 changed files with 11 additions and 1 deletions
|
|
@ -143,7 +143,7 @@ runnableExamples:
|
||||||
|
|
||||||
import
|
import
|
||||||
hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson,
|
hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson,
|
||||||
typetraits
|
typetraits, options
|
||||||
|
|
||||||
export
|
export
|
||||||
tables.`$`
|
tables.`$`
|
||||||
|
|
@ -344,6 +344,16 @@ proc `%`*[T](elements: openArray[T]): JsonNode =
|
||||||
result = newJArray()
|
result = newJArray()
|
||||||
for elem in elements: result.add(%elem)
|
for elem in elements: result.add(%elem)
|
||||||
|
|
||||||
|
proc `%`*[T](table: Table[string, T]|OrderedTable[string, T]): JsonNode =
|
||||||
|
## Generic constructor for JSON data. Creates a new ``JObject JsonNode``.
|
||||||
|
result = newJObject()
|
||||||
|
for k, v in table: result[k] = %v
|
||||||
|
|
||||||
|
proc `%`*[T](opt: Option[T]): JsonNode =
|
||||||
|
## Generic constructor for JSON data. Creates a new ``JNull JsonNode``
|
||||||
|
## if ``opt`` is empty, otherwise it delegates to the underlying value.
|
||||||
|
if opt.isSome: %opt.get else: newJNull()
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
# For 'consistency' we could do this, but that only pushes people further
|
# For 'consistency' we could do this, but that only pushes people further
|
||||||
# into that evil comfort zone where they can use Nim without understanding it
|
# into that evil comfort zone where they can use Nim without understanding it
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue