further `nil` fixes in `tojson` and `serialization`

This commit is contained in:
Vindaar 2018-10-03 19:37:36 +02:00 committed by flyx
parent 566e28dcc5
commit 890bbed5ed
2 changed files with 9 additions and 9 deletions

View File

@ -1349,7 +1349,7 @@ proc load*[K](input: Stream | string, target: var K)
else: internalError("Unexpected exception: " & $e.parent.name) else: internalError("Unexpected exception: " & $e.parent.name)
proc loadMultiDoc*[K](input: Stream | string, target: var seq[K]) = proc loadMultiDoc*[K](input: Stream | string, target: var seq[K]) =
if target.isNil: if target.len == 0:
target = newSeq[K]() target = newSeq[K]()
var var
parser = newYamlParser(serializationTagLibrary) parser = newYamlParser(serializationTagLibrary)

View File

@ -116,7 +116,7 @@ proc constructJson*(s: var YamlStream): seq[JsonNode]
if levels.len == 0: if levels.len == 0:
# parser ensures that next event will be yamlEndDocument # parser ensures that next event will be yamlEndDocument
levels.add((node: jsonFromScalar(event.scalarContent, levels.add((node: jsonFromScalar(event.scalarContent,
event.scalarTag), key: nil)) event.scalarTag), key: ""))
continue continue
case levels[levels.high].node.kind case levels[levels.high].node.kind
@ -127,7 +127,7 @@ proc constructJson*(s: var YamlStream): seq[JsonNode]
if event.scalarAnchor != yAnchorNone: if event.scalarAnchor != yAnchorNone:
anchors[event.scalarAnchor] = jsonScalar anchors[event.scalarAnchor] = jsonScalar
of JObject: of JObject:
if isNil(levels[levels.high].key): if levels[levels.high].key.len == 0:
# JSON only allows strings as keys # JSON only allows strings as keys
levels[levels.high].key = event.scalarContent levels[levels.high].key = event.scalarContent
if event.scalarAnchor != yAnchorNone: if event.scalarAnchor != yAnchorNone:
@ -137,7 +137,7 @@ proc constructJson*(s: var YamlStream): seq[JsonNode]
let jsonScalar = jsonFromScalar(event.scalarContent, let jsonScalar = jsonFromScalar(event.scalarContent,
event.scalarTag) event.scalarTag)
levels[levels.high].node[levels[levels.high].key] = jsonScalar levels[levels.high].node[levels[levels.high].key] = jsonScalar
levels[levels.high].key = nil levels[levels.high].key = ""
if event.scalarAnchor != yAnchorNone: if event.scalarAnchor != yAnchorNone:
anchors[event.scalarAnchor] = jsonScalar anchors[event.scalarAnchor] = jsonScalar
else: else:
@ -148,12 +148,12 @@ proc constructJson*(s: var YamlStream): seq[JsonNode]
case levels[levels.high].node.kind case levels[levels.high].node.kind
of JArray: levels[levels.high].node.elems.add(level.node) of JArray: levels[levels.high].node.elems.add(level.node)
of JObject: of JObject:
if isNil(levels[levels.high].key): if levels[levels.high].key.len == 0:
raise newException(YamlConstructionError, raise newException(YamlConstructionError,
"non-scalar as key not allowed in JSON") "non-scalar as key not allowed in JSON")
else: else:
levels[levels.high].node[levels[levels.high].key] = level.node levels[levels.high].node[levels[levels.high].key] = level.node
levels[levels.high].key = nil levels[levels.high].key = ""
else: else:
internalError("Unexpected node kind: " & internalError("Unexpected node kind: " &
$levels[levels.high].node.kind) $levels[levels.high].node.kind)
@ -166,13 +166,13 @@ proc constructJson*(s: var YamlStream): seq[JsonNode]
levels[levels.high].node.elems.add( levels[levels.high].node.elems.add(
anchors.getOrDefault(event.aliasTarget)) anchors.getOrDefault(event.aliasTarget))
of JObject: of JObject:
if isNil(levels[levels.high].key): if levels[levels.high].key.len == 0:
raise newException(YamlConstructionError, raise newException(YamlConstructionError,
"cannot use alias node as key in JSON") "cannot use alias node as key in JSON")
else: else:
levels[levels.high].node.fields.add( levels[levels.high].node.fields.add(
levels[levels.high].key, anchors.getOrDefault(event.aliasTarget)) levels[levels.high].key, anchors.getOrDefault(event.aliasTarget))
levels[levels.high].key = nil levels[levels.high].key = ""
else: else:
internalError("Unexpected node kind: " & $levels[levels.high].node.kind) internalError("Unexpected node kind: " & $levels[levels.high].node.kind)