DTO Tools library
fty-pack is a data transfer object (DTO) library for converting data between json
, yaml
, zconfig
, protobuf
representations and C++ structs.
Typical C++ struct looks like:
struct MyData: public pack::Node
{
pack::String str = FIELD("str-value");
pack::Float fl = FIELD("float-value", 10.f);
pack::StringList list = FIELD("mylist");
using pack::Node::Node;
META(MyData, str, fl, list);
};
For example, to convert MyData
to yaml
just use:
MyData data;
data.str = "string";
data.list = {"one", "two"};
if (auto ret = pack::yaml::serialize(data)) {
std::cout << "Yaml content: " << *ret << std::endl;
} else {
std::cerr << "Serialization error: " << ret.error() << std::endl;
}
or from json
to MyData
:
MyData data;
if (auto ret = pack::json::deserialize(jsonContent, data)) {
// Use data
} else {
std::cerr << "Deserialization error: " << ret.error() << std::endl;
}