mirror of https://github.com/status-im/NimYAML.git
Better interface for loading to JSON
This commit is contained in:
parent
d828f4ddc5
commit
6617033fd5
|
@ -126,33 +126,32 @@ proc genJsonString(size: int, maxStringLen: int): string =
|
||||||
result.add(s)
|
result.add(s)
|
||||||
curSize += s.len
|
curSize += s.len
|
||||||
|
|
||||||
var cYaml1k, cYaml10k, cYaml100k, cJson1k, cJson10k, cJson100k: clock
|
var
|
||||||
|
cYaml1k, cYaml10k, cYaml100k, cJson1k, cJson10k, cJson100k: clock
|
||||||
randomize(42)
|
|
||||||
let
|
|
||||||
json1k = genJsonString(1, 32)
|
json1k = genJsonString(1, 32)
|
||||||
json10k = genJsonString(10, 32)
|
json10k = genJsonString(10, 32)
|
||||||
json100k = genJsonString(100, 32)
|
json100k = genJsonString(100, 32)
|
||||||
|
parser = newParser(coreTagLibrary())
|
||||||
|
|
||||||
var s = newStringStream(json1k)
|
s = newStringStream(json1k)
|
||||||
|
|
||||||
block:
|
block:
|
||||||
bench(cYaml1k):
|
bench(cYaml1k):
|
||||||
let res = parseToJson(s)
|
let res = constructJson(parser.parse(s))
|
||||||
assert res[0].kind == JObject
|
assert res[0].kind == JObject
|
||||||
|
|
||||||
s = newStringStream(json10k)
|
s = newStringStream(json10k)
|
||||||
|
|
||||||
block:
|
block:
|
||||||
bench(cYaml10k):
|
bench(cYaml10k):
|
||||||
let res = parseToJson(s)
|
let res = constructJson(parser.parse(s))
|
||||||
assert res[0].kind == JObject
|
assert res[0].kind == JObject
|
||||||
|
|
||||||
s = newStringStream(json100k)
|
s = newStringStream(json100k)
|
||||||
|
|
||||||
block:
|
block:
|
||||||
bench(cYaml100k):
|
bench(cYaml100k):
|
||||||
let res = parseToJson(s)
|
let res = constructJson(parser.parse(s))
|
||||||
assert res[0].kind == JObject
|
assert res[0].kind == JObject
|
||||||
|
|
||||||
block:
|
block:
|
||||||
|
|
|
@ -62,19 +62,14 @@ proc jsonFromScalar(content: string, tag: TagId,
|
||||||
result.kind = JString
|
result.kind = JString
|
||||||
result.str = content
|
result.str = content
|
||||||
|
|
||||||
proc parseToJson*(s: string): seq[JsonNode] =
|
proc constructJson*(s: YamlStream): seq[JsonNode] =
|
||||||
result = parseToJson(newStringStream(s))
|
|
||||||
|
|
||||||
proc parseToJson*(s: Stream): seq[JsonNode] =
|
|
||||||
newSeq(result, 0)
|
newSeq(result, 0)
|
||||||
|
|
||||||
var
|
var
|
||||||
levels = newSeq[Level]()
|
levels = newSeq[Level]()
|
||||||
parser = newParser(coreTagLibrary())
|
|
||||||
events = parser.parse(s)
|
|
||||||
anchors = initTable[AnchorId, JsonNode]()
|
anchors = initTable[AnchorId, JsonNode]()
|
||||||
|
|
||||||
for event in events():
|
for event in s():
|
||||||
case event.kind
|
case event.kind
|
||||||
of yamlStartDocument:
|
of yamlStartDocument:
|
||||||
# we don't need to do anything here; root node will be created
|
# we don't need to do anything here; root node will be created
|
||||||
|
|
14
yaml.nim
14
yaml.nim
|
@ -296,11 +296,12 @@ proc anchor*(parser: YamlSequentialParser, id: AnchorId): string
|
||||||
proc parse*(parser: YamlSequentialParser, s: Stream): YamlStream
|
proc parse*(parser: YamlSequentialParser, s: Stream): YamlStream
|
||||||
## Parse a YAML character stream. ``s`` must be readable.
|
## Parse a YAML character stream. ``s`` must be readable.
|
||||||
|
|
||||||
proc parseToJson*(s: Stream): seq[JsonNode]
|
proc constructJson*(s: YamlStream): seq[JsonNode]
|
||||||
## Parse a YAML character stream to the standard library's in-memory JSON
|
## Construct an in-memory JSON tree from a YAML event stream. The stream may
|
||||||
## representation. The input may not contain any tags apart from those in
|
## not contain any tags apart from those in ``coreTagLibrary``. Anchors and
|
||||||
## ``coreTagLibrary``. Anchors and aliases will be resolved. Maps in the
|
## aliases will be resolved. Maps in the input must not contain
|
||||||
## input must not contain non-scalars as keys.
|
## 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
|
## **Warning:** The special float values ``[+-]Inf`` and ``NaN`` will be
|
||||||
## parsed into Nim's JSON structure without error. However, they cannot 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
|
## check for these values and will output invalid JSON when rendering one
|
||||||
## of these values into a JSON character stream.
|
## 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,
|
proc dump*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
|
||||||
style: YamlDumpStyle = yDumpDefault, indentationStep: int = 2)
|
style: YamlDumpStyle = yDumpDefault, indentationStep: int = 2)
|
||||||
## Convert ``s`` to a YAML character stream and write it to ``target``.
|
## Convert ``s`` to a YAML character stream and write it to ``target``.
|
||||||
|
|
Loading…
Reference in New Issue