mirror of
https://github.com/status-im/NimYAML.git
synced 2025-02-04 15:33:56 +00:00
Change serialization to omit none(T)
fields of sparse objects (#100)
* Change serialization to omit `none(T)` fields of sparse objects Co-authored-by: quantimnot <quantimnot@users.noreply.github.com>
This commit is contained in:
parent
d704a8c6d7
commit
75ed8e5be4
@ -27,7 +27,8 @@ template sparse*() {.pragma.}
|
|||||||
## This annotation can be put on an object type. During deserialization,
|
## This annotation can be put on an object type. During deserialization,
|
||||||
## the input may omit any field that has an ``Option[T]`` type (for any
|
## the input may omit any field that has an ``Option[T]`` type (for any
|
||||||
## concrete ``T``) and that field will be treated as if it had the annotation
|
## concrete ``T``) and that field will be treated as if it had the annotation
|
||||||
## ``{.defaultVal: none(T).}``.
|
## ``{.defaultVal: none(T).}``. Fields of ``none(T)`` value are omitted
|
||||||
|
## during serialization.
|
||||||
##
|
##
|
||||||
## Example usage:
|
## Example usage:
|
||||||
##
|
##
|
||||||
@ -80,4 +81,4 @@ template implicit*() {.pragma.}
|
|||||||
## of ckString:
|
## of ckString:
|
||||||
## strVal: string
|
## strVal: string
|
||||||
## of ckInt:
|
## of ckInt:
|
||||||
## intVal: int
|
## intVal: int
|
||||||
|
@ -967,12 +967,17 @@ macro genRepresentObject(t: typedesc, value, childTagStyle: typed) =
|
|||||||
name = $child
|
name = $child
|
||||||
childAccessor = newDotExpr(value, newIdentNode(name))
|
childAccessor = newDotExpr(value, newIdentNode(name))
|
||||||
result.add(quote do:
|
result.add(quote do:
|
||||||
when not `childAccessor`.hasCustomPragma(transient):
|
template serializeImpl =
|
||||||
when bool(`isVO`): c.put(startMapEvent())
|
when bool(`isVO`): c.put(startMapEvent())
|
||||||
c.put(scalarEvent(`name`, if `childTagStyle` == tsNone:
|
c.put(scalarEvent(`name`, if `childTagStyle` == tsNone:
|
||||||
yTagQuestionMark else: yTagNimField, yAnchorNone))
|
yTagQuestionMark else: yTagNimField, yAnchorNone))
|
||||||
representChild(`childAccessor`, `childTagStyle`, c)
|
representChild(`childAccessor`, `childTagStyle`, c)
|
||||||
when bool(`isVO`): c.put(endMapEvent())
|
when bool(`isVO`): c.put(endMapEvent())
|
||||||
|
when not `childAccessor`.hasCustomPragma(transient):
|
||||||
|
when hasSparse(`t`) and `child` is Option:
|
||||||
|
if `childAccessor`.isSome: serializeImpl()
|
||||||
|
else:
|
||||||
|
serializeImpl()
|
||||||
)
|
)
|
||||||
inc(fieldIndex)
|
inc(fieldIndex)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user