/******************************************************************************
* Icinga 2 *
-* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
+* 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 *
#include "base/application.h"
#include "base/objectlock.h"
#include <boost/foreach.hpp>
-#include <boost/tuple/tuple.hpp>
#include <cJSON.h>
using namespace icinga;
ObjectLock olock(input);
- String key;
- Value value;
- BOOST_FOREACH(boost::tie(key, value), input) {
- result->Set(key, Serialize(value, attributeTypes));
+ BOOST_FOREACH(const Dictionary::Pair& kv, input) {
+ result->Set(kv.first, Serialize(kv.second, attributeTypes));
}
return result;
fields->Set(field.Name, Serialize(input->GetField(i), attributeTypes));
}
- fields->Set("__type", type->GetName());
+ fields->Set("type", type->GetName());
return fields;
}
-static Array::Ptr DeserializeArray(const Array::Ptr& input, int attributeTypes)
+static Array::Ptr DeserializeArray(const Array::Ptr& input, bool safe_mode, int attributeTypes)
{
Array::Ptr result = make_shared<Array>();
ObjectLock olock(input);
BOOST_FOREACH(const Value& value, input) {
- result->Add(Deserialize(value, attributeTypes));
+ result->Add(Deserialize(value, safe_mode, attributeTypes));
}
return result;
}
-static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, int attributeTypes)
+static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
{
Dictionary::Ptr result = make_shared<Dictionary>();
ObjectLock olock(input);
- String key;
- Value value;
- BOOST_FOREACH(boost::tie(key, value), input) {
- result->Set(key, Deserialize(value, attributeTypes));
+ BOOST_FOREACH(const Dictionary::Pair& kv, input) {
+ result->Set(kv.first, Deserialize(kv.second, attributeTypes));
}
return result;
}
-static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, int attributeTypes)
+static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
{
const Type *type;
if (object)
type = object->GetReflectionType();
else
- type = Type::GetByName(input->Get("__type"));
+ type = Type::GetByName(input->Get("type"));
if (!type)
return object;
Object::Ptr instance = object;
- if (!instance)
+ 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."));
+
instance = type->Instantiate();
+ }
- for (int i = 0; i < type->GetFieldCount(); i++) {
- Field field = type->GetFieldInfo(i);
+ ObjectLock olock(input);
+ BOOST_FOREACH(const Dictionary::Pair& kv, input) {
+ if (kv.first.IsEmpty())
+ continue;
- if ((field.Attributes & attributeTypes) == 0)
+ int fid = type->GetFieldId(kv.first);
+
+ if (fid < 0)
continue;
- if (!input->Contains(field.Name))
+ Field field = type->GetFieldInfo(fid);
+
+ if ((field.Attributes & attributeTypes) == 0)
continue;
try {
- instance->SetField(i, Deserialize(input->Get(field.Name), attributeTypes));
+ instance->SetField(fid, Deserialize(kv.second, safe_mode, attributeTypes));
} catch (const std::exception&) {
- instance->SetField(i, Empty);
+ instance->SetField(fid, Empty);
}
}
return SerializeObject(input, attributeTypes);
}
-Value icinga::Deserialize(const Value& value, int attributeTypes)
+Value icinga::Deserialize(const Value& value, bool safe_mode, int attributeTypes)
{
- return Deserialize(Object::Ptr(), value, attributeTypes);
+ return Deserialize(Object::Ptr(), value, safe_mode, attributeTypes);
}
-Value icinga::Deserialize(const Object::Ptr& object, const Value& value, int attributeTypes)
+Value icinga::Deserialize(const Object::Ptr& object, const Value& value, bool safe_mode, int attributeTypes)
{
if (!value.IsObject())
return value;
Array::Ptr array = dynamic_pointer_cast<Array>(input);
if (array != NULL)
- return DeserializeArray(array, attributeTypes);
+ return DeserializeArray(array, safe_mode, attributeTypes);
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
ASSERT(dict != NULL);
- if (!dict->Contains("__type"))
- return DeserializeDictionary(dict, attributeTypes);
+ if (!dict->Contains("type"))
+ return DeserializeDictionary(dict, safe_mode, attributeTypes);
- return DeserializeObject(object, dict, attributeTypes);
+ return DeserializeObject(object, dict, safe_mode, attributeTypes);
}