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