1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/serializer.hpp"
#include "base/type.hpp"
#include "base/application.hpp"
#include "base/objectlock.hpp"
#include "base/convert.hpp"
#include "base/exception.hpp"
#include "base/namespace.hpp"
#include <boost/algorithm/string/join.hpp>
#include <deque>
#include <utility>
using namespace icinga;
struct SerializeStackEntry
{
String Name;
Value Val;
};
CircularReferenceError::CircularReferenceError(String message, std::vector<String> path)
: m_Message(message), m_Path(path)
{ }
const char *CircularReferenceError::what(void) const throw()
{
return m_Message.CStr();
}
std::vector<String> CircularReferenceError::GetPath() const
{
return m_Path;
}
struct SerializeStack
{
std::deque<SerializeStackEntry> Entries;
inline void Push(const String& name, const Value& val)
{
Object::Ptr obj;
if (val.IsObject())
obj = val;
if (obj) {
for (const auto& entry : Entries) {
if (entry.Val == obj) {
std::vector<String> path;
for (const auto& entry : Entries)
path.push_back(entry.Name);
path.push_back(name);
BOOST_THROW_EXCEPTION(CircularReferenceError("Cannot serialize object which recursively refers to itself. Attribute path which leads to the cycle: " + boost::algorithm::join(path, " -> "), path));
}
}
}
Entries.push_back({ name, obj });
}
inline void Pop()
{
Entries.pop_back();
}
};
static Value SerializeInternal(const Value& value, int attributeTypes, SerializeStack& stack, bool dryRun);
static Array::Ptr SerializeArray(const Array::Ptr& input, int attributeTypes, SerializeStack& stack, bool dryRun)
{
ArrayData result;
if (!dryRun) {
result.reserve(input->GetLength());
}
ObjectLock olock(input);
int index = 0;
for (const Value& value : input) {
stack.Push(Convert::ToString(index), value);
auto serialized (SerializeInternal(value, attributeTypes, stack, dryRun));
if (!dryRun) {
result.emplace_back(std::move(serialized));
}
stack.Pop();
index++;
}
return dryRun ? nullptr : new Array(std::move(result));
}
static Dictionary::Ptr SerializeDictionary(const Dictionary::Ptr& input, int attributeTypes, SerializeStack& stack, bool dryRun)
{
DictionaryData result;
if (!dryRun) {
result.reserve(input->GetLength());
}
ObjectLock olock(input);
for (const Dictionary::Pair& kv : input) {
stack.Push(kv.first, kv.second);
auto serialized (SerializeInternal(kv.second, attributeTypes, stack, dryRun));
if (!dryRun) {
result.emplace_back(kv.first, std::move(serialized));
}
stack.Pop();
}
return dryRun ? nullptr : new Dictionary(std::move(result));
}
static Dictionary::Ptr SerializeNamespace(const Namespace::Ptr& input, int attributeTypes, SerializeStack& stack, bool dryRun)
{
DictionaryData result;
if (!dryRun) {
result.reserve(input->GetLength());
}
ObjectLock olock(input);
for (const Namespace::Pair& kv : input) {
Value val = kv.second.Val;
stack.Push(kv.first, val);
auto serialized (SerializeInternal(val, attributeTypes, stack, dryRun));
if (!dryRun) {
result.emplace_back(kv.first, std::move(serialized));
}
stack.Pop();
}
return dryRun ? nullptr : new Dictionary(std::move(result));
}
static Object::Ptr SerializeObject(const Object::Ptr& input, int attributeTypes, SerializeStack& stack, bool dryRun)
{
Type::Ptr type = input->GetReflectionType();
if (!type)
return nullptr;
DictionaryData fields;
if (!dryRun) {
fields.reserve(type->GetFieldCount() + 1);
}
ObjectLock olock(input);
for (int i = 0; i < type->GetFieldCount(); i++) {
Field field = type->GetFieldInfo(i);
if (attributeTypes != 0 && (field.Attributes & attributeTypes) == 0)
continue;
if (strcmp(field.Name, "type") == 0)
continue;
Value value = input->GetField(i);
stack.Push(field.Name, value);
auto serialized (SerializeInternal(value, attributeTypes, stack, dryRun));
if (!dryRun) {
fields.emplace_back(field.Name, std::move(serialized));
}
stack.Pop();
}
if (!dryRun) {
fields.emplace_back("type", type->GetName());
}
return dryRun ? nullptr : new Dictionary(std::move(fields));
}
static Array::Ptr DeserializeArray(const Array::Ptr& input, bool safe_mode, int attributeTypes)
{
ArrayData result;
result.reserve(input->GetLength());
ObjectLock olock(input);
for (const Value& value : input) {
result.emplace_back(Deserialize(value, safe_mode, attributeTypes));
}
return new Array(std::move(result));
}
static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
{
DictionaryData result;
result.reserve(input->GetLength());
ObjectLock olock(input);
for (const Dictionary::Pair& kv : input) {
result.emplace_back(kv.first, Deserialize(kv.second, safe_mode, attributeTypes));
}
return new Dictionary(std::move(result));
}
static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
{
if (!object && safe_mode)
BOOST_THROW_EXCEPTION(std::runtime_error("Tried to instantiate object while safe mode is enabled."));
Type::Ptr type;
if (object)
type = object->GetReflectionType();
else
type = Type::GetByName(input->Get("type"));
if (!type)
return object;
Object::Ptr instance;
if (object)
instance = object;
else
instance = type->Instantiate(std::vector<Value>());
ObjectLock olock(input);
for (const Dictionary::Pair& kv : input) {
if (kv.first.IsEmpty())
continue;
int fid = type->GetFieldId(kv.first);
if (fid < 0)
continue;
Field field = type->GetFieldInfo(fid);
if ((field.Attributes & attributeTypes) == 0)
continue;
try {
instance->SetField(fid, Deserialize(kv.second, safe_mode, attributeTypes), true);
} catch (const std::exception&) {
instance->SetField(fid, Empty);
}
}
return instance;
}
static Value SerializeInternal(const Value& value, int attributeTypes, SerializeStack& stack, bool dryRun)
{
if (!value.IsObject())
return dryRun ? Empty : value;
Object::Ptr input = value;
Array::Ptr array = dynamic_pointer_cast<Array>(input);
if (array)
return SerializeArray(array, attributeTypes, stack, dryRun);
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
if (dict)
return SerializeDictionary(dict, attributeTypes, stack, dryRun);
Namespace::Ptr ns = dynamic_pointer_cast<Namespace>(input);
if (ns)
return SerializeNamespace(ns, attributeTypes, stack, dryRun);
return SerializeObject(input, attributeTypes, stack, dryRun);
}
void icinga::AssertNoCircularReferences(const Value& value)
{
SerializeStack stack;
SerializeInternal(value, FAConfig, stack, true);
}
Value icinga::Serialize(const Value& value, int attributeTypes)
{
SerializeStack stack;
return SerializeInternal(value, attributeTypes, stack, false);
}
Value icinga::Deserialize(const Value& value, bool safe_mode, int attributeTypes)
{
return Deserialize(nullptr, value, safe_mode, attributeTypes);
}
Value icinga::Deserialize(const Object::Ptr& object, const Value& value, bool safe_mode, int attributeTypes)
{
if (!value.IsObject())
return value;
Object::Ptr input = value;
Array::Ptr array = dynamic_pointer_cast<Array>(input);
if (array)
return DeserializeArray(array, safe_mode, attributeTypes);
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
ASSERT(dict);
if ((safe_mode && !object) || !dict->Contains("type"))
return DeserializeDictionary(dict, safe_mode, attributeTypes);
else
return DeserializeObject(object, dict, safe_mode, attributeTypes);
}
|