mirror of https://github.com/status-im/NimYAML.git
Updated serialization documentation
This commit is contained in:
parent
f4f1817180
commit
650b858861
|
@ -317,21 +317,20 @@ representObject
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
proc representObject*(value: MyObject, ts: TagStyle = tsNone,
|
proc representObject*(value: MyObject, ts: TagStyle = tsNone,
|
||||||
c: SerializationContext): RawYamlStream {.raises: [].}
|
c: SerializationContext, tag: TagId): {.raises: [].}
|
||||||
|
|
||||||
This proc should return an iterator over ``YamlStreamEvent`` which represents
|
This proc should push a list of tokens that represent the type into the
|
||||||
the type. Follow the following guidelines when implementing a custom
|
serialization context via ``c.put``. Follow the following guidelines when
|
||||||
``representObject`` proc:
|
implementing a custom ``representObject`` proc:
|
||||||
|
|
||||||
- You can use the helper template
|
- You can use the helper template
|
||||||
`presentTag <yaml.html#presentTag,typedesc,TagStyle>`_ for outputting the
|
`presentTag <yaml.html#presentTag,typedesc,TagStyle>`_ for outputting the
|
||||||
tag.
|
tag.
|
||||||
- Always output the first tag with a ``yAnchorNone``. Anchors will be set
|
- Always output the first token with a ``yAnchorNone``. Anchors will be set
|
||||||
automatically by ``ref`` type handling.
|
automatically by ``ref`` type handling.
|
||||||
- When outputting non-scalar types, you should use the ``representObject``
|
- When outputting non-scalar types, you should use the ``representObject``
|
||||||
implementation of the child types, if possible.
|
implementation of the child types, if possible.
|
||||||
- Check if the given ``TagStyle`` equals ``tsRootOnly`` and if yes, change it
|
- Always use the ``tag`` parameter as tag for the first token you generate.
|
||||||
to ``tsNone`` for the child values.
|
|
||||||
- Never write a ``representObject`` proc for ``ref`` types.
|
- Never write a ``representObject`` proc for ``ref`` types.
|
||||||
|
|
||||||
The following example for representing to a YAML scalar is the actual
|
The following example for representing to a YAML scalar is the actual
|
||||||
|
@ -339,28 +338,21 @@ implementation of representing ``int`` types:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
proc representObject*[T: uint8|uint16|uint32|uint64](
|
proc representObject*[T: int8|int16|int32|int64](value: T, ts: TagStyle,
|
||||||
value: T, ts: TagStyle, c: SerializationContext):
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
||||||
RawYamlStream {.raises: [].} =
|
## represents an integer value as YAML scalar
|
||||||
result = iterator(): YamlStreamEvent =
|
c.put(scalarEvent($value, tag, yAnchorNone))
|
||||||
yield scalarEvent($value, presentTag(T, ts), yAnchorNone)
|
|
||||||
|
|
||||||
The following example for representing to a YAML non-scalar is the actual
|
The following example for representing to a YAML non-scalar is the actual
|
||||||
implementation of representing ``seq`` types:
|
implementation of representing ``seq`` and ``set`` types:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
proc representObject*[T](value: seq[T], ts: TagStyle,
|
proc representObject*[T](value: seq[T]|set[T], ts: TagStyle,
|
||||||
c: SerializationContext): RawYamlStream {.raises: [].} =
|
c: SerializationContext, tag: TagId) {.raises: [YamlStreamError].} =
|
||||||
result = iterator(): YamlStreamEvent =
|
## represents a Nim seq as YAML sequence
|
||||||
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
let childTagStyle = if ts == tsRootOnly: tsNone else: ts
|
||||||
yield YamlStreamEvent(kind: yamlStartSequence,
|
c.put(startSeqEvent(tag))
|
||||||
seqTag: presentTag(seq[T], ts),
|
for item in value:
|
||||||
seqAnchor: yAnchorNone)
|
representChild(item, childTagStyle, c)
|
||||||
for item in value:
|
c.put(endSeqEvent())
|
||||||
var events = representObject(item, childTagStyle, c)
|
|
||||||
while true:
|
|
||||||
let event = events()
|
|
||||||
if finished(events): break
|
|
||||||
yield event
|
|
||||||
yield YamlStreamEvent(kind: yamlEndSequence)
|
|
Loading…
Reference in New Issue