🟨 Serialize to / from text formats (JSON) using Reflection
Serialization Text uses Reflection, to serialize C++ objects to text based formats that currently includes a JSON serializer / deserializer.
T[N]
arrays🟨 MVP
Only JSON has been implemented and it needs additional testing.
SC::SerializationJson reads or writes C++ structures to / from json using Reflection information.
Let's consider the following structure described by Reflection:
This is how you can serialize the class to JSON
This is how you can de-serialize the class from JSON, matching fields by their label name, even if they come in different order than the original class or even if there are missing field.
This is a special loader to deserialize the class from the exact same JSON that was output by the serializer itself. Whitespace changes are fine, but changing the order in which two fields exists or removing one will make deserialization fail. If these limitations are fine for the usage (for example the generated json files are not meant to be manually edited by users) than maybe it could be worth using it, as this code path is a lot simpler (*) than SC::SerializationJson::loadVersioned.
SC::detail::SerializationTextReadVersioned
provides common framework for all text / structured formats, walking the data structure using reflection information.
Every writer or reader needs to implement methods like startObject
/ endObject
and startArray
/ endArray
, so that only a minimal amount of work is needed to support a new output format.
So far only JSON format has been implemented, but it would be easily possible adding XML or other formats like YAML if needed.
Also this serializer has an exact and a versioned variant.
The exact json deserializer (SC::detail::SerializationTextReadWriteExact
) must receive as input a file with fields in the exact same order as output by the json writer, so it makes it a little bit inflexible but maybe it could provide some performance boost as it's clearly doing less work (even if it should always be measured to verify it's actually faster in the specific use case).
SC::JsonTokenizer
verifies the correctness of JSON elements but it will not validate Strings or parse numbers. The design is a stateful streaming tokenizer, so it can ingest documents of arbitrary size. It allocates just a single enum value for every nesting level of json objects. This small allocation can be controlled by the caller through a SC::Vector. The allocation can be eliminated by passing a SC::SmallVector with an bounded level of json nesting. The design of being a tokenizer implies that we are not building any tree / hierarchy / DOM, even if it can be trivially done by just pushing the outputs of the tokenizer into a hierarchical data structure.
🟩 Usable
🟦 Complete Features:
💡 Unplanned Features: