2013-11-04 23:14:34 +01:00
|
|
|
/******************************************************************************
|
2014-05-03 20:02:22 +02:00
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
2013-11-04 23:14:34 +01:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/serializer.hpp"
|
|
|
|
#include "base/type.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2013-11-08 11:17:46 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <cJSON.h>
|
2013-11-04 23:14:34 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
/**
|
|
|
|
* Serializes a Value into a JSON string.
|
|
|
|
*
|
|
|
|
* @returns A string representing the Value.
|
|
|
|
*/
|
|
|
|
String icinga::JsonSerialize(const Value& value)
|
2013-11-04 23:14:34 +01:00
|
|
|
{
|
2013-11-08 11:17:46 +01:00
|
|
|
cJSON *json = value.ToJson();
|
2013-11-04 23:14:34 +01:00
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
char *jsonString;
|
|
|
|
|
2014-05-23 18:05:04 +02:00
|
|
|
#ifdef _DEBUG
|
|
|
|
jsonString = cJSON_Print(json);
|
|
|
|
#else /* _DEBUG */
|
|
|
|
jsonString = cJSON_PrintUnformatted(json);
|
|
|
|
#endif /* _DEBUG */
|
2013-11-08 11:17:46 +01:00
|
|
|
|
|
|
|
cJSON_Delete(json);
|
|
|
|
|
|
|
|
String result = jsonString;
|
|
|
|
|
|
|
|
free(jsonString);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserializes the string representation of a Value.
|
|
|
|
*
|
|
|
|
* @param data A JSON string obtained from JsonSerialize
|
|
|
|
* @returns The newly deserialized Value.
|
|
|
|
*/
|
|
|
|
Value icinga::JsonDeserialize(const String& data)
|
|
|
|
{
|
|
|
|
cJSON *json = cJSON_Parse(data.CStr());
|
|
|
|
|
|
|
|
if (!json)
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid JSON String: " + data));
|
|
|
|
|
|
|
|
Value value = Value::FromJson(json);
|
|
|
|
cJSON_Delete(json);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Array::Ptr SerializeArray(const Array::Ptr& input, int attributeTypes)
|
|
|
|
{
|
|
|
|
Array::Ptr result = make_shared<Array>();
|
|
|
|
|
|
|
|
ObjectLock olock(input);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Value& value, input) {
|
|
|
|
result->Add(Serialize(value, attributeTypes));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Dictionary::Ptr SerializeDictionary(const Dictionary::Ptr& input, int attributeTypes)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr result = make_shared<Dictionary>();
|
|
|
|
|
|
|
|
ObjectLock olock(input);
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, input) {
|
|
|
|
result->Set(kv.first, Serialize(kv.second, attributeTypes));
|
2013-11-08 11:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Object::Ptr SerializeObject(const Object::Ptr& input, int attributeTypes)
|
|
|
|
{
|
|
|
|
const Type *type = input->GetReflectionType();
|
|
|
|
|
|
|
|
VERIFY(type);
|
|
|
|
|
|
|
|
Dictionary::Ptr fields = make_shared<Dictionary>();
|
2013-11-04 23:14:34 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < type->GetFieldCount(); i++) {
|
|
|
|
Field field = type->GetFieldInfo(i);
|
|
|
|
|
|
|
|
if ((field.Attributes & attributeTypes) == 0)
|
|
|
|
continue;
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
fields->Set(field.Name, Serialize(input->GetField(i), attributeTypes));
|
|
|
|
}
|
|
|
|
|
2014-03-28 13:52:29 +01:00
|
|
|
fields->Set("type", type->GetName());
|
2013-11-08 11:17:46 +01:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
static Array::Ptr DeserializeArray(const Array::Ptr& input, bool safe_mode, int attributeTypes)
|
2013-11-08 11:17:46 +01:00
|
|
|
{
|
|
|
|
Array::Ptr result = make_shared<Array>();
|
|
|
|
|
|
|
|
ObjectLock olock(input);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Value& value, input) {
|
2013-12-18 10:18:57 +01:00
|
|
|
result->Add(Deserialize(value, safe_mode, attributeTypes));
|
2013-11-08 11:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
|
2013-11-08 11:17:46 +01:00
|
|
|
{
|
|
|
|
Dictionary::Ptr result = make_shared<Dictionary>();
|
|
|
|
|
|
|
|
ObjectLock olock(input);
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, input) {
|
2014-05-11 06:00:34 +02:00
|
|
|
result->Set(kv.first, Deserialize(kv.second, safe_mode, attributeTypes));
|
2013-11-04 23:14:34 +01:00
|
|
|
}
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
return result;
|
2013-11-04 23:14:34 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
|
2013-11-04 23:14:34 +01:00
|
|
|
{
|
2013-11-08 11:17:46 +01:00
|
|
|
const Type *type;
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
type = object->GetReflectionType();
|
|
|
|
else
|
2014-03-28 13:52:29 +01:00
|
|
|
type = Type::GetByName(input->Get("type"));
|
2013-11-08 11:17:46 +01:00
|
|
|
|
|
|
|
if (!type)
|
|
|
|
return object;
|
|
|
|
|
|
|
|
Object::Ptr instance = object;
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
if (!instance) {
|
|
|
|
if (safe_mode && !type->IsSafe())
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Tried to instantiate type '" + type->GetName() + "' which is not marked as safe."));
|
|
|
|
|
2013-11-08 11:17:46 +01:00
|
|
|
instance = type->Instantiate();
|
2013-12-18 10:18:57 +01:00
|
|
|
}
|
2013-11-04 23:14:34 +01:00
|
|
|
|
2014-03-29 23:03:19 +01:00
|
|
|
ObjectLock olock(input);
|
2013-12-13 12:54:14 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, input) {
|
|
|
|
if (kv.first.IsEmpty())
|
|
|
|
continue;
|
2013-11-04 23:14:34 +01:00
|
|
|
|
2013-12-13 12:54:14 +01:00
|
|
|
int fid = type->GetFieldId(kv.first);
|
|
|
|
|
|
|
|
if (fid < 0)
|
2013-11-04 23:14:34 +01:00
|
|
|
continue;
|
|
|
|
|
2013-12-13 12:54:14 +01:00
|
|
|
Field field = type->GetFieldInfo(fid);
|
2013-12-03 09:59:21 +01:00
|
|
|
|
2013-12-13 12:54:14 +01:00
|
|
|
if ((field.Attributes & attributeTypes) == 0)
|
2013-11-04 23:14:34 +01:00
|
|
|
continue;
|
|
|
|
|
2013-11-09 18:30:17 +01:00
|
|
|
try {
|
2013-12-18 10:18:57 +01:00
|
|
|
instance->SetField(fid, Deserialize(kv.second, safe_mode, attributeTypes));
|
2013-11-09 18:30:17 +01:00
|
|
|
} catch (const std::exception&) {
|
2013-12-13 12:54:14 +01:00
|
|
|
instance->SetField(fid, Empty);
|
2013-11-09 18:30:17 +01:00
|
|
|
}
|
2013-11-04 23:14:34 +01:00
|
|
|
}
|
2013-11-08 11:17:46 +01:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::Serialize(const Value& value, int attributeTypes)
|
|
|
|
{
|
|
|
|
if (!value.IsObject())
|
|
|
|
return value;
|
|
|
|
|
|
|
|
Object::Ptr input = value;
|
|
|
|
|
|
|
|
Array::Ptr array = dynamic_pointer_cast<Array>(input);
|
|
|
|
|
|
|
|
if (array != NULL)
|
|
|
|
return SerializeArray(array, attributeTypes);
|
|
|
|
|
|
|
|
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
|
|
|
|
|
|
|
|
if (dict != NULL)
|
|
|
|
return SerializeDictionary(dict, attributeTypes);
|
|
|
|
|
|
|
|
return SerializeObject(input, attributeTypes);
|
|
|
|
}
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
Value icinga::Deserialize(const Value& value, bool safe_mode, int attributeTypes)
|
2013-11-08 11:17:46 +01:00
|
|
|
{
|
2013-12-18 10:18:57 +01:00
|
|
|
return Deserialize(Object::Ptr(), value, safe_mode, attributeTypes);
|
2013-11-08 11:17:46 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
Value icinga::Deserialize(const Object::Ptr& object, const Value& value, bool safe_mode, int attributeTypes)
|
2013-11-08 11:17:46 +01:00
|
|
|
{
|
|
|
|
if (!value.IsObject())
|
|
|
|
return value;
|
|
|
|
|
|
|
|
Object::Ptr input = value;
|
|
|
|
|
|
|
|
Array::Ptr array = dynamic_pointer_cast<Array>(input);
|
|
|
|
|
|
|
|
if (array != NULL)
|
2013-12-18 10:18:57 +01:00
|
|
|
return DeserializeArray(array, safe_mode, attributeTypes);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
|
|
|
|
|
|
|
|
ASSERT(dict != NULL);
|
|
|
|
|
2014-03-28 13:52:29 +01:00
|
|
|
if (!dict->Contains("type"))
|
2013-12-18 10:18:57 +01:00
|
|
|
return DeserializeDictionary(dict, safe_mode, attributeTypes);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2013-12-18 10:18:57 +01:00
|
|
|
return DeserializeObject(object, dict, safe_mode, attributeTypes);
|
2013-11-04 23:14:34 +01:00
|
|
|
}
|