mirror of https://github.com/status-im/NimYAML.git
Fixed presenter JSON output
This commit is contained in:
parent
58ec98f639
commit
46acdc3ccd
|
@ -13,8 +13,8 @@ type
|
||||||
|
|
||||||
proc needsEscaping(scalar: string): bool {.raises: [].} =
|
proc needsEscaping(scalar: string): bool {.raises: [].} =
|
||||||
scalar.len == 0 or
|
scalar.len == 0 or
|
||||||
scalar.find({'{', '}', '[', ']', ',', '#', '-', ':', '?', '%',
|
scalar.find({'{', '}', '[', ']', ',', '#', '-', ':', '?', '%', '"',
|
||||||
'\x0A', '\c'}) != -1
|
'\'', '\x0A', '\c'}) != -1
|
||||||
|
|
||||||
proc writeDoubleQuoted(scalar: string, s: Stream)
|
proc writeDoubleQuoted(scalar: string, s: Stream)
|
||||||
{.raises: [YamlPresenterOutputError].} =
|
{.raises: [YamlPresenterOutputError].} =
|
||||||
|
@ -71,9 +71,11 @@ proc startItem(target: Stream, style: YamlPresentationStyle, indentation: int,
|
||||||
if (isObject and style != ypsMinimal) or
|
if (isObject and style != ypsMinimal) or
|
||||||
style in [ypsJson, ypsCanonical]:
|
style in [ypsJson, ypsCanonical]:
|
||||||
target.write(",\x0A" & repeat(' ', indentation))
|
target.write(",\x0A" & repeat(' ', indentation))
|
||||||
if style != ypsJson:
|
if style == ypsJson:
|
||||||
|
state = dFlowImplicitMapKey
|
||||||
|
else:
|
||||||
target.write("? ")
|
target.write("? ")
|
||||||
state = dFlowExplicitMapKey
|
state = dFlowExplicitMapKey
|
||||||
elif isObject and style == ypsMinimal:
|
elif isObject and style == ypsMinimal:
|
||||||
target.write(", ? ")
|
target.write(", ? ")
|
||||||
state = dFlowExplicitMapKey
|
state = dFlowExplicitMapKey
|
||||||
|
@ -84,9 +86,11 @@ proc startItem(target: Stream, style: YamlPresentationStyle, indentation: int,
|
||||||
if (isObject and style != ypsMinimal) or
|
if (isObject and style != ypsMinimal) or
|
||||||
style in [ypsJson, ypsCanonical]:
|
style in [ypsJson, ypsCanonical]:
|
||||||
target.write("\x0A" & repeat(' ', indentation))
|
target.write("\x0A" & repeat(' ', indentation))
|
||||||
if style != ypsJson:
|
if style == ypsJson:
|
||||||
|
state = dFlowImplicitMapKey
|
||||||
|
else:
|
||||||
target.write("? ")
|
target.write("? ")
|
||||||
state = dFlowExplicitMapKey
|
state = dFlowExplicitMapKey
|
||||||
else:
|
else:
|
||||||
state = dFlowImplicitMapKey
|
state = dFlowImplicitMapKey
|
||||||
of dFlowImplicitMapKey:
|
of dFlowImplicitMapKey:
|
||||||
|
@ -208,18 +212,15 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
|
||||||
raise newException(YamlPresenterJsonError,
|
raise newException(YamlPresenterJsonError,
|
||||||
"Infinity and not-a-number values cannot be presented as JSON!")
|
"Infinity and not-a-number values cannot be presented as JSON!")
|
||||||
else:
|
else:
|
||||||
safeWrite(item.scalarContent)
|
writeDoubleQuoted(item.scalarContent, target)
|
||||||
elif style == ypsCanonical or item.scalarContent.needsEscaping or
|
elif style == ypsCanonical or item.scalarContent.needsEscaping:
|
||||||
(style == ypsJson and
|
|
||||||
(item.scalarTag notin [yTagQuestionMark, yTagInteger, yTagFloat,
|
|
||||||
yTagBoolean, yTagNull] or
|
|
||||||
(item.scalarTag == yTagQuestionMark and item.scalarType notin
|
|
||||||
[yTypeBoolFalse, yTypeBoolTrue, yTypeInteger, yTypeFloat,
|
|
||||||
yTypeNull]))):
|
|
||||||
writeDoubleQuoted(item.scalarContent, target)
|
writeDoubleQuoted(item.scalarContent, target)
|
||||||
else:
|
else:
|
||||||
safeWrite(item.scalarContent)
|
safeWrite(item.scalarContent)
|
||||||
of yamlAlias:
|
of yamlAlias:
|
||||||
|
if style == ypsJson:
|
||||||
|
raise newException(YamlPresenterJsonError,
|
||||||
|
"Alias not allowed in JSON output")
|
||||||
assert levels.len > 0
|
assert levels.len > 0
|
||||||
startItem(target, style, indentation, levels[levels.high], false)
|
startItem(target, style, indentation, levels[levels.high], false)
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue