diff options
Diffstat (limited to 'lib/pure/json.nim')
-rw-r--r-- | lib/pure/json.nim | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 19947fbc2..c76bbc3b5 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -580,7 +580,7 @@ type of JNull: nil of JObject: - fields*: Table[string, JsonNode] + fields*: OrderedTable[string, JsonNode] of JArray: elems*: seq[JsonNode] @@ -630,7 +630,7 @@ proc newJObject*(): JsonNode = ## Creates a new `JObject JsonNode` new(result) result.kind = JObject - result.fields = initTable[string, JsonNode](4) + result.fields = initOrderedTable[string, JsonNode](4) proc newJArray*(): JsonNode = ## Creates a new `JArray JsonNode` @@ -670,8 +670,8 @@ proc getBVal*(n: JsonNode, default: bool = false): bool = else: return n.bval proc getFields*(n: JsonNode, - default = initTable[string, JsonNode](4)): - Table[string, JsonNode] = + default = initOrderedTable[string, JsonNode](4)): + OrderedTable[string, JsonNode] = ## Retrieves the key, value pairs of a `JObject JsonNode`. ## ## Returns ``default`` if ``n`` is not a ``JObject``, or if ``n`` is nil. @@ -760,12 +760,12 @@ proc toJson(x: NimNode): NimNode {.compiletime.} = result = prefix(result, "%") -macro `%*`*(x: expr): expr = +macro `%*`*(x: untyped): untyped = ## Convert an expression to a JsonNode directly, without having to specify ## `%` for every element. result = toJson(x) -proc `==`* (a,b: JsonNode): bool = +proc `==`* (a, b: JsonNode): bool = ## Check two nodes for equality if a.isNil: if b.isNil: return true @@ -773,23 +773,29 @@ proc `==`* (a,b: JsonNode): bool = elif b.isNil or a.kind != b.kind: return false else: - return case a.kind + case a.kind of JString: - a.str == b.str + result = a.str == b.str of JInt: - a.num == b.num + result = a.num == b.num of JFloat: - a.fnum == b.fnum + result = a.fnum == b.fnum of JBool: - a.bval == b.bval + result = a.bval == b.bval of JNull: - true + result = true of JArray: - a.elems == b.elems + result = a.elems == b.elems of JObject: - a.fields == b.fields + # we cannot use OrderedTable's equality here as + # the order does not matter for equality here. + if a.fields.len != b.fields.len: return false + for key, val in a.fields: + if not b.fields.hasKey(key): return false + if b.fields[key] != val: return false + result = true -proc hash*(n: Table[string, JsonNode]): Hash {.noSideEffect.} +proc hash*(n: OrderedTable[string, JsonNode]): Hash {.noSideEffect.} proc hash*(n: JsonNode): Hash = ## Compute the hash for a JSON node @@ -807,9 +813,9 @@ proc hash*(n: JsonNode): Hash = of JString: result = hash(n.str) of JNull: - result = hash(0) + result = Hash(0) -proc hash*(n: Table[string, JsonNode]): Hash = +proc hash*(n: OrderedTable[string, JsonNode]): Hash = for key, val in n: result = result xor (hash(key) !& hash(val)) result = !$result |