1 /******************************************************************************
3 * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/serializer.h"
21 #include "base/type.h"
22 #include "base/application.h"
23 #include "base/objectlock.h"
24 #include <boost/foreach.hpp>
27 using namespace icinga;
30 * Serializes a Value into a JSON string.
32 * @returns A string representing the Value.
34 String icinga::JsonSerialize(const Value& value)
36 cJSON *json = value.ToJson();
40 if (Application::IsDebugging())
41 jsonString = cJSON_Print(json);
43 jsonString = cJSON_PrintUnformatted(json);
47 String result = jsonString;
55 * Deserializes the string representation of a Value.
57 * @param data A JSON string obtained from JsonSerialize
58 * @returns The newly deserialized Value.
60 Value icinga::JsonDeserialize(const String& data)
62 cJSON *json = cJSON_Parse(data.CStr());
65 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid JSON String: " + data));
67 Value value = Value::FromJson(json);
73 static Array::Ptr SerializeArray(const Array::Ptr& input, int attributeTypes)
75 Array::Ptr result = make_shared<Array>();
77 ObjectLock olock(input);
79 BOOST_FOREACH(const Value& value, input) {
80 result->Add(Serialize(value, attributeTypes));
86 static Dictionary::Ptr SerializeDictionary(const Dictionary::Ptr& input, int attributeTypes)
88 Dictionary::Ptr result = make_shared<Dictionary>();
90 ObjectLock olock(input);
92 BOOST_FOREACH(const Dictionary::Pair& kv, input) {
93 result->Set(kv.first, Serialize(kv.second, attributeTypes));
99 static Object::Ptr SerializeObject(const Object::Ptr& input, int attributeTypes)
101 const Type *type = input->GetReflectionType();
105 Dictionary::Ptr fields = make_shared<Dictionary>();
107 for (int i = 0; i < type->GetFieldCount(); i++) {
108 Field field = type->GetFieldInfo(i);
110 if ((field.Attributes & attributeTypes) == 0)
113 fields->Set(field.Name, Serialize(input->GetField(i), attributeTypes));
116 fields->Set("__type", type->GetName());
121 static Array::Ptr DeserializeArray(const Array::Ptr& input, bool safe_mode, int attributeTypes)
123 Array::Ptr result = make_shared<Array>();
125 ObjectLock olock(input);
127 BOOST_FOREACH(const Value& value, input) {
128 result->Add(Deserialize(value, safe_mode, attributeTypes));
134 static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
136 Dictionary::Ptr result = make_shared<Dictionary>();
138 ObjectLock olock(input);
140 BOOST_FOREACH(const Dictionary::Pair& kv, input) {
141 result->Set(kv.first, Deserialize(kv.second, attributeTypes));
147 static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
152 type = object->GetReflectionType();
154 type = Type::GetByName(input->Get("__type"));
159 Object::Ptr instance = object;
162 if (safe_mode && !type->IsSafe())
163 BOOST_THROW_EXCEPTION(std::runtime_error("Tried to instantiate type '" + type->GetName() + "' which is not marked as safe."));
165 instance = type->Instantiate();
168 BOOST_FOREACH(const Dictionary::Pair& kv, input) {
169 if (kv.first.IsEmpty())
172 int fid = type->GetFieldId(kv.first);
177 Field field = type->GetFieldInfo(fid);
179 if ((field.Attributes & attributeTypes) == 0)
183 instance->SetField(fid, Deserialize(kv.second, safe_mode, attributeTypes));
184 } catch (const std::exception&) {
185 instance->SetField(fid, Empty);
192 Value icinga::Serialize(const Value& value, int attributeTypes)
194 if (!value.IsObject())
197 Object::Ptr input = value;
199 Array::Ptr array = dynamic_pointer_cast<Array>(input);
202 return SerializeArray(array, attributeTypes);
204 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
207 return SerializeDictionary(dict, attributeTypes);
209 return SerializeObject(input, attributeTypes);
212 Value icinga::Deserialize(const Value& value, bool safe_mode, int attributeTypes)
214 return Deserialize(Object::Ptr(), value, safe_mode, attributeTypes);
217 Value icinga::Deserialize(const Object::Ptr& object, const Value& value, bool safe_mode, int attributeTypes)
219 if (!value.IsObject())
222 Object::Ptr input = value;
224 Array::Ptr array = dynamic_pointer_cast<Array>(input);
227 return DeserializeArray(array, safe_mode, attributeTypes);
229 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
231 ASSERT(dict != NULL);
233 if (!dict->Contains("__type"))
234 return DeserializeDictionary(dict, safe_mode, attributeTypes);
236 return DeserializeObject(object, dict, safe_mode, attributeTypes);