From 5b8f33a9058b78e2e714ce341107ac52d98dcc5f Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Sat, 16 Sep 2017 20:09:44 +0200 Subject: Reorder json `add` and `%`, fixes #6385 (#6388) * Reorder json `add` and `%`, fixes https://github.com/nim-lang/Nim/issues/6385 * rename json test files --- lib/pure/json.nim | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'lib/pure/json.nim') diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 097952588..73c4f97f4 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -721,6 +721,16 @@ proc getElems*(n: JsonNode, default: seq[JsonNode] = @[]): seq[JsonNode] = if n.isNil or n.kind != JArray: return default else: return n.elems +proc add*(father, child: JsonNode) = + ## Adds `child` to a JArray node `father`. + assert father.kind == JArray + father.elems.add(child) + +proc add*(obj: JsonNode, key: string, val: JsonNode) = + ## Sets a field from a `JObject`. + assert obj.kind == JObject + obj.fields[key] = val + proc `%`*(s: string): JsonNode = ## Generic constructor for JSON data. Creates a new `JString JsonNode`. new(result) @@ -909,16 +919,6 @@ proc contains*(node: JsonNode, val: JsonNode): bool = proc existsKey*(node: JsonNode, key: string): bool {.deprecated.} = node.hasKey(key) ## Deprecated for `hasKey` -proc add*(father, child: JsonNode) = - ## Adds `child` to a JArray node `father`. - assert father.kind == JArray - father.elems.add(child) - -proc add*(obj: JsonNode, key: string, val: JsonNode) = - ## Sets a field from a `JObject`. - assert obj.kind == JObject - obj.fields[key] = val - proc `[]=`*(obj: JsonNode, key: string, val: JsonNode) {.inline.} = ## Sets a field from a `JObject`. assert(obj.kind == JObject) -- cgit 1.4.1-2-gfad0 From d453bc1d4dc2c56215be34967afdca3a33b63042 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Thu, 28 Sep 2017 10:35:45 +0200 Subject: fixes #6438 --- lib/pure/json.nim | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'lib/pure/json.nim') diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 73c4f97f4..f02bdbf08 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -769,6 +769,19 @@ proc `%`*[T](elements: openArray[T]): JsonNode = result = newJArray() for elem in elements: result.add(%elem) +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 + # causing problems later on. + proc `%`*(elements: set[bool]): JsonNode = + ## Generic constructor for JSON data. Creates a new `JObject JsonNode`. + ## This can only be used with the empty set ``{}`` and is supported + ## to prevent the gotcha ``%*{}`` which used to produce an empty + ## JSON array. + result = newJObject() + assert false notin elements, "usage error: only empty sets allowed" + assert true notin elements, "usage error: only empty sets allowed" + proc `%`*(o: object): JsonNode = ## Generic constructor for JSON data. Creates a new `JObject JsonNode` result = newJObject() @@ -789,27 +802,25 @@ proc `%`*(o: enum): JsonNode = proc toJson(x: NimNode): NimNode {.compiletime.} = case x.kind of nnkBracket: # array + if x.len == 0: return newCall(bindSym"newJArray") result = newNimNode(nnkBracket) for i in 0 .. Date: Thu, 28 Sep 2017 11:35:19 +0300 Subject: Add default value for filename in parseJson proc (#6441) --- lib/pure/json.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/pure/json.nim') diff --git a/lib/pure/json.nim b/lib/pure/json.nim index f02bdbf08..656114fb1 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -1214,7 +1214,7 @@ proc parseJson(p: var JsonParser): JsonNode = raiseParseErr(p, "{") when not defined(js): - proc parseJson*(s: Stream, filename: string): JsonNode = + proc parseJson*(s: Stream, filename: string = ""): JsonNode = ## Parses from a stream `s` into a `JsonNode`. `filename` is only needed ## for nice error messages. ## If `s` contains extra data, it will raise `JsonParsingError`. -- cgit 1.4.1-2-gfad0