From f223e94ccd98448e245e5a7bd7e7da396b9ae9c8 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 5 Jan 2015 01:30:30 +0100 Subject: [PATCH] Add operator `%*` to JSON --- lib/pure/json.nim | 77 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index c5510a50e2..eed01e3761 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -30,9 +30,28 @@ ## ## 1.3000000000000000e+00 ## true +## +## This module can also be used to comfortably create JSON using the `%*` +## operator: +## +## .. code-block:: nim +## +## var hisName = "John" +## let herAge = 31 +## var j = %* +## [ +## { +## "name": hisName, +## "age": 30 +## }, +## { +## "name": "Susan", +## "age": herAge +## } +## ] import - hashes, strutils, lexbase, streams, unicode + hashes, strutils, lexbase, streams, unicode, macros type JsonEventKind* = enum ## enumeration of all events that may occur when parsing @@ -625,6 +644,31 @@ proc `%`*(elements: openArray[JsonNode]): JsonNode = newSeq(result.elems, elements.len) for i, p in pairs(elements): result.elems[i] = p +proc toJson(x: expr): expr {.compiletime.} = + case x.kind + of nnkBracket: + result = newNimNode(nnkBracket) + for i in 0 ..