13 std::string _type_name;
17 const std::string &typeName()
const {
return _type_name; }
18 virtual void createReplacement()
const = 0;
29 virtual void createReplacement()
const {
30 Type::global<SerializationTypeReplacement<T>>(typeName(), Type::find<T>());
35 auto serialize(
const T &v) ->
36 typename std::enable_if<toStringSupported((T *)nullptr), Variant>::type {
42 auto deserialize(T &v,
const Variant &r) ->
43 typename std::enable_if<fromStringSupported((T *)nullptr), void>::type {
44 fromString(v, r.value<std::string>());
48 auto serialize(
const T &v) ->
49 typename std::enable_if<propertiesSupported((T *)nullptr), Variant>::type {
50 std::map<std::string, Variant> r;
51 std::vector<Property> pp;
52 properties(*(T *)&v, pp);
53 for (
auto &property : pp) {
54 r[
property.displayName()] =
property.serialize();
59 auto deserialize(T &value,
const Variant &variant) ->
60 typename std::enable_if<propertiesSupported((T *)nullptr), void>::type {
61 auto map = variant.value<std::map<std::string, Variant>>();
62 std::vector<Property> pp;
63 properties(*(T *)&value, pp);
64 for (
auto &property : pp) {
65 if (map.find(property.name()) != map.end()) {
66 property.deserialize(map[property.name()]);
67 }
else if (map.find(property.displayName()) != map.end()) {
68 property.deserialize(map[property.displayName()]);
73 template <
class T>
Variant serialize(
const std::vector<T> &v) {
74 std::vector<Variant> r;
76 r.push_back(serialize(x));
80 template <
class T>
void deserialize(std::vector<T> &v,
const Variant &x) {
81 auto &r = x.value<std::vector<Variant>>();
83 for (
size_t i = 0; i < v.size(); i++) {
84 deserialize(v[i], r[i]);
89 return Variant(std::to_string(v.id()));
91 template <
class T>
void deserialize(
Handle<T> &handle,
const Variant &variant) {
92 handle.reset(std::stoul(variant.value<std::string>()));
96 static std::shared_ptr<const Type>
97 findSerializationType(
const std::shared_ptr<T> &p) {
98 if (p ==
nullptr ||
typeid(*p) ==
typeid(T)) {
101 return Type::find(
typeid(*p));
103 template <
class T>
Variant serialize(
const std::shared_ptr<T> &v) {
105 std::map<std::string, Variant> r;
106 for (
auto &property : v->properties()) {
107 r[
property.name()] =
property.serialize();
109 if (
auto type = findSerializationType(v)) {
110 r[
"type"] =
Variant(std::string(type->name()));
112 r[
"id"] =
Variant(std::to_string(v->id()));
115 return Variant(std::string(
"null"));
119 void deserialize(std::shared_ptr<T> &
object,
const Variant &variant) {
120 if (variant.type() ==
typeid(std::string)) {
121 auto s = variant.value<std::string>();
122 if (s ==
"null" || s ==
"") {
125 throw std::runtime_error(
"invalid value");
128 auto map = variant.value<std::map<std::string, Variant>>();
129 if (map.find(
"type") != map.end()) {
130 std::string type_name = map[
"type"].value<std::string>();
131 auto type = Type::tryFind(type_name);
135 if (
object ==
nullptr ||
136 std::type_index(
typeid(*
object)) != type->typeId()) {
137 object = type->instantiate<T>();
140 std::shared_ptr<Type> type = Type::find<T>();
141 if (
object ==
nullptr ||
142 std::type_index(
typeid(*
object)) != type->typeId()) {
143 object = type->instantiate<T>();
146 if (map.find(
"id") != map.end()) {
147 object->setId(std::stoull(map[
"id"].value<std::string>()));
149 for (
auto &property : object->properties()) {
150 if (map.find(property.name()) != map.end()) {
151 property.deserialize(map[property.name()]);
159 return ::serialize(*(T *)value);
163 ::deserialize(*(T *)value, v);
166 void toYAML(
const Variant &v, std::ostream &stream);
167 std::string toYAML(
const Variant &v);
169 Variant parseYAML(
const std::string &str);