Implement proper support for JSON arrays.

This commit is contained in:
Gunnar Beutner 2013-03-10 09:08:59 +01:00
parent a38263b3c8
commit 98ba1424b5
9 changed files with 354 additions and 13 deletions

View File

@ -7,6 +7,8 @@ pkglib_LTLIBRARIES = \
libbase_la_SOURCES = \
application.cpp \
application.h \
array.cpp \
array.h \
asynctask.h \
attribute.cpp \
attribute.h \

234
lib/base/array.cpp Normal file
View File

@ -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;
}

98
lib/base/array.h Normal file
View File

@ -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 */

View File

@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="application.cpp" />
<ClCompile Include="array.cpp" />
<ClCompile Include="attribute.cpp" />
<ClCompile Include="component.cpp" />
<ClCompile Include="connection.cpp" />
@ -63,6 +64,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="application.h" />
<ClInclude Include="array.h" />
<ClInclude Include="asynctask.h" />
<ClInclude Include="attribute.h" />
<ClInclude Include="component.h" />

View File

@ -109,6 +109,9 @@
<ClCompile Include="stacktrace.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="array.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="application.h">
@ -222,6 +225,9 @@
<ClInclude Include="stacktrace.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="array.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Quelldateien">

View File

@ -222,6 +222,7 @@ void Dictionary::Remove(const String& key)
if (it == m_Data.end())
return;
ASSERT(!m_Sealed);
m_Data.erase(it);
}
@ -235,7 +236,7 @@ void Dictionary::Remove(Dictionary::Iterator it)
ASSERT(!OwnsLock());
ObjectLock olock(this);
String key = it->first;
ASSERT(!m_Sealed);
m_Data.erase(it);
}

View File

@ -201,6 +201,7 @@ namespace signals2 = boost::signals2;
#include "value.h"
#include "convert.h"
#include "dictionary.h"
#include "array.h"
#include "ringbuffer.h"
#include "timer.h"
#include "stream.h"

View File

@ -37,6 +37,9 @@ public:
typedef shared_ptr<Object> Ptr;
typedef weak_ptr<Object> WeakPtr;
Object(void);
virtual ~Object(void);
/**
* Holds a shared pointer and provides support for implicit upcasts.
*
@ -96,9 +99,6 @@ public:
#endif /* _DEBUG */
protected:
Object(void);
virtual ~Object(void);
SharedPtrHolder GetSelf(void);
private:

View File

@ -105,17 +105,11 @@ Value Value::FromJson(cJSON *json)
return 0;
else if (json->type == cJSON_Object)
return Dictionary::FromJson(json);
else if (json->type == cJSON_Array)
return Array::FromJson(json);
else if (json->type == cJSON_NULL)
return Value();
else if (json->type == cJSON_Array) {
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
else
BOOST_THROW_EXCEPTION(invalid_argument("Unsupported JSON type."));
}
@ -162,6 +156,9 @@ cJSON *Value::ToJson(void) const
if (IsObjectType<Dictionary>()) {
Dictionary::Ptr dictionary = *this;
return dictionary->ToJson();
} else if (IsObjectType<Array>()) {
Array::Ptr array = *this;
return array->ToJson();
} else {
Logger::Write(LogDebug, "base", "Ignoring unknown object while converting variant to JSON.");
return cJSON_CreateNull();