From 779c51c29b7d78ec57ac2b50d2a1fe01056dbf5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nihlg=C3=A5rd?= Date: Sat, 9 Feb 2019 22:34:35 +0100 Subject: [PATCH] Implement `json.%` for tables and options --- lib/pure/json.nim | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 176da1d9dc..e387c516b8 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -143,7 +143,7 @@ runnableExamples: import hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson, - typetraits + typetraits, options export tables.`$` @@ -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