2016-10-08 21:35:33 +00:00
|
|
|
import yaml, streams
|
2016-10-08 16:38:27 +00:00
|
|
|
type Person = object
|
|
|
|
name: string
|
|
|
|
|
2016-10-10 18:16:54 +00:00
|
|
|
setTagUri(Person, nimTag("demo:Person"), yTagPerson)
|
2016-10-08 16:38:27 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
s = newFileStream("in.yaml", fmRead)
|
|
|
|
context = newConstructionContext()
|
|
|
|
parser = newYamlParser(serializationTagLibrary)
|
|
|
|
events = parser.parse(s)
|
|
|
|
|
|
|
|
assert events.next().kind == yamlStartDoc
|
|
|
|
assert events.next().kind == yamlStartSeq
|
|
|
|
var nextEvent = events.peek()
|
|
|
|
while nextEvent.kind != yamlEndSeq:
|
|
|
|
var curTag = nextEvent.tag()
|
|
|
|
if curTag == yTagQuestionMark:
|
|
|
|
# we only support implicitly tagged scalars
|
|
|
|
assert nextEvent.kind == yamlScalar
|
|
|
|
case guessType(nextEvent.scalarContent)
|
|
|
|
of yTypeInteger: curTag = yTagInteger
|
|
|
|
of yTypeBoolTrue, yTypeBoolFalse:
|
|
|
|
curTag = yTagBoolean
|
|
|
|
of yTypeUnknown: curTag = yTagString
|
|
|
|
else: assert false, "Type not supported!"
|
|
|
|
elif curTag == yTagExclamationMark:
|
|
|
|
curTag = yTagString
|
|
|
|
case curTag
|
|
|
|
of yTagString:
|
|
|
|
var s: string
|
|
|
|
events.constructChild(context, s)
|
|
|
|
echo "got string: ", s
|
|
|
|
of yTagInteger:
|
|
|
|
var i: int32
|
|
|
|
events.constructChild(context, i)
|
|
|
|
echo "got integer: ", i
|
|
|
|
of yTagBoolean:
|
|
|
|
var b: bool
|
|
|
|
events.constructChild(context, b)
|
|
|
|
echo "got boolean: ", b
|
|
|
|
of yTagPerson:
|
|
|
|
var p: Person
|
|
|
|
events.constructChild(context, p)
|
|
|
|
echo "got Person with name: ", p.name
|
|
|
|
else: assert false, "unsupported tag: " & $curTag
|
|
|
|
nextEvent = events.peek()
|
|
|
|
assert events.next().kind == yamlEndSeq
|
|
|
|
assert events.next().kind == yamlEndDoc
|
|
|
|
assert events.finished()
|
|
|
|
s.close()
|