]> granicus.if.org Git - icinga2/blob - lib/base/serializer.cpp
Merge branch 'feature/debian-packaging-4988' into next
[icinga2] / lib / base / serializer.cpp
1 /******************************************************************************
2 * Icinga 2                                                                   *
3 * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
4 *                                                                            *
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.                     *
9 *                                                                            *
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.                               *
14 *                                                                            *
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 ******************************************************************************/
19
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>
26 #include <cJSON.h>
27
28 using namespace icinga;
29
30 /**
31  * Serializes a Value into a JSON string.
32  *
33  * @returns A string representing the Value.
34  */
35 String icinga::JsonSerialize(const Value& value)
36 {
37         cJSON *json = value.ToJson();
38
39         char *jsonString;
40
41         if (Application::IsDebugging())
42                 jsonString = cJSON_Print(json);
43         else
44                 jsonString = cJSON_PrintUnformatted(json);
45
46         cJSON_Delete(json);
47
48         String result = jsonString;
49
50         free(jsonString);
51
52         return result;
53 }
54
55 /**
56  * Deserializes the string representation of a Value.
57  *
58  * @param data A JSON string obtained from JsonSerialize
59  * @returns The newly deserialized Value.
60  */
61 Value icinga::JsonDeserialize(const String& data)
62 {
63         cJSON *json = cJSON_Parse(data.CStr());
64
65         if (!json)
66                 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid JSON String: " + data));
67
68         Value value = Value::FromJson(json);
69         cJSON_Delete(json);
70
71         return value;
72 }
73
74 static Array::Ptr SerializeArray(const Array::Ptr& input, int attributeTypes)
75 {
76         Array::Ptr result = make_shared<Array>();
77
78         ObjectLock olock(input);
79
80         BOOST_FOREACH(const Value& value, input) {
81                 result->Add(Serialize(value, attributeTypes));
82         }
83
84         return result;
85 }
86
87 static Dictionary::Ptr SerializeDictionary(const Dictionary::Ptr& input, int attributeTypes)
88 {
89         Dictionary::Ptr result = make_shared<Dictionary>();
90
91         ObjectLock olock(input);
92
93         String key;
94         Value value;
95         BOOST_FOREACH(boost::tie(key, value), input) {
96                 result->Set(key, Serialize(value, attributeTypes));
97         }
98
99         return result;
100 }
101
102 static Object::Ptr SerializeObject(const Object::Ptr& input, int attributeTypes)
103 {
104         const Type *type = input->GetReflectionType();
105
106         VERIFY(type);
107
108         Dictionary::Ptr fields = make_shared<Dictionary>();
109
110         for (int i = 0; i < type->GetFieldCount(); i++) {
111                 Field field = type->GetFieldInfo(i);
112
113                 if ((field.Attributes & attributeTypes) == 0)
114                         continue;
115
116                 fields->Set(field.Name, Serialize(input->GetField(i), attributeTypes));
117         }
118
119         fields->Set("__type", type->GetName());
120
121         return fields;
122 }
123
124 static Array::Ptr DeserializeArray(const Array::Ptr& input, int attributeTypes)
125 {
126         Array::Ptr result = make_shared<Array>();
127
128         ObjectLock olock(input);
129
130         BOOST_FOREACH(const Value& value, input) {
131                 result->Add(Deserialize(value, attributeTypes));
132         }
133
134         return result;
135 }
136
137 static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, int attributeTypes)
138 {
139         Dictionary::Ptr result = make_shared<Dictionary>();
140
141         ObjectLock olock(input);
142
143         String key;
144         Value value;
145         BOOST_FOREACH(boost::tie(key, value), input) {
146                 result->Set(key, Deserialize(value, attributeTypes));
147         }
148
149         return result;
150 }
151
152 static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, int attributeTypes)
153 {
154         const Type *type;
155
156         if (object)
157                 type = object->GetReflectionType();
158         else
159                 type = Type::GetByName(input->Get("__type"));
160
161         if (!type)
162                 return object;
163
164         Object::Ptr instance = object;
165
166         if (!instance)
167                 instance = type->Instantiate();
168
169         for (int i = 0; i < type->GetFieldCount(); i++) {
170                 Field field = type->GetFieldInfo(i);
171
172                 if ((field.Attributes & attributeTypes) == 0)
173                         continue;
174
175                 if (!input->Contains(field.Name))
176                         continue;
177
178                 instance->SetField(i, Deserialize(input->Get(field.Name), attributeTypes));
179         }
180
181         return instance;
182 }
183
184 Value icinga::Serialize(const Value& value, int attributeTypes)
185 {
186         if (!value.IsObject())
187                 return value;
188
189         Object::Ptr input = value;
190
191         Array::Ptr array = dynamic_pointer_cast<Array>(input);
192
193         if (array != NULL)
194                 return SerializeArray(array, attributeTypes);
195
196         Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
197
198         if (dict != NULL)
199                 return SerializeDictionary(dict, attributeTypes);
200
201         return SerializeObject(input, attributeTypes);
202 }
203
204 Value icinga::Deserialize(const Value& value, int attributeTypes)
205 {
206         return Deserialize(Object::Ptr(), value, attributeTypes);
207 }
208
209 Value icinga::Deserialize(const Object::Ptr& object, const Value& value, int attributeTypes)
210 {
211         if (!value.IsObject())
212                 return value;
213
214         Object::Ptr input = value;
215
216         Array::Ptr array = dynamic_pointer_cast<Array>(input);
217
218         if (array != NULL)
219                 return DeserializeArray(array, attributeTypes);
220
221         Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
222
223         ASSERT(dict != NULL);
224
225         if (!dict->Contains("__type"))
226                 return DeserializeDictionary(dict, attributeTypes);
227
228         return DeserializeObject(object, dict, attributeTypes);
229 }