Avoid overloading the separate classes of writeField ops
This commit is contained in:
parent
95399caff3
commit
bedbe6595a
|
@ -1,14 +1,14 @@
|
|||
import std/options, ../../json_serialization/[reader, writer, lexer]
|
||||
export options
|
||||
|
||||
template writeField*(w: var JsonWriter,
|
||||
template writeObjectField*(w: var JsonWriter,
|
||||
record: auto,
|
||||
fieldName: static string,
|
||||
field: Option) =
|
||||
mixin writeField
|
||||
mixin writeObjectField
|
||||
|
||||
if field.isSome:
|
||||
writeField(w, record, fieldName, field.get)
|
||||
writeObjectField(w, record, fieldName, field.get)
|
||||
|
||||
proc writeValue*(writer: var JsonWriter, value: Option) =
|
||||
mixin writeValue
|
||||
|
|
|
@ -4,20 +4,26 @@ import
|
|||
export
|
||||
results
|
||||
|
||||
template writeField*[T](w: var JsonWriter,
|
||||
template writeObjectField*[T](w: var JsonWriter,
|
||||
record: auto,
|
||||
fieldName: static string,
|
||||
field: Result[T, void]) =
|
||||
mixin writeObjectField
|
||||
|
||||
if field.isOk:
|
||||
writeField(w, record, fieldName, field.get)
|
||||
writeObjectField(w, record, fieldName, field.get)
|
||||
|
||||
proc writeValue*[T](writer: var JsonWriter, value: Result[T, void]) =
|
||||
mixin writeValue
|
||||
|
||||
if value.isOk:
|
||||
writer.writeValue value.get
|
||||
else:
|
||||
writer.writeValue JsonString("null")
|
||||
|
||||
proc readValue*[T](reader: var JsonReader, value: var Result[T, void]) =
|
||||
mixin readValue
|
||||
|
||||
let tok = reader.lexer.lazyTok
|
||||
if tok == tkNull:
|
||||
reset value
|
||||
|
|
|
@ -145,7 +145,7 @@ template isStringLike(v: string|cstring|openArray[char]|seq[char]): bool = true
|
|||
template isStringLike[N](v: array[N, char]): bool = true
|
||||
template isStringLike(v: auto): bool = false
|
||||
|
||||
template writeField*[FieldType, RecordType](w: var JsonWriter,
|
||||
template writeObjectField*[FieldType, RecordType](w: var JsonWriter,
|
||||
record: RecordType,
|
||||
fieldName: static string,
|
||||
field: FieldType) =
|
||||
|
@ -237,8 +237,8 @@ proc writeValue*(w: var JsonWriter, value: auto) =
|
|||
type RecordType = type value
|
||||
w.beginRecord RecordType
|
||||
value.enumInstanceSerializedFields(fieldName, field):
|
||||
mixin writeField
|
||||
writeField(w, value, fieldName, field)
|
||||
mixin writeObjectField
|
||||
writeObjectField(w, value, fieldName, field)
|
||||
w.state = AfterField
|
||||
w.endRecord()
|
||||
|
||||
|
|
Loading…
Reference in New Issue