mirror of https://github.com/Icinga/icinga2.git
Implement proper support for JSON arrays.
This commit is contained in:
parent
a38263b3c8
commit
98ba1424b5
|
@ -7,6 +7,8 @@ pkglib_LTLIBRARIES = \
|
||||||
libbase_la_SOURCES = \
|
libbase_la_SOURCES = \
|
||||||
application.cpp \
|
application.cpp \
|
||||||
application.h \
|
application.h \
|
||||||
|
array.cpp \
|
||||||
|
array.h \
|
||||||
asynctask.h \
|
asynctask.h \
|
||||||
attribute.cpp \
|
attribute.cpp \
|
||||||
attribute.h \
|
attribute.h \
|
||||||
|
|
|
@ -0,0 +1,234 @@
|
||||||
|
/******************************************************************************
|
||||||
|
* Icinga 2 *
|
||||||
|
* Copyright (C) 2012 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. *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include "i2-base.h"
|
||||||
|
#include <cJSON.h>
|
||||||
|
|
||||||
|
using namespace icinga;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for the Array class.
|
||||||
|
*/
|
||||||
|
Array::Array(void)
|
||||||
|
: m_Sealed(false)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restrieves a value from an array.
|
||||||
|
*
|
||||||
|
* @param index The index..
|
||||||
|
* @returns The value.
|
||||||
|
* @threadsafety Always.
|
||||||
|
*/
|
||||||
|
Value Array::Get(unsigned int index) const
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
return m_Data.at(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in the array.
|
||||||
|
*
|
||||||
|
* @param index The index.
|
||||||
|
* @param value The value.
|
||||||
|
* @threadsafety Always.
|
||||||
|
*/
|
||||||
|
void Array::Set(unsigned int index, const Value& value)
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
ASSERT(!m_Sealed);
|
||||||
|
m_Data.at(index) = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a value to the array.
|
||||||
|
*
|
||||||
|
* @param value The value.
|
||||||
|
* @threadsafety Always.
|
||||||
|
*/
|
||||||
|
void Array::Add(const Value& value)
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
ASSERT(!m_Sealed);
|
||||||
|
m_Data.push_back(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an iterator to the beginning of the array.
|
||||||
|
*
|
||||||
|
* @returns An iterator.
|
||||||
|
*/
|
||||||
|
Array::Iterator Array::Begin(void)
|
||||||
|
{
|
||||||
|
ASSERT(OwnsLock());
|
||||||
|
|
||||||
|
return m_Data.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an iterator to the end of the array.
|
||||||
|
*
|
||||||
|
* @returns An iterator.
|
||||||
|
*/
|
||||||
|
Array::Iterator Array::End(void)
|
||||||
|
{
|
||||||
|
ASSERT(OwnsLock());
|
||||||
|
|
||||||
|
return m_Data.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of elements in the array.
|
||||||
|
*
|
||||||
|
* @returns Number of elements.
|
||||||
|
* @threadsafety Always.
|
||||||
|
*/
|
||||||
|
size_t Array::GetLength(void) const
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
return m_Data.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the specified index from the array.
|
||||||
|
*
|
||||||
|
* @param index The index.
|
||||||
|
* @threadsafety Always.
|
||||||
|
*/
|
||||||
|
void Array::Remove(unsigned int index)
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
ASSERT(!m_Sealed);
|
||||||
|
m_Data.erase(m_Data.begin() + index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the item specified by the iterator from the array.
|
||||||
|
*
|
||||||
|
* @param it The iterator.
|
||||||
|
*/
|
||||||
|
void Array::Remove(Array::Iterator it)
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
ASSERT(!m_Sealed);
|
||||||
|
m_Data.erase(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks the array as read-only. Attempting to modify a sealed
|
||||||
|
* array is an error.
|
||||||
|
*/
|
||||||
|
void Array::Seal(void)
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
m_Sealed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the array is sealed.
|
||||||
|
*
|
||||||
|
* @returns true if the array is sealed, false otherwise.
|
||||||
|
*/
|
||||||
|
bool Array::IsSealed(void) const
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
return m_Sealed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes a shallow copy of an array.
|
||||||
|
*
|
||||||
|
* @returns a copy of the array.
|
||||||
|
* @threadsafety Always.
|
||||||
|
*/
|
||||||
|
Array::Ptr Array::ShallowClone(void) const
|
||||||
|
{
|
||||||
|
ASSERT(!OwnsLock());
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
Array::Ptr clone = boost::make_shared<Array>();
|
||||||
|
|
||||||
|
std::copy(m_Data.begin(), m_Data.end(), std::back_inserter(clone->m_Data));
|
||||||
|
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a JSON object to an array.
|
||||||
|
*
|
||||||
|
* @param json The JSON object.
|
||||||
|
* @returns An array that is equivalent to the JSON object.
|
||||||
|
* @threadsafety Always.
|
||||||
|
*/
|
||||||
|
Array::Ptr Array::FromJson(cJSON *json)
|
||||||
|
{
|
||||||
|
Array::Ptr array = boost::make_shared<Array>();
|
||||||
|
|
||||||
|
if (json->type != cJSON_Array)
|
||||||
|
BOOST_THROW_EXCEPTION(invalid_argument("JSON type must be cJSON_Array."));
|
||||||
|
|
||||||
|
for (cJSON *i = json->child; i != NULL; i = i->next) {
|
||||||
|
array->Add(Value::FromJson(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
array->Seal();
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts this array to a JSON object.
|
||||||
|
*
|
||||||
|
* @returns A JSON object that is equivalent to the array. Values that
|
||||||
|
* cannot be represented in JSON are omitted.
|
||||||
|
* @threadsafety Always.
|
||||||
|
*/
|
||||||
|
cJSON *Array::ToJson(void) const
|
||||||
|
{
|
||||||
|
cJSON *json = cJSON_CreateArray();
|
||||||
|
|
||||||
|
try {
|
||||||
|
ObjectLock olock(this);
|
||||||
|
|
||||||
|
BOOST_FOREACH(const Value& value, m_Data) {
|
||||||
|
cJSON_AddItemToArray(json, value.ToJson());
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
cJSON_Delete(json);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
|
@ -0,0 +1,98 @@
|
||||||
|
/******************************************************************************
|
||||||
|
* Icinga 2 *
|
||||||
|
* Copyright (C) 2012 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. *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#ifndef ARRAY_H
|
||||||
|
#define ARRAY_H
|
||||||
|
|
||||||
|
namespace icinga
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An array of Value items.
|
||||||
|
*
|
||||||
|
* @ingroup base
|
||||||
|
*/
|
||||||
|
class I2_BASE_API Array : public Object
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef shared_ptr<Array> Ptr;
|
||||||
|
typedef weak_ptr<Array> WeakPtr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An iterator that can be used to iterate over array elements.
|
||||||
|
*/
|
||||||
|
typedef vector<Value>::iterator Iterator;
|
||||||
|
|
||||||
|
Array(void);
|
||||||
|
|
||||||
|
Value Get(unsigned int index) const;
|
||||||
|
void Set(unsigned int index, const Value& value);
|
||||||
|
void Add(const Value& value);
|
||||||
|
|
||||||
|
void Seal(void);
|
||||||
|
bool IsSealed(void) const;
|
||||||
|
|
||||||
|
Iterator Begin(void);
|
||||||
|
Iterator End(void);
|
||||||
|
|
||||||
|
size_t GetLength(void) const;
|
||||||
|
|
||||||
|
void Remove(unsigned int index);
|
||||||
|
void Remove(Iterator it);
|
||||||
|
|
||||||
|
Array::Ptr ShallowClone(void) const;
|
||||||
|
|
||||||
|
static Array::Ptr FromJson(cJSON *json);
|
||||||
|
cJSON *ToJson(void) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
vector<Value> m_Data; /**< The data for the array. */
|
||||||
|
bool m_Sealed; /**< Whether the array is read-only. */
|
||||||
|
};
|
||||||
|
|
||||||
|
inline Array::Iterator range_begin(Array::Ptr x)
|
||||||
|
{
|
||||||
|
return x->Begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Array::Iterator range_end(Array::Ptr x)
|
||||||
|
{
|
||||||
|
return x->End();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace boost
|
||||||
|
{
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct range_mutable_iterator<icinga::Array::Ptr>
|
||||||
|
{
|
||||||
|
typedef icinga::Array::Iterator type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct range_const_iterator<icinga::Array::Ptr>
|
||||||
|
{
|
||||||
|
typedef icinga::Array::Iterator type;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* ARRAY_H */
|
|
@ -20,6 +20,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="application.cpp" />
|
<ClCompile Include="application.cpp" />
|
||||||
|
<ClCompile Include="array.cpp" />
|
||||||
<ClCompile Include="attribute.cpp" />
|
<ClCompile Include="attribute.cpp" />
|
||||||
<ClCompile Include="component.cpp" />
|
<ClCompile Include="component.cpp" />
|
||||||
<ClCompile Include="connection.cpp" />
|
<ClCompile Include="connection.cpp" />
|
||||||
|
@ -63,6 +64,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="application.h" />
|
<ClInclude Include="application.h" />
|
||||||
|
<ClInclude Include="array.h" />
|
||||||
<ClInclude Include="asynctask.h" />
|
<ClInclude Include="asynctask.h" />
|
||||||
<ClInclude Include="attribute.h" />
|
<ClInclude Include="attribute.h" />
|
||||||
<ClInclude Include="component.h" />
|
<ClInclude Include="component.h" />
|
||||||
|
|
|
@ -109,6 +109,9 @@
|
||||||
<ClCompile Include="stacktrace.cpp">
|
<ClCompile Include="stacktrace.cpp">
|
||||||
<Filter>Quelldateien</Filter>
|
<Filter>Quelldateien</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="array.cpp">
|
||||||
|
<Filter>Quelldateien</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="application.h">
|
<ClInclude Include="application.h">
|
||||||
|
@ -222,6 +225,9 @@
|
||||||
<ClInclude Include="stacktrace.h">
|
<ClInclude Include="stacktrace.h">
|
||||||
<Filter>Headerdateien</Filter>
|
<Filter>Headerdateien</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="array.h">
|
||||||
|
<Filter>Headerdateien</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Filter Include="Quelldateien">
|
<Filter Include="Quelldateien">
|
||||||
|
|
|
@ -222,6 +222,7 @@ void Dictionary::Remove(const String& key)
|
||||||
if (it == m_Data.end())
|
if (it == m_Data.end())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
ASSERT(!m_Sealed);
|
||||||
m_Data.erase(it);
|
m_Data.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,7 +236,7 @@ void Dictionary::Remove(Dictionary::Iterator it)
|
||||||
ASSERT(!OwnsLock());
|
ASSERT(!OwnsLock());
|
||||||
ObjectLock olock(this);
|
ObjectLock olock(this);
|
||||||
|
|
||||||
String key = it->first;
|
ASSERT(!m_Sealed);
|
||||||
m_Data.erase(it);
|
m_Data.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -201,6 +201,7 @@ namespace signals2 = boost::signals2;
|
||||||
#include "value.h"
|
#include "value.h"
|
||||||
#include "convert.h"
|
#include "convert.h"
|
||||||
#include "dictionary.h"
|
#include "dictionary.h"
|
||||||
|
#include "array.h"
|
||||||
#include "ringbuffer.h"
|
#include "ringbuffer.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
|
|
|
@ -37,6 +37,9 @@ public:
|
||||||
typedef shared_ptr<Object> Ptr;
|
typedef shared_ptr<Object> Ptr;
|
||||||
typedef weak_ptr<Object> WeakPtr;
|
typedef weak_ptr<Object> WeakPtr;
|
||||||
|
|
||||||
|
Object(void);
|
||||||
|
virtual ~Object(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds a shared pointer and provides support for implicit upcasts.
|
* Holds a shared pointer and provides support for implicit upcasts.
|
||||||
*
|
*
|
||||||
|
@ -96,9 +99,6 @@ public:
|
||||||
#endif /* _DEBUG */
|
#endif /* _DEBUG */
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Object(void);
|
|
||||||
virtual ~Object(void);
|
|
||||||
|
|
||||||
SharedPtrHolder GetSelf(void);
|
SharedPtrHolder GetSelf(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -105,17 +105,11 @@ Value Value::FromJson(cJSON *json)
|
||||||
return 0;
|
return 0;
|
||||||
else if (json->type == cJSON_Object)
|
else if (json->type == cJSON_Object)
|
||||||
return Dictionary::FromJson(json);
|
return Dictionary::FromJson(json);
|
||||||
|
else if (json->type == cJSON_Array)
|
||||||
|
return Array::FromJson(json);
|
||||||
else if (json->type == cJSON_NULL)
|
else if (json->type == cJSON_NULL)
|
||||||
return Value();
|
return Value();
|
||||||
else if (json->type == cJSON_Array) {
|
else
|
||||||
Dictionary::Ptr dict = boost::make_shared<Dictionary>();
|
|
||||||
|
|
||||||
for (cJSON *i = json->child; i != NULL; i = i->next) {
|
|
||||||
dict->Add(Value::FromJson(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
return dict;
|
|
||||||
} else
|
|
||||||
BOOST_THROW_EXCEPTION(invalid_argument("Unsupported JSON type."));
|
BOOST_THROW_EXCEPTION(invalid_argument("Unsupported JSON type."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,6 +156,9 @@ cJSON *Value::ToJson(void) const
|
||||||
if (IsObjectType<Dictionary>()) {
|
if (IsObjectType<Dictionary>()) {
|
||||||
Dictionary::Ptr dictionary = *this;
|
Dictionary::Ptr dictionary = *this;
|
||||||
return dictionary->ToJson();
|
return dictionary->ToJson();
|
||||||
|
} else if (IsObjectType<Array>()) {
|
||||||
|
Array::Ptr array = *this;
|
||||||
|
return array->ToJson();
|
||||||
} else {
|
} else {
|
||||||
Logger::Write(LogDebug, "base", "Ignoring unknown object while converting variant to JSON.");
|
Logger::Write(LogDebug, "base", "Ignoring unknown object while converting variant to JSON.");
|
||||||
return cJSON_CreateNull();
|
return cJSON_CreateNull();
|
||||||
|
|
Loading…
Reference in New Issue