Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::SerializationJson Struct Reference

SC::SerializationJson reads or writes C++ structures to / from json using Reflection information. More...

#include <SerializationJson.h>

Classes

struct  Options
 Formatting options. More...
 

Static Public Member Functions

template<typename T >
static bool write (T &object, StringFormatOutput &output, Options options=Options())
 Writes a C++ object to JSON using Reflection. More...
 
template<typename T >
static bool loadExact (T &object, StringView text)
 Parses a JSON produced by SerializationJson::write loading its values into a C++ object Read a JSON buffer as output by SerializationJson::write, that means mainly not changing the relative order of struct fields in the json text. More...
 
template<typename T >
static bool loadVersioned (T &object, StringView text)
 Parses a JSON buffer and writes C++ objects supporting reordered or missing fields. More...
 

Detailed Description

SC::SerializationJson reads or writes C++ structures to / from json using Reflection information.


Let's consider the following structure described by Reflection:

struct SC::Test
{
int x = 2;
float y = 1.5f;
int xy[2] = {1, 3};
String myTest = "asdf"_a8;
Vector<String> myVector = {"Str1"_a8, "Str2"_a8};
bool operator==(const Test& other) const
{
return x == other.x and y == other.y and //
xy[0] == other.xy[0] and xy[1] == other.xy[1] and //
myTest == other.myTest and myVector.size() == 2 and //
myVector.size() == other.myVector.size() and //
myVector[0] == other.myVector[0] and myVector[1] == other.myVector[1];
}
};
SC_REFLECT_STRUCT_VISIT(SC::Test)
SC_REFLECT_STRUCT_FIELD(0, x)
SC_REFLECT_STRUCT_FIELD(1, y)
SC_REFLECT_STRUCT_FIELD(2, xy)
SC_REFLECT_STRUCT_FIELD(3, myTest)
SC_REFLECT_STRUCT_FIELD(4, myVector)
SC_REFLECT_STRUCT_LEAVE()

This is how you can serialize the class to JSON

constexpr StringView testJSON = R"({"x":2,"y":1.50,"xy":[1,3],"myTest":"asdf","myVector":["Str1","Str2"]})"_a8;
Test test;
SmallVector<char, 256> buffer;
StringFormatOutput output(StringEncoding::Ascii, buffer);
const StringView serializedJSON({buffer.data(), buffer.size() - 1}, false, StringEncoding::Ascii);
SC_TEST_EXPECT(serializedJSON == testJSON);
@ Ascii
Encoding is ASCII.
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113
static bool write(T &object, StringFormatOutput &output, Options options=Options())
Writes a C++ object to JSON using Reflection.
Definition: SerializationJson.h:63

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.

constexpr StringView scrambledJson =
R"({"y" : 1.50, "x": 2.0, "myVector" : ["Str1","Str2"], "myTest":"asdf"})"_a8;
Test test;
test.x = 0;
test.y = 0;
(void)test.myVector.resize(1);
(void)test.myTest.assign("FDFSA"_a8);
SC_TEST_EXPECT(test == Test());
static bool loadVersioned(T &object, StringView text)
Parses a JSON buffer and writes C++ objects supporting reordered or missing fields.
Definition: SerializationJson.h:96

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.

constexpr StringView testJSON = R"({"x":2,"y":1.50,"xy":[1,3],"myTest":"asdf","myVector":["Str1","Str2"]})"_a8;
Test test;
test.x = 1;
test.y = 3.22f;
test.xy[0] = 4;
test.xy[1] = 4;
test.myTest = "KFDOK";
test.myVector = {"LPDFSOK", "DSAFKO"};
SC_TEST_EXPECT(test == Test());
static bool loadExact(T &object, StringView text)
Parses a JSON produced by SerializationJson::write loading its values into a C++ object Read a JSON b...
Definition: SerializationJson.h:83
Note
(*) simpler code probably means faster code, even if it has not been properly benchmarked yet so the hypothetical performance gain is yet to be defined.

Member Function Documentation

◆ loadExact()

template<typename T >
static bool SC::SerializationJson::loadExact ( T &  object,
StringView  text 
)
inlinestatic

Parses a JSON produced by SerializationJson::write loading its values into a C++ object Read a JSON buffer as output by SerializationJson::write, that means mainly not changing the relative order of struct fields in the json text.

Note
This code path does less checks that SerializationJson::loadVersioned and it could be potentially faster.
Template Parameters
TType of object to load
Parameters
objectObject to load
textJson text to be deserialized
Returns
true if load succeeded
See also
SC::SerializationJson for example usage

◆ loadVersioned()

template<typename T >
static bool SC::SerializationJson::loadVersioned ( T &  object,
StringView  text 
)
inlinestatic

Parses a JSON buffer and writes C++ objects supporting reordered or missing fields.

Template Parameters
TType of object load
Parameters
objectObject to load
textJson text to be deserialized
Returns
true if load succeeded
See also
SC::SerializationJson for example usage

◆ write()

template<typename T >
static bool SC::SerializationJson::write ( T &  object,
StringFormatOutput output,
Options  options = Options() 
)
inlinestatic

Writes a C++ object to JSON using Reflection.

Uses the strings associated with fields in Reflection to generate a JSON representation of a given C++ serializable structure.

Template Parameters
TType of object to write
Parameters
objectObject to write
outputOutput string interface
optionsJSON formatting options
Returns
true if write succeeded
See also
SC::SerializationJson for example usage

The documentation for this struct was generated from the following file: