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>
25 #include <boost/tuple/tuple.hpp>
28 using namespace icinga;
31 * Serializes a Value into a JSON string.
33 * @returns A string representing the Value.
35 String icinga::JsonSerialize(const Value& value)
37 cJSON *json = value.ToJson();
41 if (Application::IsDebugging())
42 jsonString = cJSON_Print(json);
44 jsonString = cJSON_PrintUnformatted(json);
48 String result = jsonString;
56 * Deserializes the string representation of a Value.
58 * @param data A JSON string obtained from JsonSerialize
59 * @returns The newly deserialized Value.
61 Value icinga::JsonDeserialize(const String& data)
63 cJSON *json = cJSON_Parse(data.CStr());
66 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid JSON String: " + data));
68 Value value = Value::FromJson(json);
74 static Array::Ptr SerializeArray(const Array::Ptr& input, int attributeTypes)
76 Array::Ptr result = make_shared<Array>();
78 ObjectLock olock(input);
80 BOOST_FOREACH(const Value& value, input) {
81 result->Add(Serialize(value, attributeTypes));
87 static Dictionary::Ptr SerializeDictionary(const Dictionary::Ptr& input, int attributeTypes)
89 Dictionary::Ptr result = make_shared<Dictionary>();
91 ObjectLock olock(input);
95 BOOST_FOREACH(boost::tie(key, value), input) {
96 result->Set(key, Serialize(value, attributeTypes));
102 static Object::Ptr SerializeObject(const Object::Ptr& input, int attributeTypes)
104 const Type *type = input->GetReflectionType();
108 Dictionary::Ptr fields = make_shared<Dictionary>();
110 for (int i = 0; i < type->GetFieldCount(); i++) {
111 Field field = type->GetFieldInfo(i);
113 if ((field.Attributes & attributeTypes) == 0)
116 fields->Set(field.Name, Serialize(input->GetField(i), attributeTypes));
119 fields->Set("__type", type->GetName());
124 static Array::Ptr DeserializeArray(const Array::Ptr& input, int attributeTypes)
126 Array::Ptr result = make_shared<Array>();
128 ObjectLock olock(input);
130 BOOST_FOREACH(const Value& value, input) {
131 result->Add(Deserialize(value, attributeTypes));
137 static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, int attributeTypes)
139 Dictionary::Ptr result = make_shared<Dictionary>();
141 ObjectLock olock(input);
145 BOOST_FOREACH(boost::tie(key, value), input) {
146 result->Set(key, Deserialize(value, attributeTypes));
152 static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, int attributeTypes)
157 type = object->GetReflectionType();
159 type = Type::GetByName(input->Get("__type"));
164 Object::Ptr instance = object;
167 instance = type->Instantiate();
169 for (int i = 0; i < type->GetFieldCount(); i++) {
170 Field field = type->GetFieldInfo(i);
172 if ((field.Attributes & attributeTypes) == 0)
175 if (!input->Contains(field.Name))
179 instance->SetField(i, Deserialize(input->Get(field.Name), attributeTypes));
180 } catch (const std::exception&) {
181 instance->SetField(i, Empty);
188 Value icinga::Serialize(const Value& value, int attributeTypes)
190 if (!value.IsObject())
193 Object::Ptr input = value;
195 Array::Ptr array = dynamic_pointer_cast<Array>(input);
198 return SerializeArray(array, attributeTypes);
200 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
203 return SerializeDictionary(dict, attributeTypes);
205 return SerializeObject(input, attributeTypes);
208 Value icinga::Deserialize(const Value& value, int attributeTypes)
210 return Deserialize(Object::Ptr(), value, attributeTypes);
213 Value icinga::Deserialize(const Object::Ptr& object, const Value& value, int attributeTypes)
215 if (!value.IsObject())
218 Object::Ptr input = value;
220 Array::Ptr array = dynamic_pointer_cast<Array>(input);
223 return DeserializeArray(array, attributeTypes);
225 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
227 ASSERT(dict != NULL);
229 if (!dict->Contains("__type"))
230 return DeserializeDictionary(dict, attributeTypes);
232 return DeserializeObject(object, dict, attributeTypes);