diff --git a/bench/json.nim b/bench/json.nim index 7d4b05b..c2b9df3 100644 --- a/bench/json.nim +++ b/bench/json.nim @@ -126,33 +126,32 @@ proc genJsonString(size: int, maxStringLen: int): string = result.add(s) curSize += s.len -var cYaml1k, cYaml10k, cYaml100k, cJson1k, cJson10k, cJson100k: clock - -randomize(42) -let +var + cYaml1k, cYaml10k, cYaml100k, cJson1k, cJson10k, cJson100k: clock json1k = genJsonString(1, 32) json10k = genJsonString(10, 32) json100k = genJsonString(100, 32) - -var s = newStringStream(json1k) + parser = newParser(coreTagLibrary()) + + s = newStringStream(json1k) block: bench(cYaml1k): - let res = parseToJson(s) + let res = constructJson(parser.parse(s)) assert res[0].kind == JObject s = newStringStream(json10k) block: bench(cYaml10k): - let res = parseToJson(s) + let res = constructJson(parser.parse(s)) assert res[0].kind == JObject s = newStringStream(json100k) block: bench(cYaml100k): - let res = parseToJson(s) + let res = constructJson(parser.parse(s)) assert res[0].kind == JObject block: diff --git a/private/json.nim b/private/json.nim index 598c227..f49346f 100644 --- a/private/json.nim +++ b/private/json.nim @@ -62,19 +62,14 @@ proc jsonFromScalar(content: string, tag: TagId, result.kind = JString result.str = content -proc parseToJson*(s: string): seq[JsonNode] = - result = parseToJson(newStringStream(s)) - -proc parseToJson*(s: Stream): seq[JsonNode] = +proc constructJson*(s: YamlStream): seq[JsonNode] = newSeq(result, 0) var levels = newSeq[Level]() - parser = newParser(coreTagLibrary()) - events = parser.parse(s) anchors = initTable[AnchorId, JsonNode]() - for event in events(): + for event in s(): case event.kind of yamlStartDocument: # we don't need to do anything here; root node will be created diff --git a/yaml.nim b/yaml.nim index fb4dac5..f587265 100644 --- a/yaml.nim +++ b/yaml.nim @@ -296,11 +296,12 @@ proc anchor*(parser: YamlSequentialParser, id: AnchorId): string proc parse*(parser: YamlSequentialParser, s: Stream): YamlStream ## Parse a YAML character stream. ``s`` must be readable. -proc parseToJson*(s: Stream): seq[JsonNode] - ## Parse a YAML character stream to the standard library's in-memory JSON - ## representation. The input may not contain any tags apart from those in - ## ``coreTagLibrary``. Anchors and aliases will be resolved. Maps in the - ## input must not contain non-scalars as keys. +proc constructJson*(s: YamlStream): seq[JsonNode] + ## Construct an in-memory JSON tree from a YAML event stream. The stream may + ## not contain any tags apart from those in ``coreTagLibrary``. Anchors and + ## aliases will be resolved. Maps in the input must not contain + ## non-scalars as keys. Each element of the result represents one document + ## in the YAML stream. ## ## **Warning:** The special float values ``[+-]Inf`` and ``NaN`` will be ## parsed into Nim's JSON structure without error. However, they cannot be @@ -309,9 +310,6 @@ proc parseToJson*(s: Stream): seq[JsonNode] ## check for these values and will output invalid JSON when rendering one ## of these values into a JSON character stream. -proc parseToJson*(s: string): seq[JsonNode] - ## see `parseToJson <#parseToJson,Stream,seq[JsonNode]>`_ - proc dump*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary, style: YamlDumpStyle = yDumpDefault, indentationStep: int = 2) ## Convert ``s`` to a YAML character stream and write it to ``target``.