Handle the writing aspects of custom field serialization

This commit is contained in:
Zahary Karadjov 2019-08-07 01:20:27 +03:00
parent 9a8d593598
commit f6a7da52e0
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
1 changed files with 7 additions and 3 deletions

View File

@ -125,7 +125,7 @@ proc writeArray[T](w: var JsonWriter, elements: openarray[T]) =
writeIterable(w, elements)
proc writeValue*(w: var JsonWriter, value: auto) =
mixin enumInstanceSerializedFields, writeValue
mixin enumInstanceSerializedFields, writeValue, writeFieldIMPL
template addChar(c) =
append c
@ -185,8 +185,12 @@ proc writeValue*(w: var JsonWriter, value: auto) =
w.writeArray(value)
elif value is (object or tuple):
w.beginRecord(type(value))
value.enumInstanceSerializedFields(k, v):
w.writeField k, v
type RecordType = type value
value.enumInstanceSerializedFields(fieldName, field):
type FieldType = type field
w.writeFieldName(fieldName)
w.writeFieldIMPL(FieldTag[RecordType, fieldName, FieldType], field, value)
w.state = AfterField
w.endRecord()
else:
const typeName = typetraits.name(value.type)