ElementJsonSerializers.cpp
Go to the documentation of this file.
2
3namespace armarx::viz
4{
5 std::string
6 json::getTypeName(const nlohmann::json& j, const std::string& key)
7 {
8 try
9 {
10 return j.at(key);
11 }
12 catch (const nlohmann::detail::out_of_range&)
13 {
15 }
16 catch (const nlohmann::detail::type_error&)
17 {
19 }
20 }
21
22 void
23 json::setTypeName(nlohmann::json& j, const std::string& key, const std::string& typeName)
24 {
25 if (j.count(key) > 0)
26 {
27 throw "todo"; //error::TypeNameEntryAlreadyInJsonObject(key, typeName, j);
28 }
29 j[key] = typeName;
30 }
31
32 void
33 data::to_json(nlohmann::json& j, const Element& element)
34 {
36 }
37
38 void
39 data::from_json(const nlohmann::json& j, Element& element)
40 {
42 }
43
44 void
45 data::to_json(nlohmann::json& j, const ElementPtr& shapePtr)
46 {
48 }
49
50 void
51 data::from_json(const nlohmann::json& j, ElementPtr& shapePtr)
52 {
54 }
55
56 void
57 data::to_json(nlohmann::json& j, const Element* shapePtr)
58 {
60 }
61} // namespace armarx::viz
62
64{
65
66 const std::string ElementJsonSerializers::JSON_TYPE_NAME_KEY = "__type__";
67
68 ElementJsonSerializers ElementJsonSerializers::_instance = {};
69
70 void
71 ElementJsonSerializers::ElementJsonSerializer::to_json(nlohmann::json& j,
72 const data::Element& element)
73 {
74 _to_json(j, element);
75 }
76
77 void
78 ElementJsonSerializers::ElementJsonSerializer::from_json(const nlohmann::json& j,
79 data::Element& element)
80 {
81 _from_json(j, element);
82 }
83
84 void
85 ElementJsonSerializers::ElementJsonSerializer::to_json(nlohmann::json& j,
86 const data::ElementPtr& shapePtr)
87 {
88 _to_json(j, *shapePtr);
89 }
90
91 void
92 ElementJsonSerializers::ElementJsonSerializer::from_json(const nlohmann::json& j,
93 data::ElementPtr& shapePtr)
94 {
95 _from_json_ptr(j, shapePtr);
96 }
97
98 ElementJsonSerializers::ElementJsonSerializer&
99 ElementJsonSerializers::getSerializer(const nlohmann::json& j)
100 {
101 return _instance._getSerializer(getTypeName(j));
102 }
103
104 ElementJsonSerializers::ElementJsonSerializer&
105 ElementJsonSerializers::getSerializer(const std::string& demangledTypeName)
106 {
107 return _instance._getSerializer(demangledTypeName);
108 }
109
110 std::vector<std::string>
112 {
113 return _instance._getRegisteredTypes();
114 }
115
116 bool
117 ElementJsonSerializers::isTypeRegistered(const std::string& typeName)
118 {
119 return _instance._isTypeRegistered(typeName);
120 }
121
122 void
123 ElementJsonSerializers::to_json(nlohmann::json& j, const data::Element& element)
124 {
125 return getSerializer(simox::meta::get_type_name(element)).to_json(j, element);
126 }
127
128 void
129 ElementJsonSerializers::from_json(const nlohmann::json& j, data::Element& element)
130 {
131 const std::string typeName = getTypeName(j);
132 if (typeName != simox::meta::get_type_name(element))
133 {
134 throw error::TypeNameMismatch(typeName, simox::meta::get_type_name(element));
135 }
136 getSerializer(typeName).from_json(j, element);
137 }
138
139 void
140 ElementJsonSerializers::to_json(nlohmann::json& j, const data::Element* elementPtr)
141 {
142 if (!elementPtr)
143 {
145 "data::Element* is null in ElementJsonSerializers::to_json().");
146 }
147 to_json(j, *elementPtr);
148 }
149
150 void
151 ElementJsonSerializers::from_json(const nlohmann::json& j, data::ElementPtr& shapePtr)
152 {
153 getSerializer(j).from_json(j, shapePtr);
154 }
155
156 std::string
157 ElementJsonSerializers::getTypeName(const nlohmann::json& j)
158 {
160 }
161
162 ElementJsonSerializers::ElementJsonSerializers()
163 {
164 registerElements();
165 }
166
167 ElementJsonSerializers::ElementJsonSerializer&
168 ElementJsonSerializers::_getSerializer(const std::string& demangledTypeName)
169 {
170 if (auto find = _serializers.find(demangledTypeName); find != _serializers.end())
171 {
172 return find->second;
173 }
174 else
175 {
176 throw error::NoSerializerForType(demangledTypeName);
177 }
178 }
179
180 std::vector<std::string>
181 ElementJsonSerializers::_getRegisteredTypes() const
182 {
183 std::vector<std::string> types;
184 for (const auto& [typeName, serializer] : _serializers)
185 {
186 types.push_back(typeName);
187 }
188 return types;
189 }
190
191 bool
192 ElementJsonSerializers::_isTypeRegistered(const std::string& typeName) const
193 {
194 return _serializers.count(typeName) > 0;
195 }
196
197
198} // namespace armarx::viz::json
Indicates that a JSON document did not contain the type name.
Definition exceptions.h:19
Indicates that the type name in a JSON object did not match the type of the passed C++ object.
Definition exceptions.h:42
Handles serialization and deserialization of dynamic data::Element objects to and from JSON.
static void from_json(const nlohmann::json &j, data::Element &element)
Deserialize element from JSON according to its dynamic type.
static const std::string JSON_TYPE_NAME_KEY
JSON key under which demangled type name is stored.
static void to_json(nlohmann::json &j, const data::Element &element)
Serialize element to JSON according to its dynamic type.
static std::vector< std::string > getRegisteredTypes()
Get the type names for which serializers are registered.
static std::string getTypeName(const nlohmann::json &j)
static bool isTypeRegistered(const std::string &typeName)
Indicates whether there is a serializer registered for the given type name.
void to_json(nlohmann::json &j, RecordingBatchHeader const &batch)
void from_json(nlohmann::json const &j, RecordingBatchHeader &batch)
std::string getTypeName(const nlohmann::json &j, const std::string &key)
Get the type name stored in j.
void setTypeName(nlohmann::json &j, const std::string &key, const std::string &typeName)
Store the type name in j.
This file is part of ArmarX.