1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/serializer.hpp"
4 #include "base/type.hpp"
5 #include "base/application.hpp"
6 #include "base/objectlock.hpp"
7 #include "base/convert.hpp"
8 #include "base/exception.hpp"
9 #include "base/namespace.hpp"
10 #include <boost/algorithm/string/join.hpp>
13 using namespace icinga;
15 struct SerializeStackEntry
21 CircularReferenceError::CircularReferenceError(String message, std::vector<String> path)
22 : m_Message(message), m_Path(path)
25 const char *CircularReferenceError::what(void) const throw()
27 return m_Message.CStr();
30 std::vector<String> CircularReferenceError::GetPath() const
37 std::deque<SerializeStackEntry> Entries;
39 inline void Push(const String& name, const Value& val)
47 for (const auto& entry : Entries) {
48 if (entry.Val == obj) {
49 std::vector<String> path;
50 for (const auto& entry : Entries)
51 path.push_back(entry.Name);
53 BOOST_THROW_EXCEPTION(CircularReferenceError("Cannot serialize object which recursively refers to itself. Attribute path which leads to the cycle: " + boost::algorithm::join(path, " -> "), path));
58 Entries.push_back({ name, obj });
67 static Value SerializeInternal(const Value& value, int attributeTypes, SerializeStack& stack);
69 static Array::Ptr SerializeArray(const Array::Ptr& input, int attributeTypes, SerializeStack& stack)
73 result.reserve(input->GetLength());
75 ObjectLock olock(input);
79 for (const Value& value : input) {
80 stack.Push(Convert::ToString(index), value);
81 result.emplace_back(SerializeInternal(value, attributeTypes, stack));
86 return new Array(std::move(result));
89 static Dictionary::Ptr SerializeDictionary(const Dictionary::Ptr& input, int attributeTypes, SerializeStack& stack)
91 DictionaryData result;
93 result.reserve(input->GetLength());
95 ObjectLock olock(input);
97 for (const Dictionary::Pair& kv : input) {
98 stack.Push(kv.first, kv.second);
99 result.emplace_back(kv.first, SerializeInternal(kv.second, attributeTypes, stack));
103 return new Dictionary(std::move(result));
106 static Dictionary::Ptr SerializeNamespace(const Namespace::Ptr& input, int attributeTypes, SerializeStack& stack)
108 DictionaryData result;
110 ObjectLock olock(input);
112 for (const Namespace::Pair& kv : input) {
113 Value val = kv.second->Get();
114 stack.Push(kv.first, val);
115 result.emplace_back(kv.first, Serialize(val, attributeTypes));
119 return new Dictionary(std::move(result));
122 static Object::Ptr SerializeObject(const Object::Ptr& input, int attributeTypes, SerializeStack& stack)
124 Type::Ptr type = input->GetReflectionType();
129 DictionaryData fields;
130 fields.reserve(type->GetFieldCount() + 1);
132 for (int i = 0; i < type->GetFieldCount(); i++) {
133 Field field = type->GetFieldInfo(i);
135 if (attributeTypes != 0 && (field.Attributes & attributeTypes) == 0)
138 if (strcmp(field.Name, "type") == 0)
141 Value value = input->GetField(i);
142 stack.Push(field.Name, value);
143 fields.emplace_back(field.Name, SerializeInternal(input->GetField(i), attributeTypes, stack));
147 fields.emplace_back("type", type->GetName());
149 return new Dictionary(std::move(fields));
152 static Array::Ptr DeserializeArray(const Array::Ptr& input, bool safe_mode, int attributeTypes)
156 result.reserve(input->GetLength());
158 ObjectLock olock(input);
160 for (const Value& value : input) {
161 result.emplace_back(Deserialize(value, safe_mode, attributeTypes));
164 return new Array(std::move(result));
167 static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
169 DictionaryData result;
171 result.reserve(input->GetLength());
173 ObjectLock olock(input);
175 for (const Dictionary::Pair& kv : input) {
176 result.emplace_back(kv.first, Deserialize(kv.second, safe_mode, attributeTypes));
179 return new Dictionary(std::move(result));
182 static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
184 if (!object && safe_mode)
185 BOOST_THROW_EXCEPTION(std::runtime_error("Tried to instantiate object while safe mode is enabled."));
190 type = object->GetReflectionType();
192 type = Type::GetByName(input->Get("type"));
197 Object::Ptr instance;
202 instance = type->Instantiate(std::vector<Value>());
204 ObjectLock olock(input);
205 for (const Dictionary::Pair& kv : input) {
206 if (kv.first.IsEmpty())
209 int fid = type->GetFieldId(kv.first);
214 Field field = type->GetFieldInfo(fid);
216 if ((field.Attributes & attributeTypes) == 0)
220 instance->SetField(fid, Deserialize(kv.second, safe_mode, attributeTypes), true);
221 } catch (const std::exception&) {
222 instance->SetField(fid, Empty);
229 static Value SerializeInternal(const Value& value, int attributeTypes, SerializeStack& stack)
231 if (!value.IsObject())
234 Object::Ptr input = value;
236 Array::Ptr array = dynamic_pointer_cast<Array>(input);
239 return SerializeArray(array, attributeTypes, stack);
241 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
244 return SerializeDictionary(dict, attributeTypes, stack);
246 Namespace::Ptr ns = dynamic_pointer_cast<Namespace>(input);
249 return SerializeNamespace(ns, attributeTypes, stack);
251 return SerializeObject(input, attributeTypes, stack);
254 Value icinga::Serialize(const Value& value, int attributeTypes)
256 SerializeStack stack;
257 return SerializeInternal(value, attributeTypes, stack);
260 Value icinga::Deserialize(const Value& value, bool safe_mode, int attributeTypes)
262 return Deserialize(nullptr, value, safe_mode, attributeTypes);
265 Value icinga::Deserialize(const Object::Ptr& object, const Value& value, bool safe_mode, int attributeTypes)
267 if (!value.IsObject())
270 Object::Ptr input = value;
272 Array::Ptr array = dynamic_pointer_cast<Array>(input);
275 return DeserializeArray(array, safe_mode, attributeTypes);
277 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
281 if ((safe_mode && !object) || !dict->Contains("type"))
282 return DeserializeDictionary(dict, safe_mode, attributeTypes);
284 return DeserializeObject(object, dict, safe_mode, attributeTypes);