2015-12-28 21:24:05 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2016-09-21 19:20:57 +00:00
|
|
|
# (c) Copyright 2016 Felix Krause
|
2015-12-28 21:24:05 +00:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-09-21 19:20:57 +00:00
|
|
|
## ==================
|
|
|
|
## Module yaml.tojson
|
|
|
|
## ==================
|
|
|
|
##
|
|
|
|
## The tojson API enables you to parser a YAML character stream into the JSON
|
|
|
|
## structures provided by Nim's stdlib.
|
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
import json, streams, strutils, tables
|
2020-11-03 21:08:21 +00:00
|
|
|
import data, taglib, hints, serialization, stream, private/internal, parser
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2018-10-10 15:45:54 +00:00
|
|
|
# represents a single YAML level. The `node` with name `key`.
|
|
|
|
# `expKey` is used to indicate that an empty node shall be filled
|
|
|
|
type Level = tuple[node: JsonNode, key: string, expKey: bool]
|
2015-12-24 14:21:49 +00:00
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc initLevel(node: JsonNode): Level {.raises: [].} =
|
2018-10-10 15:45:54 +00:00
|
|
|
(node: node, key: "", expKey: true)
|
2015-12-24 14:21:49 +00:00
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
proc jsonFromScalar(content: string, tag: Tag): JsonNode
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError].}=
|
|
|
|
new(result)
|
|
|
|
var mappedType: TypeHint
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
case tag
|
|
|
|
of yTagQuestionMark: mappedType = guessType(content)
|
|
|
|
of yTagExclamationMark, yTagString: mappedType = yTypeUnknown
|
|
|
|
of yTagBoolean:
|
|
|
|
case guessType(content)
|
|
|
|
of yTypeBoolTrue: mappedType = yTypeBoolTrue
|
|
|
|
of yTypeBoolFalse: mappedType = yTypeBoolFalse
|
|
|
|
else:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Invalid boolean value: " & content)
|
|
|
|
of yTagInteger: mappedType = yTypeInteger
|
|
|
|
of yTagNull: mappedType = yTypeNull
|
|
|
|
of yTagFloat:
|
|
|
|
case guessType(content)
|
|
|
|
of yTypeFloat: mappedType = yTypeFloat
|
|
|
|
of yTypeFloatInf: mappedType = yTypeFloatInf
|
|
|
|
of yTypeFloatNaN: mappedType = yTypeFloatNaN
|
|
|
|
else:
|
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"Invalid float value: " & content)
|
|
|
|
else: mappedType = yTypeUnknown
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
|
|
|
case mappedType
|
|
|
|
of yTypeInteger:
|
2019-06-16 11:43:08 +00:00
|
|
|
result = JsonNode(kind: JInt, num: parseBiggestInt(content))
|
2016-04-02 15:48:22 +00:00
|
|
|
of yTypeFloat:
|
2019-06-16 11:43:08 +00:00
|
|
|
result = JsonNode(kind: JFloat, fnum: parseFloat(content))
|
2016-04-02 15:48:22 +00:00
|
|
|
of yTypeFloatInf:
|
2019-06-16 11:43:08 +00:00
|
|
|
result = JsonNode(kind: JFloat, fnum: if content[0] == '-': NegInf else: Inf)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yTypeFloatNaN:
|
2019-06-16 11:43:08 +00:00
|
|
|
result = JsonNode(kind: JFloat, fnum: NaN)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yTypeBoolTrue:
|
2019-06-16 11:43:08 +00:00
|
|
|
result = JsonNode(kind: JBool, bval: true)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yTypeBoolFalse:
|
2019-06-16 11:43:08 +00:00
|
|
|
result = JsonNode(kind: JBool, bval: false)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yTypeNull:
|
2019-06-16 11:43:08 +00:00
|
|
|
result = JsonNode(kind: JNull)
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2019-06-16 11:43:08 +00:00
|
|
|
result = JsonNode(kind: JString)
|
2016-04-02 15:48:22 +00:00
|
|
|
shallowCopy(result.str, content)
|
|
|
|
except ValueError:
|
|
|
|
var e = newException(YamlConstructionError, "Cannot parse numeric value")
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
proc constructJson*(s: var YamlStream): seq[JsonNode]
|
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
## 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
|
|
|
|
## rendered to a JSON character stream, because these values are not part
|
|
|
|
## of the JSON specification. Nim's JSON implementation currently does not
|
|
|
|
## check for these values and will output invalid JSON when rendering one
|
|
|
|
## of these values into a JSON character stream.
|
2016-04-02 15:48:22 +00:00
|
|
|
newSeq(result, 0)
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
var
|
|
|
|
levels = newSeq[Level]()
|
2020-11-04 15:40:37 +00:00
|
|
|
anchors = initTable[Anchor, JsonNode]()
|
2016-04-02 15:48:22 +00:00
|
|
|
for event in s:
|
|
|
|
case event.kind
|
2020-11-04 15:40:37 +00:00
|
|
|
of yamlStartStream, yamlEndStream: discard
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlStartDoc:
|
|
|
|
# we don't need to do anything here; root node will be created
|
|
|
|
# by first scalar, sequence or map event
|
|
|
|
discard
|
|
|
|
of yamlEndDoc:
|
|
|
|
# we can savely assume that levels has e length of exactly 1.
|
|
|
|
result.add(levels.pop().node)
|
|
|
|
of yamlStartSeq:
|
|
|
|
levels.add(initLevel(newJArray()))
|
2020-11-03 21:08:21 +00:00
|
|
|
if event.seqProperties.anchor != yAnchorNone:
|
|
|
|
anchors[event.seqProperties.anchor] = levels[levels.high].node
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlStartMap:
|
|
|
|
levels.add(initLevel(newJObject()))
|
2020-11-04 15:40:37 +00:00
|
|
|
if event.mapProperties.anchor != yAnchorNone:
|
|
|
|
anchors[event.mapProperties.anchor] = levels[levels.high].node
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlScalar:
|
|
|
|
if levels.len == 0:
|
|
|
|
# parser ensures that next event will be yamlEndDocument
|
|
|
|
levels.add((node: jsonFromScalar(event.scalarContent,
|
2020-11-04 15:40:37 +00:00
|
|
|
event.scalarProperties.tag),
|
2018-10-10 15:45:54 +00:00
|
|
|
key: "",
|
|
|
|
expKey: true))
|
2016-04-02 15:48:22 +00:00
|
|
|
continue
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
case levels[levels.high].node.kind
|
|
|
|
of JArray:
|
|
|
|
let jsonScalar = jsonFromScalar(event.scalarContent,
|
2020-11-04 15:40:37 +00:00
|
|
|
event.scalarProperties.tag)
|
2016-04-02 15:48:22 +00:00
|
|
|
levels[levels.high].node.elems.add(jsonScalar)
|
2020-11-04 15:40:37 +00:00
|
|
|
if event.scalarProperties.anchor != yAnchorNone:
|
|
|
|
anchors[event.scalarProperties.anchor] = jsonScalar
|
2016-04-02 15:48:22 +00:00
|
|
|
of JObject:
|
2018-10-10 15:45:54 +00:00
|
|
|
if levels[levels.high].expKey:
|
|
|
|
levels[levels.high].expKey = false
|
2016-04-02 15:48:22 +00:00
|
|
|
# JSON only allows strings as keys
|
|
|
|
levels[levels.high].key = event.scalarContent
|
2020-11-04 15:40:37 +00:00
|
|
|
if event.scalarProperties.anchor != yAnchorNone:
|
2016-01-24 17:24:09 +00:00
|
|
|
raise newException(YamlConstructionError,
|
2016-04-02 15:48:22 +00:00
|
|
|
"scalar keys may not have anchors in JSON")
|
2016-01-24 17:24:09 +00:00
|
|
|
else:
|
2016-04-02 15:48:22 +00:00
|
|
|
let jsonScalar = jsonFromScalar(event.scalarContent,
|
2020-11-04 15:40:37 +00:00
|
|
|
event.scalarProperties.tag)
|
2016-04-02 15:48:22 +00:00
|
|
|
levels[levels.high].node[levels[levels.high].key] = jsonScalar
|
2018-10-10 15:45:54 +00:00
|
|
|
levels[levels.high].expKey = true
|
2020-11-04 15:40:37 +00:00
|
|
|
if event.scalarProperties.anchor != yAnchorNone:
|
|
|
|
anchors[event.scalarProperties.anchor] = jsonScalar
|
2016-08-09 19:20:25 +00:00
|
|
|
else:
|
|
|
|
internalError("Unexpected node kind: " & $levels[levels.high].node.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlEndSeq, yamlEndMap:
|
|
|
|
if levels.len > 1:
|
|
|
|
let level = levels.pop()
|
|
|
|
case levels[levels.high].node.kind
|
|
|
|
of JArray: levels[levels.high].node.elems.add(level.node)
|
|
|
|
of JObject:
|
2018-10-11 12:38:48 +00:00
|
|
|
if levels[levels.high].expKey:
|
2016-01-24 17:24:09 +00:00
|
|
|
raise newException(YamlConstructionError,
|
2016-04-02 15:48:22 +00:00
|
|
|
"non-scalar as key not allowed in JSON")
|
|
|
|
else:
|
|
|
|
levels[levels.high].node[levels[levels.high].key] = level.node
|
2018-10-10 15:45:54 +00:00
|
|
|
levels[levels.high].expKey = true
|
2016-08-09 19:20:25 +00:00
|
|
|
else:
|
|
|
|
internalError("Unexpected node kind: " &
|
|
|
|
$levels[levels.high].node.kind)
|
2016-04-02 15:48:22 +00:00
|
|
|
else: discard # wait for yamlEndDocument
|
|
|
|
of yamlAlias:
|
|
|
|
# we can savely assume that the alias exists in anchors
|
|
|
|
# (else the parser would have already thrown an exception)
|
|
|
|
case levels[levels.high].node.kind
|
|
|
|
of JArray:
|
2016-08-09 18:47:22 +00:00
|
|
|
levels[levels.high].node.elems.add(
|
|
|
|
anchors.getOrDefault(event.aliasTarget))
|
2016-04-02 15:48:22 +00:00
|
|
|
of JObject:
|
2018-10-11 12:38:48 +00:00
|
|
|
if levels[levels.high].expKey:
|
2016-04-02 15:48:22 +00:00
|
|
|
raise newException(YamlConstructionError,
|
|
|
|
"cannot use alias node as key in JSON")
|
2016-01-24 17:24:09 +00:00
|
|
|
else:
|
2016-08-09 18:47:22 +00:00
|
|
|
levels[levels.high].node.fields.add(
|
|
|
|
levels[levels.high].key, anchors.getOrDefault(event.aliasTarget))
|
2018-10-10 15:45:54 +00:00
|
|
|
levels[levels.high].expKey = true
|
2016-08-09 19:20:25 +00:00
|
|
|
else:
|
|
|
|
internalError("Unexpected node kind: " & $levels[levels.high].node.kind)
|
2016-01-05 18:58:46 +00:00
|
|
|
|
2017-03-29 15:14:50 +00:00
|
|
|
when not defined(JS):
|
|
|
|
proc loadToJson*(s: Stream): seq[JsonNode]
|
2020-11-04 15:40:37 +00:00
|
|
|
{.raises: [YamlParserError, YamlConstructionError, IOError, OSError].} =
|
2017-03-29 15:14:50 +00:00
|
|
|
## Uses `YamlParser <#YamlParser>`_ and
|
|
|
|
## `constructJson <#constructJson>`_ to construct an in-memory JSON tree
|
|
|
|
## from a YAML character stream.
|
2020-11-04 15:40:37 +00:00
|
|
|
var parser: YamlParser
|
|
|
|
parser.init(initCoreTagLibrary())
|
|
|
|
var events = parser.parse(s)
|
2017-03-29 15:14:50 +00:00
|
|
|
try:
|
|
|
|
return constructJson(events)
|
|
|
|
except YamlStreamError:
|
|
|
|
let e = getCurrentException()
|
|
|
|
if e.parent of IOError:
|
|
|
|
raise (ref IOError)(e.parent)
|
2020-11-10 20:28:56 +00:00
|
|
|
elif e.parent of OSError:
|
|
|
|
raise (ref OSError)(e.parent)
|
2017-03-29 15:14:50 +00:00
|
|
|
elif e.parent of YamlParserError:
|
|
|
|
raise (ref YamlParserError)(e.parent)
|
|
|
|
else: internalError("Unexpected exception: " & e.parent.repr)
|
2016-09-30 17:35:43 +00:00
|
|
|
|
|
|
|
proc loadToJson*(str: string): seq[JsonNode]
|
|
|
|
{.raises: [YamlParserError, YamlConstructionError].} =
|
|
|
|
## Uses `YamlParser <#YamlParser>`_ and
|
|
|
|
## `constructJson <#constructJson>`_ to construct an in-memory JSON tree
|
|
|
|
## from a YAML character stream.
|
2020-11-04 15:40:37 +00:00
|
|
|
var parser: YamlParser
|
|
|
|
parser.init(initCoreTagLibrary())
|
|
|
|
var events = parser.parse(str)
|
2016-09-30 17:35:43 +00:00
|
|
|
try: return constructJson(events)
|
|
|
|
except YamlStreamError:
|
|
|
|
let e = getCurrentException()
|
|
|
|
if e.parent of YamlParserError:
|
|
|
|
raise (ref YamlParserError)(e.parent)
|
2018-10-03 17:37:36 +00:00
|
|
|
else: internalError("Unexpected exception: " & e.parent.repr)
|