Serialization: Correctly encode structs with no fields

Empty structs were being serialized as `null`, when they should have been serialized as `{}`.

This was due to the type inference on the serializer - where no calls to `field()` would result in the default `null` type.

To solve this, the `serialize(const void* object, const std::initializer_list<Field>&)` inline helper has been promoted to a virtual function (and renamed to `fields()`).
The JSON serializer implementation of this now first sets the object type to `object`, even if there are no fields to serialize.

Added test for this.

Fixes: #10
This commit is contained in:
Ben Clayton
2020-01-05 09:25:43 +00:00
parent 3a10d4cabd
commit cdc19ac4d9
5 changed files with 30 additions and 22 deletions

View File

@@ -164,7 +164,7 @@ M member_type(M T::*);
return d->deserialize(ptr, {__VA_ARGS__}); \
} \
bool serialize(Serializer* s, const void* ptr) const override { \
return s->serialize(ptr, {__VA_ARGS__}); \
return s->fields(ptr, {__VA_ARGS__}); \
} \
}; \
static TI typeinfo; \