Fixed presenter JSON output

This commit is contained in:
Felix Krause 2016-01-14 19:00:03 +01:00
parent 58ec98f639
commit 46acdc3ccd
1 changed files with 16 additions and 15 deletions

View File

@ -13,8 +13,8 @@ type
proc needsEscaping(scalar: string): bool {.raises: [].} =
scalar.len == 0 or
scalar.find({'{', '}', '[', ']', ',', '#', '-', ':', '?', '%',
'\x0A', '\c'}) != -1
scalar.find({'{', '}', '[', ']', ',', '#', '-', ':', '?', '%', '"',
'\'', '\x0A', '\c'}) != -1
proc writeDoubleQuoted(scalar: string, s: Stream)
{.raises: [YamlPresenterOutputError].} =
@ -71,7 +71,9 @@ proc startItem(target: Stream, style: YamlPresentationStyle, indentation: int,
if (isObject and style != ypsMinimal) or
style in [ypsJson, ypsCanonical]:
target.write(",\x0A" & repeat(' ', indentation))
if style != ypsJson:
if style == ypsJson:
state = dFlowImplicitMapKey
else:
target.write("? ")
state = dFlowExplicitMapKey
elif isObject and style == ypsMinimal:
@ -84,7 +86,9 @@ proc startItem(target: Stream, style: YamlPresentationStyle, indentation: int,
if (isObject and style != ypsMinimal) or
style in [ypsJson, ypsCanonical]:
target.write("\x0A" & repeat(' ', indentation))
if style != ypsJson:
if style == ypsJson:
state = dFlowImplicitMapKey
else:
target.write("? ")
state = dFlowExplicitMapKey
else:
@ -208,18 +212,15 @@ proc present*(s: YamlStream, target: Stream, tagLib: YamlTagLibrary,
raise newException(YamlPresenterJsonError,
"Infinity and not-a-number values cannot be presented as JSON!")
else:
safeWrite(item.scalarContent)
elif style == ypsCanonical or item.scalarContent.needsEscaping or
(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)
elif style == ypsCanonical or item.scalarContent.needsEscaping:
writeDoubleQuoted(item.scalarContent, target)
else:
safeWrite(item.scalarContent)
of yamlAlias:
if style == ypsJson:
raise newException(YamlPresenterJsonError,
"Alias not allowed in JSON output")
assert levels.len > 0
startItem(target, style, indentation, levels[levels.high], false)
try: