Fixed bugs in presenter, serialization, README

* Made presenter output numbers without double quotes in
   JSON style again
 * Fixed broken serialization.load
 * Updated examples in README
This commit is contained in:
Felix Krause 2016-01-24 20:53:51 +01:00
parent 8436026250
commit 36872370d5
3 changed files with 39 additions and 19 deletions

View File

@ -26,7 +26,7 @@ a list:
"""
var
parser = newParser(coreTagLibrary())
parser = newYamlParser(initCoreTagLibrary())
events = parser.parse(newStringStream(input))
for event in events():
@ -60,13 +60,13 @@ import yaml, streams
proc example(): YamlStream =
result = iterator(): YamlStreamEvent =
yield startDocEvent()
yield startMapEvent(mayHaveKeyObjects = false)
yield startMapEvent()
yield scalarEvent("an integer")
yield scalarEvent("42", tag = yTagInteger)
yield scalarEvent("a list")
yield startSeqEvent(tag = yTagSequence)
yield scalarEvent("item", tag = yTagString)
yield scalarEvent("no", tag = yTagBoolean, typeHint = yTypeBoolFalse)
yield scalarEvent("no", tag = yTagBoolean)
yield scalarEvent("")
yield endSeqEvent()
yield scalarEvent("a float")
@ -74,11 +74,11 @@ proc example(): YamlStream =
yield endMapEvent()
yield endDocEvent()
present(example(), newFileStream(stdout), coreTagLibrary(), yDumpBlockOnly)
present(example(), newFileStream(stdout), initCoreTagLibrary(), psBlockOnly)
echo "\n\n"
present(example(), newFileStream(stdout), coreTagLibrary(), yDumpCanonical)
present(example(), newFileStream(stdout), initCoreTagLibrary(), psCanonical)
echo "\n\n"
present(example(), newFileStream(stdout), coreTagLibrary(), yDumpJson)
present(example(), newFileStream(stdout), initCoreTagLibrary(), psJson)
```
Output:
@ -129,7 +129,7 @@ a float: !!float 3.14159
import yaml.serialization
import tables
make_serializable:
serializable:
type
Person = object
firstname, surname: string
@ -162,7 +162,7 @@ assert persons[1].additionalAttributes["location"] == "Celle"
# dumping
dump(persons, newFileStream(stdout), ypsCanonical)
dump(persons, newFileStream(stdout), psCanonical)
```
Output:
@ -177,9 +177,9 @@ Output:
? !!str "surname"
: !!str "Pan",
? !!str "age"
: !!int "12",
: !nim:int "12",
? !!str "additionalAttributes"
: {
: !nim:Table(tag:yaml.org,2002:str,tag:yaml.org,2002:str) {
? !!str "canFly"
: !!str "yes",
? !!str "location"
@ -192,9 +192,9 @@ Output:
? !!str "surname"
: !!str "Koch",
? !!str "age"
: !!int "23",
: !nim:int "23",
? !!str "additionalAttributes"
: {
: !nim:Table(tag:yaml.org,2002:str,tag:yaml.org,2002:str) {
? !!str "occupation"
: !!str "Hacker",
? !!str "location"

View File

@ -208,10 +208,16 @@ proc present*(s: YamlStream, target: Stream, tagLib: TagLibrary,
elif item.scalarTag in [yTagQuestionMark, yTagNull] and
hint == yTypeNull:
safeWrite("null")
elif item.scalarTag in [yTagQuestionMark, yTagInteger] and
hint == yTypeInteger:
safeWrite(item.scalarContent)
elif item.scalarTag in [yTagQuestionMark, yTagFloat] and
hint in [yTypeFloatInf, yTypeFloatNaN]:
raise newException(YamlPresenterJsonError,
"Infinity and not-a-number values cannot be presented as JSON!")
elif item.scalarTag in [yTagQuestionMark, yTagFloat] and
hint == yTypeFloat:
safeWrite(item.scalarContent)
else:
writeDoubleQuoted(item.scalarContent, target)
elif style == psCanonical or item.scalarContent.needsEscaping:

View File

@ -473,13 +473,27 @@ proc serialize*[K, V](value: Table[K, V],
yield YamlStreamEvent(kind: yamlEndMap)
proc load*[K](input: Stream, target: var K)
{.raises: [YamlConstructionError, YamlConstructionStreamError].} =
{.raises: [YamlConstructionError, IOError, YamlParserError].} =
try:
var
tagLib = serializationTagLibrary
events = parse(tagLib, input)
parser = newYamlParser(serializationTagLibrary)
events = parser.parse(input)
assert events().kind == yamlStartDocument
construct(events, target)
assert events().kind == yamlEndDocument
except YamlConstructionError, IOError, YamlParserError:
raise
except YamlConstructionStreamError:
let e = cast[ref YamlConstructionError](getCurrentException())
if e.parent is IOError:
raise cast[ref IOError](e.parent)
elif e.parent is YamlParserError:
raise cast[ref YamlParserError](e.parent)
else:
assert(false)
except Exception:
# compiler bug: https://github.com/nim-lang/Nim/issues/3772
assert(false)
proc dump*[K](value: K, target: Stream, style: PresentationStyle = psDefault,
tagStyle: TagStyle = tsRootOnly, indentationStep: int = 2)