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 ObjectLock olock(input);
134 for (int i = 0; i < type->GetFieldCount(); i++) {
135 Field field = type->GetFieldInfo(i);
137 if (attributeTypes != 0 && (field.Attributes & attributeTypes) == 0)
140 if (strcmp(field.Name, "type") == 0)
143 Value value = input->GetField(i);
144 stack.Push(field.Name, value);
145 fields.emplace_back(field.Name, SerializeInternal(input->GetField(i), attributeTypes, stack));
149 fields.emplace_back("type", type->GetName());
151 return new Dictionary(std::move(fields));
154 static Array::Ptr DeserializeArray(const Array::Ptr& input, bool safe_mode, int attributeTypes)
158 result.reserve(input->GetLength());
160 ObjectLock olock(input);
162 for (const Value& value : input) {
163 result.emplace_back(Deserialize(value, safe_mode, attributeTypes));
166 return new Array(std::move(result));
169 static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
171 DictionaryData result;
173 result.reserve(input->GetLength());
175 ObjectLock olock(input);
177 for (const Dictionary::Pair& kv : input) {
178 result.emplace_back(kv.first, Deserialize(kv.second, safe_mode, attributeTypes));
181 return new Dictionary(std::move(result));
184 static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
186 if (!object && safe_mode)
187 BOOST_THROW_EXCEPTION(std::runtime_error("Tried to instantiate object while safe mode is enabled."));
192 type = object->GetReflectionType();
194 type = Type::GetByName(input->Get("type"));
199 Object::Ptr instance;
204 instance = type->Instantiate(std::vector<Value>());
206 ObjectLock olock(input);
207 for (const Dictionary::Pair& kv : input) {
208 if (kv.first.IsEmpty())
211 int fid = type->GetFieldId(kv.first);
216 Field field = type->GetFieldInfo(fid);
218 if ((field.Attributes & attributeTypes) == 0)
222 instance->SetField(fid, Deserialize(kv.second, safe_mode, attributeTypes), true);
223 } catch (const std::exception&) {
224 instance->SetField(fid, Empty);
231 static Value SerializeInternal(const Value& value, int attributeTypes, SerializeStack& stack)
233 if (!value.IsObject())
236 Object::Ptr input = value;
238 Array::Ptr array = dynamic_pointer_cast<Array>(input);
241 return SerializeArray(array, attributeTypes, stack);
243 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
246 return SerializeDictionary(dict, attributeTypes, stack);
248 Namespace::Ptr ns = dynamic_pointer_cast<Namespace>(input);
251 return SerializeNamespace(ns, attributeTypes, stack);
253 return SerializeObject(input, attributeTypes, stack);
256 Value icinga::Serialize(const Value& value, int attributeTypes)
258 SerializeStack stack;
259 return SerializeInternal(value, attributeTypes, stack);
262 Value icinga::Deserialize(const Value& value, bool safe_mode, int attributeTypes)
264 return Deserialize(nullptr, value, safe_mode, attributeTypes);
267 Value icinga::Deserialize(const Object::Ptr& object, const Value& value, bool safe_mode, int attributeTypes)
269 if (!value.IsObject())
272 Object::Ptr input = value;
274 Array::Ptr array = dynamic_pointer_cast<Array>(input);
277 return DeserializeArray(array, safe_mode, attributeTypes);
279 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
283 if ((safe_mode && !object) || !dict->Contains("type"))
284 return DeserializeDictionary(dict, safe_mode, attributeTypes);
286 return DeserializeObject(object, dict, safe_mode, attributeTypes);