Renamed serialize() to represent() to better conform to YAML spec

This commit is contained in:
Felix Krause 2016-02-02 18:19:40 +01:00
parent e89a9a5b14
commit 32c42054f7
3 changed files with 40 additions and 41 deletions

View File

@ -45,7 +45,7 @@ suite "Serialization":
assert result[0] == "a" assert result[0] == "a"
assert result[1] == "b" assert result[1] == "b"
test "Serialization: Serialize string sequence": test "Serialization: Represent string sequence":
var input = @["a", "b"] var input = @["a", "b"]
var output = newStringStream() var output = newStringStream()
dump(input, output, psBlockOnly, tsNone) dump(input, output, psBlockOnly, tsNone)
@ -62,7 +62,7 @@ suite "Serialization":
assert result[23] == "dreiundzwanzig" assert result[23] == "dreiundzwanzig"
assert result[42] == "zweiundvierzig" assert result[42] == "zweiundvierzig"
test "Serialization: Serialize Table[int, string]": test "Serialization: Represent Table[int, string]":
var input = initTable[int32, string]() var input = initTable[int32, string]()
input[23] = "dreiundzwanzig" input[23] = "dreiundzwanzig"
input[42] = "zweiundvierzig" input[42] = "zweiundvierzig"
@ -84,7 +84,7 @@ suite "Serialization":
assert result[1] == @[4.int32, 5.int32] assert result[1] == @[4.int32, 5.int32]
assert result[2] == @[6.int32] assert result[2] == @[6.int32]
test "Serialization: Serialize Sequences in Sequence": test "Serialization: Represent Sequences in Sequence":
let input = @[@[1.int32, 2.int32, 3.int32], @[4.int32, 5.int32], let input = @[@[1.int32, 2.int32, 3.int32], @[4.int32, 5.int32],
@[6.int32]] @[6.int32]]
var output = newStringStream() var output = newStringStream()
@ -104,7 +104,7 @@ suite "Serialization":
assert result[1] == tlGreen assert result[1] == tlGreen
assert result[2] == tlYellow assert result[2] == tlYellow
test "Serialization: Serialize Enum": test "Serialization: Represent Enum":
let input = @[tlRed, tlGreen, tlYellow] let input = @[tlRed, tlGreen, tlYellow]
var output = newStringStream() var output = newStringStream()
dump(input, output, psBlockOnly, tsNone) dump(input, output, psBlockOnly, tsNone)
@ -122,7 +122,7 @@ suite "Serialization":
assert result.i == 42 assert result.i == 42
assert result.b == true assert result.b == true
test "Serialization: Serialize Tuple": test "Serialization: Represent Tuple":
let input = (str: "value", i: 42.int32, b: true) let input = (str: "value", i: 42.int32, b: true)
var output = newStringStream() var output = newStringStream()
dump(input, output, psDefault, tsNone) dump(input, output, psDefault, tsNone)
@ -140,7 +140,7 @@ suite "Serialization":
assert result.surname == "Pan" assert result.surname == "Pan"
assert result.age == 12 assert result.age == 12
test "Serialization: Serialize custom object": test "Serialization: Represent custom object":
let input = Person(firstname: "Peter", surname: "Pan", age: 12) let input = Person(firstname: "Peter", surname: "Pan", age: 12)
var output = newStringStream() var output = newStringStream()
dump(input, output, psBlockOnly, tsNone) dump(input, output, psBlockOnly, tsNone)
@ -159,7 +159,7 @@ suite "Serialization":
assert result[0] == "one" assert result[0] == "one"
assert result[1] == "two" assert result[1] == "two"
test "Serialization: Serialize sequence with explicit tags": test "Serialization: Represent sequence with explicit tags":
let input = @["one", "two"] let input = @["one", "two"]
var output = newStringStream() var output = newStringStream()
dump(input, output, psBlockOnly, tsAll) dump(input, output, psBlockOnly, tsAll)
@ -179,7 +179,7 @@ suite "Serialization":
assert result.surname == "Pan" assert result.surname == "Pan"
assert result.age == 12 assert result.age == 12
test "Serialization: Serialize custom object with explicit root tag": test "Serialization: Represent custom object with explicit root tag":
let input = Person(firstname: "Peter", surname: "Pan", age: 12) let input = Person(firstname: "Peter", surname: "Pan", age: 12)
var output = newStringStream() var output = newStringStream()
dump(input, output, psBlockOnly, tsRootOnly) dump(input, output, psBlockOnly, tsRootOnly)
@ -187,7 +187,7 @@ suite "Serialization":
"--- !nim:custom:Person \nfirstname: Peter\nsurname: Pan\nage: 12", "--- !nim:custom:Person \nfirstname: Peter\nsurname: Pan\nage: 12",
output.data) output.data)
test "Serialization: Serialize cyclic data structure": test "Serialization: Represent cyclic data structure":
var var
a = newNode("a") a = newNode("a")
b = newNode("b") b = newNode("b")

View File

@ -13,8 +13,7 @@
## large data structures. ## large data structures.
## ##
## As YAML is a strict superset of `JSON <http://json.org>`_, JSON input is ## As YAML is a strict superset of `JSON <http://json.org>`_, JSON input is
## automatically supported. Additionally, there is functionality available to ## automatically supported. While JSON is less readable than YAML,
## convert any YAML stream into JSON. While JSON is less readable than YAML,
## this enhances interoperability with other languages. ## this enhances interoperability with other languages.
import streams, unicode, lexbase, tables, strutils, json, hashes, queues, macros import streams, unicode, lexbase, tables, strutils, json, hashes, queues, macros

View File

@ -210,16 +210,16 @@ macro serializable*(types: stmt): stmt =
constructProc[6] = impl constructProc[6] = impl
result.add(constructProc) result.add(constructProc)
# serializeObject() # representObject()
var serializeProc = newProc(newIdentNode("serializeObject"), [ var representProc = newProc(newIdentNode("representObject"), [
newIdentNode("YamlStream"), newIdentNode("YamlStream"),
newIdentDefs(newIdentNode("value"), tIdent), newIdentDefs(newIdentNode("value"), tIdent),
newIdentDefs(newIdentNode("ts"), newIdentDefs(newIdentNode("ts"),
newIdentNode("TagStyle")), newIdentNode("TagStyle")),
newIdentDefs(newIdentNode("c"), newIdentDefs(newIdentNode("c"),
newIdentNode("SerializationContext"))]) newIdentNode("SerializationContext"))])
serializeProc[4] = newNimNode(nnkPragma).add( representProc[4] = newNimNode(nnkPragma).add(
newNimNode(nnkExprColonExpr).add(newIdentNode("raises"), newNimNode(nnkExprColonExpr).add(newIdentNode("raises"),
newNimNode(nnkBracket))) newNimNode(nnkBracket)))
var iterBody = newStmtList( var iterBody = newStmtList(
@ -264,7 +264,7 @@ macro serializable*(types: stmt): stmt =
scalarContent: `fieldNameString`) scalarContent: `fieldNameString`)
) )
iterbody.insert(i + 1, newVarStmt(fieldIterIdent, iterbody.insert(i + 1, newVarStmt(fieldIterIdent,
newCall("serializeObject", newDotExpr(newIdentNode("value"), newCall("representObject", newDotExpr(newIdentNode("value"),
field.name), newIdentNode("childTagStyle"), field.name), newIdentNode("childTagStyle"),
newIdentNode("c")))) newIdentNode("c"))))
iterbody.insert(i + 2, quote do: iterbody.insert(i + 2, quote do:
@ -275,8 +275,8 @@ macro serializable*(types: stmt): stmt =
impl = newStmtList(newAssignment(newIdentNode("result"), newProc( impl = newStmtList(newAssignment(newIdentNode("result"), newProc(
newEmptyNode(), [newIdentNode("YamlStreamEvent")], iterBody, newEmptyNode(), [newIdentNode("YamlStreamEvent")], iterBody,
nnkIteratorDef))) nnkIteratorDef)))
serializeProc[6] = impl representProc[6] = impl
result.add(serializeProc) result.add(representProc)
proc prepend(event: YamlStreamEvent, s: YamlStream): YamlStream {.raises: [].} = proc prepend(event: YamlStreamEvent, s: YamlStream): YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
@ -337,7 +337,7 @@ proc constructObject*(s: YamlStream, c: ConstructionContext, result: var string)
constructScalarItem(item, "string", yTagString): constructScalarItem(item, "string", yTagString):
result = item.scalarContent result = item.scalarContent
proc serializeObject*(value: string, ts: TagStyle = tsNone, proc representObject*(value: string, ts: TagStyle = tsNone,
c: SerializationContext): YamlStream {.raises: [].} = c: SerializationContext): YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
yield scalarEvent(value, presentTag(string, ts), yAnchorNone) yield scalarEvent(value, presentTag(string, ts), yAnchorNone)
@ -359,13 +359,13 @@ template constructObject*(s: YamlStream, c: ConstructionContext,
{.fatal: "The length of `int` is platform dependent. Use int[8|16|32|64].".} {.fatal: "The length of `int` is platform dependent. Use int[8|16|32|64].".}
discard discard
proc serializeObject*[T: int8|int16|int32|int64]( proc representObject*[T: int8|int16|int32|int64](
value: T, ts: TagStyle = tsNone, c: SerializationContext): value: T, ts: TagStyle = tsNone, c: SerializationContext):
YamlStream {.raises: [].} = YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
yield scalarEvent($value, presentTag(T, ts), yAnchorNone) yield scalarEvent($value, presentTag(T, ts), yAnchorNone)
template serializeObject*(value: int, tagStyle: TagStyle, template representObject*(value: int, tagStyle: TagStyle,
c: SerializationContext) = c: SerializationContext) =
{.fatal: "The length of `int` is platform dependent. Use int[8|16|32|64].".} {.fatal: "The length of `int` is platform dependent. Use int[8|16|32|64].".}
discard discard
@ -400,13 +400,13 @@ template constructObject*(s: YamlStream, c: ConstructionContext,
"The length of `uint` is platform dependent. Use uint[8|16|32|64].".} "The length of `uint` is platform dependent. Use uint[8|16|32|64].".}
discard discard
proc serializeObject*[T: uint8|uint16|uint32|uint64]( proc representObject*[T: uint8|uint16|uint32|uint64](
value: T, ts: TagStyle, c: SerializationContext): value: T, ts: TagStyle, c: SerializationContext):
YamlStream {.raises: [].} = YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
yield scalarEvent($value, presentTag(T, ts), yAnchorNone) yield scalarEvent($value, presentTag(T, ts), yAnchorNone)
template serializeObject*(value: uint, ts: TagStyle, c: SerializationContext) = template representObject*(value: uint, ts: TagStyle, c: SerializationContext) =
{.fatal: {.fatal:
"The length of `uint` is platform dependent. Use uint[8|16|32|64].".} "The length of `uint` is platform dependent. Use uint[8|16|32|64].".}
discard discard
@ -440,7 +440,7 @@ template constructObject*(s: YamlStream, c: ConstructionContext,
result: var float) = result: var float) =
{.fatal: "The length of `float` is platform dependent. Use float[32|64].".} {.fatal: "The length of `float` is platform dependent. Use float[32|64].".}
proc serializeObject*[T: float32|float64](value: T, ts: TagStyle, proc representObject*[T: float32|float64](value: T, ts: TagStyle,
c: SerializationContext): c: SerializationContext):
YamlStream {.raises: [].} = YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
@ -457,7 +457,7 @@ proc serializeObject*[T: float32|float64](value: T, ts: TagStyle,
asString = $value asString = $value
yield scalarEvent(asString, presentTag(T, ts), yAnchorNone) yield scalarEvent(asString, presentTag(T, ts), yAnchorNone)
template serializeObject*(value: float, tagStyle: TagStyle, template representObject*(value: float, tagStyle: TagStyle,
c: SerializationContext) = c: SerializationContext) =
{.fatal: "The length of `float` is platform dependent. Use float[32|64].".} {.fatal: "The length of `float` is platform dependent. Use float[32|64].".}
@ -476,7 +476,7 @@ proc constructObject*(s: YamlStream, c: ConstructionContext, result: var bool)
raise newException(YamlConstructionError, raise newException(YamlConstructionError,
"Cannot construct to bool: " & item.scalarContent) "Cannot construct to bool: " & item.scalarContent)
proc serializeObject*(value: bool, ts: TagStyle, proc representObject*(value: bool, ts: TagStyle,
c: SerializationContext): YamlStream {.raises: [].} = c: SerializationContext): YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
yield scalarEvent(if value: "y" else: "n", presentTag(bool, ts), yield scalarEvent(if value: "y" else: "n", presentTag(bool, ts),
@ -495,7 +495,7 @@ proc constructObject*(s: YamlStream, c: ConstructionContext, result: var char)
else: else:
result = item.scalarContent[0] result = item.scalarContent[0]
proc serializeObject*(value: char, ts: TagStyle, proc representObject*(value: char, ts: TagStyle,
c: SerializationContext): YamlStream {.raises: [].} = c: SerializationContext): YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
yield scalarEvent("" & value, presentTag(char, ts), yAnchorNone) yield scalarEvent("" & value, presentTag(char, ts), yAnchorNone)
@ -531,7 +531,7 @@ proc constructObject*[T](s: YamlStream, c: ConstructionContext,
safeNextEvent(event, s) safeNextEvent(event, s)
assert(not finished(s)) assert(not finished(s))
proc serializeObject*[T](value: seq[T], ts: TagStyle, proc representObject*[T](value: seq[T], ts: TagStyle,
c: SerializationContext): YamlStream {.raises: [].} = c: SerializationContext): YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
let childTagStyle = if ts == tsRootOnly: tsNone else: ts let childTagStyle = if ts == tsRootOnly: tsNone else: ts
@ -539,7 +539,7 @@ proc serializeObject*[T](value: seq[T], ts: TagStyle,
seqTag: presentTag(seq[T], ts), seqTag: presentTag(seq[T], ts),
seqAnchor: yAnchorNone) seqAnchor: yAnchorNone)
for item in value: for item in value:
var events = serializeObject(item, childTagStyle, c) var events = representObject(item, childTagStyle, c)
for event in events(): for event in events():
yield event yield event
yield YamlStreamEvent(kind: yamlEndSequence) yield YamlStreamEvent(kind: yamlEndSequence)
@ -589,7 +589,7 @@ proc constructObject*[K, V](s: YamlStream, c: ConstructionContext,
safeNextEvent(event, s) safeNextEvent(event, s)
assert(not finished(s)) assert(not finished(s))
proc serializeObject*[K, V](value: Table[K, V], ts: TagStyle, proc representObject*[K, V](value: Table[K, V], ts: TagStyle,
c: SerializationContext): YamlStream {.raises:[].} = c: SerializationContext): YamlStream {.raises:[].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
let childTagStyle = if ts == tsRootOnly: tsNone else: ts let childTagStyle = if ts == tsRootOnly: tsNone else: ts
@ -597,10 +597,10 @@ proc serializeObject*[K, V](value: Table[K, V], ts: TagStyle,
mapTag: presentTag(Table[K, V], ts), mapTag: presentTag(Table[K, V], ts),
mapAnchor: yAnchorNone) mapAnchor: yAnchorNone)
for key, value in value.pairs: for key, value in value.pairs:
var events = serializeObject(key, childTagStyle, c) var events = representObject(key, childTagStyle, c)
for event in events(): for event in events():
yield event yield event
events = serializeObject(value, childTagStyle, c) events = representObject(value, childTagStyle, c)
for event in events(): for event in events():
yield event yield event
yield YamlStreamEvent(kind: yamlEndMap) yield YamlStreamEvent(kind: yamlEndMap)
@ -649,7 +649,7 @@ proc constructObject*[O: object|tuple](s: YamlStream, c: ConstructionContext,
e = s() e = s()
assert(not finished(s)) assert(not finished(s))
proc serializeObject*[O: object|tuple](value: O, ts: TagStyle, proc representObject*[O: object|tuple](value: O, ts: TagStyle,
c: SerializationContext): c: SerializationContext):
YamlStream {.raises: [].} = YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
@ -658,7 +658,7 @@ proc serializeObject*[O: object|tuple](value: O, ts: TagStyle,
for name, value in fieldPairs(value): for name, value in fieldPairs(value):
yield scalarEvent(name, presentTag(string, childTagStyle), yield scalarEvent(name, presentTag(string, childTagStyle),
yAnchorNone) yAnchorNone)
var events = serializeObject(value, childTagStyle, c) var events = representObject(value, childTagStyle, c)
for event in events(): for event in events():
yield event yield event
yield endMapEvent() yield endMapEvent()
@ -678,7 +678,7 @@ proc constructObject*[O: enum](s: YamlStream, c: ConstructionContext,
ex.parent = getCurrentException() ex.parent = getCurrentException()
raise ex raise ex
proc serializeObject*[O: enum](value: O, ts: TagStyle, proc representObject*[O: enum](value: O, ts: TagStyle,
c: SerializationContext): c: SerializationContext):
YamlStream {.raises: [].} = YamlStream {.raises: [].} =
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
@ -724,12 +724,12 @@ proc constructObject*[O](s: YamlStream, c: ConstructionContext,
e.parent = getCurrentException() e.parent = getCurrentException()
raise e raise e
proc serializeObject*[O](value: ref O, ts: TagStyle, c: SerializationContext): proc representObject*[O](value: ref O, ts: TagStyle, c: SerializationContext):
YamlStream {.raises: [].} = YamlStream {.raises: [].} =
if value == nil: if value == nil:
result = iterator(): YamlStreamEvent = yield scalarEvent("~", yTagNull) result = iterator(): YamlStreamEvent = yield scalarEvent("~", yTagNull)
elif c.style == asNone: elif c.style == asNone:
result = serializeObject(value[], ts, c) result = representObject(value[], ts, c)
else: else:
let let
p = cast[pointer](value) p = cast[pointer](value)
@ -745,7 +745,7 @@ proc serializeObject*[O](value: ref O, ts: TagStyle, c: SerializationContext):
cast[AnchorId](p) cast[AnchorId](p)
try: try:
var var
objStream = serializeObject(value[], ts, c) objStream = representObject(value[], ts, c)
first = objStream() first = objStream()
assert(not finished(objStream)) assert(not finished(objStream))
case first.kind case first.kind
@ -842,13 +842,13 @@ proc insideDoc(s: YamlStream): YamlStream {.raises: [].} =
yield event yield event
yield YamlStreamEvent(kind: yamlEndDocument) yield YamlStreamEvent(kind: yamlEndDocument)
proc serialize*[T](value: T, ts: TagStyle = tsRootOnly, proc represent*[T](value: T, ts: TagStyle = tsRootOnly,
a: AnchorStyle = asTidy): YamlStream {.raises: [].} = a: AnchorStyle = asTidy): YamlStream {.raises: [].} =
var var
context = newSerializationContext(a) context = newSerializationContext(a)
objStream: YamlStream objStream: YamlStream
try: try:
objStream = insideDoc(serializeObject(value, ts, context)) objStream = insideDoc(representObject(value, ts, context))
except Exception: except Exception:
assert(false) assert(false)
if a == asTidy: if a == asTidy:
@ -881,7 +881,7 @@ proc dump*[K](value: K, target: Stream, style: PresentationStyle = psDefault,
tagStyle: TagStyle = tsRootOnly, tagStyle: TagStyle = tsRootOnly,
anchorStyle: AnchorStyle = asTidy, indentationStep: int = 2) anchorStyle: AnchorStyle = asTidy, indentationStep: int = 2)
{.raises: [YamlPresenterJsonError, YamlPresenterOutputError].} = {.raises: [YamlPresenterJsonError, YamlPresenterOutputError].} =
var events = serialize(value, if style == psCanonical: tsAll else: tagStyle, var events = represent(value, if style == psCanonical: tsAll else: tagStyle,
if style == psJson: asNone else: anchorStyle) if style == psJson: asNone else: anchorStyle)
try: try:
present(events, target, serializationTagLibrary, style, indentationStep) present(events, target, serializationTagLibrary, style, indentationStep)
@ -894,5 +894,5 @@ proc dump*[K](value: K, target: Stream, style: PresentationStyle = psDefault,
except YamlPresenterJsonError, YamlPresenterOutputError, AssertionError: except YamlPresenterJsonError, YamlPresenterOutputError, AssertionError:
raise raise
except Exception: except Exception:
# cannot occur as serialize() doesn't raise any errors # cannot occur as represent() doesn't raise any errors
assert(false) assert(false)