Skip to content

Commit

Permalink
Implement json.% for tables and options
Browse files Browse the repository at this point in the history
  • Loading branch information
GULPF authored and Araq committed Feb 13, 2019
1 parent d83520e commit 779c51c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/pure/json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ runnableExamples:

import
hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson,
typetraits
typetraits, options

export
tables.`$`
Expand Down Expand Up @@ -344,6 +344,16 @@ proc `%`*[T](elements: openArray[T]): JsonNode =
result = newJArray()
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:
# 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
Expand Down

0 comments on commit 779c51c

Please sign in to comment.