Skip to content

Serialization and deserialization

pribault edited this page Jan 28, 2023 · 1 revision

Json

Serialization

You can serialize a reflected class to json using the CppReflection::JsonWriter class, the compute method will then convert a Reflectable to a std::string for you like the following:

#include <CppReflection/JsonWriter.h>
#include "MyReflectedClass.h"

using namespace	CppReflection;

int main()
{
	MyReflectedClass instance;

	std::string result = JsonWriter::compute(instance);
	return 0;
}

Deserialization

You can deserialize a reflected class from json using the CppReflection::JsonReader class, the load method will then instanciate your reflectable from the given json for you like the following:

#include <CppReflection/JsonReader.h>
#include "MyReflectedClass.h"

using namespace	CppReflection;

int main()
{
	MyReflectedClass* instance;

	instance = JsonReader::load("{\"type\": \"MyReflectedClass\", \"value\": 42}");
	return 0;
}

Yaml

Serialization

You can serialize a reflected class to yaml using the CppReflection::YamlWriter class, the compute method will then convert a Reflectable to a std::string for you like the following:

#include <CppReflection/YamlWriter.h>
#include "MyReflectedClass.h"

using namespace	CppReflection;

int main()
{
	MyReflectedClass instance;

	std::string result = YamlWriter::compute(instance);
	return 0;
}

Deserialization

You can deserialize a reflected class from yaml using the CppReflection::YamlReader class, the load method will then instanciate your reflectable from the given json for you like the following:

#include <CppReflection/YamlReader.h>
#include "MyReflectedClass.h"

using namespace	CppReflection;

int main()
{
	MyReflectedClass* instance;

	instance = YamlReader::load("type: MyReflectedClass\nvalue: 42");
	return 0;
}

Clone this wiki locally