icinga2/jsonrpc/messagepart.cpp

165 lines
4.4 KiB
C++
Raw Normal View History

/******************************************************************************
* 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 *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2012-04-18 15:22:25 +02:00
#include "i2-jsonrpc.h"
#include <cJSON.h>
2012-04-18 15:22:25 +02:00
using namespace icinga;
2012-05-16 11:30:54 +02:00
MessagePart::MessagePart(void)
2012-04-18 15:22:25 +02:00
{
m_Dictionary = make_shared<Dictionary>();
}
2012-05-16 11:30:54 +02:00
MessagePart::MessagePart(string jsonString)
{
json_t *json = cJSON_Parse(jsonString.c_str());
if (!json)
throw InvalidArgumentException("Invalid JSON string");
m_Dictionary = GetDictionaryFromJson(json);
cJSON_Delete(json);
}
2012-05-16 11:30:54 +02:00
MessagePart::MessagePart(const Dictionary::Ptr& dictionary)
2012-04-18 15:22:25 +02:00
{
m_Dictionary = dictionary;
}
2012-05-16 11:30:54 +02:00
MessagePart::MessagePart(const MessagePart& message)
2012-04-18 15:22:25 +02:00
{
m_Dictionary = message.GetDictionary();
}
2012-05-16 11:30:54 +02:00
Dictionary::Ptr MessagePart::GetDictionaryFromJson(json_t *json)
{
Dictionary::Ptr dictionary = make_shared<Dictionary>();
for (cJSON *i = json->child; i != NULL; i = i->next) {
switch (i->type) {
case cJSON_Number:
dictionary->SetProperty(i->string, i->valueint);
break;
case cJSON_String:
dictionary->SetProperty(i->string, i->valuestring);
break;
case cJSON_Object:
dictionary->SetProperty(i->string, GetDictionaryFromJson(i));
break;
default:
break;
}
}
return dictionary;
}
2012-05-16 11:30:54 +02:00
json_t *MessagePart::GetJsonFromDictionary(const Dictionary::Ptr& dictionary)
{
cJSON *json;
string valueString;
Dictionary::Ptr valueDictionary;
json = cJSON_CreateObject();
for (DictionaryIterator i = dictionary->Begin(); i != dictionary->End(); i++) {
switch (i->second.GetType()) {
case VariantInteger:
cJSON_AddNumberToObject(json, i->first.c_str(), i->second.GetInteger());
break;
case VariantString:
valueString = i->second.GetString();
cJSON_AddStringToObject(json, i->first.c_str(), valueString.c_str());
break;
case VariantObject:
valueDictionary = dynamic_pointer_cast<Dictionary>(i->second.GetObject());
if (valueDictionary)
cJSON_AddItemToObject(json, i->first.c_str(), GetJsonFromDictionary(valueDictionary));
default:
break;
}
}
return json;
}
2012-05-16 11:30:54 +02:00
string MessagePart::ToJsonString(void) const
{
json_t *json = GetJsonFromDictionary(m_Dictionary);
char *jsonString;
string result;
#ifdef _DEBUG
jsonString = cJSON_Print(json);
#else /* _DEBUG */
jsonString = cJSON_PrintUnformatted(json);
#endif /* _DEBUG */
cJSON_Delete(json);
result = jsonString;
free(jsonString);
return result;
}
2012-05-16 11:30:54 +02:00
Dictionary::Ptr MessagePart::GetDictionary(void) const
2012-04-18 15:22:25 +02:00
{
return m_Dictionary;
}
2012-04-20 13:49:04 +02:00
2012-05-16 11:30:54 +02:00
bool MessagePart::GetProperty(string key, MessagePart *value) const
2012-04-20 13:49:04 +02:00
{
Dictionary::Ptr dictionary;
2012-05-16 11:30:54 +02:00
if (!GetDictionary()->GetProperty(key, &dictionary))
2012-04-20 13:49:04 +02:00
return false;
2012-05-16 11:30:54 +02:00
*value = MessagePart(dictionary);
2012-04-20 13:49:04 +02:00
return true;
}
2012-05-16 11:30:54 +02:00
void MessagePart::SetProperty(string key, const MessagePart& value)
2012-04-20 13:49:04 +02:00
{
2012-05-16 11:30:54 +02:00
GetDictionary()->SetProperty(key, value.GetDictionary());
2012-04-20 13:49:04 +02:00
}
2012-05-16 11:30:54 +02:00
void MessagePart::AddUnnamedProperty(const MessagePart& value)
2012-04-20 13:49:04 +02:00
{
2012-05-16 11:30:54 +02:00
GetDictionary()->AddUnnamedProperty(value.GetDictionary());
2012-04-20 13:49:04 +02:00
}
2012-05-16 11:30:54 +02:00
DictionaryIterator MessagePart::Begin(void)
{
2012-05-16 11:30:54 +02:00
return GetDictionary()->Begin();
}
2012-05-16 11:30:54 +02:00
DictionaryIterator MessagePart::End(void)
{
2012-05-16 11:30:54 +02:00
return GetDictionary()->End();
}
2012-05-16 11:30:54 +02:00
MessagePart::operator Dictionary::Ptr(void)
{
2012-05-16 11:30:54 +02:00
return GetDictionary();
}