icinga2/jsonrpc/jsonrpcmessage.cpp

168 lines
2.9 KiB
C++
Raw Normal View History

2012-03-28 13:24:49 +02:00
#include "i2-jsonrpc.h"
using namespace icinga;
JsonRpcMessage::JsonRpcMessage(void)
{
m_JSON = NULL;
}
JsonRpcMessage::~JsonRpcMessage(void)
{
cJSON_Delete(m_JSON);
}
2012-03-28 21:20:13 +02:00
void JsonRpcMessage::SetJSON(cJSON *object)
2012-03-28 13:24:49 +02:00
{
2012-03-28 21:20:13 +02:00
cJSON_Delete(m_JSON);
m_JSON = object;
2012-03-28 13:24:49 +02:00
}
2012-03-28 21:20:13 +02:00
cJSON *JsonRpcMessage::GetJSON(void)
2012-03-28 13:24:49 +02:00
{
2012-03-28 21:20:13 +02:00
return m_JSON;
}
void JsonRpcMessage::InitJson(void)
{
if (m_JSON == NULL)
m_JSON = cJSON_CreateObject();
}
void JsonRpcMessage::SetFieldObject(const char *field, cJSON *object)
{
if (m_JSON == NULL && object == NULL)
return;
InitJson();
cJSON_DeleteItemFromObject(m_JSON, field);
if (object != NULL)
cJSON_AddItemToObject(m_JSON, field, object);
2012-03-28 13:24:49 +02:00
}
cJSON *JsonRpcMessage::GetFieldObject(const char *field)
2012-03-28 13:24:49 +02:00
{
if (m_JSON == NULL)
return NULL;
return cJSON_GetObjectItem(m_JSON, field);
}
void JsonRpcMessage::SetFieldString(const char *field, const string& value)
{
cJSON *object = cJSON_CreateString(value.c_str());
SetFieldObject(field, object);
}
2012-03-28 13:24:49 +02:00
string JsonRpcMessage::GetFieldString(const char *field)
{
cJSON *object = GetFieldObject(field);
if (object == NULL || object->type != cJSON_String)
2012-03-28 13:24:49 +02:00
return string();
return string(object->valuestring);
2012-03-28 13:24:49 +02:00
}
void JsonRpcMessage::SetVersion(const string& version)
{
SetFieldString("version", version);
}
string JsonRpcMessage::GetVersion(void)
{
return GetFieldString("jsonrpc");
}
void JsonRpcMessage::SetID(const string& id)
2012-03-28 13:24:49 +02:00
{
SetFieldString("id", id);
2012-03-28 13:24:49 +02:00
}
string JsonRpcMessage::GetID(void)
{
return GetFieldString("id");
}
void JsonRpcMessage::SetMethod(const string& method)
2012-03-28 13:24:49 +02:00
{
SetFieldString("method", method);
2012-03-28 13:24:49 +02:00
}
string JsonRpcMessage::GetMethod(void)
{
return GetFieldString("method");
}
void JsonRpcMessage::ClearParams(void)
2012-03-28 13:24:49 +02:00
{
SetFieldObject("params", NULL);
2012-03-28 13:24:49 +02:00
}
cJSON *JsonRpcMessage::GetParams(void)
2012-03-28 13:24:49 +02:00
{
cJSON *object = GetFieldObject("params");
if (object == NULL) {
object = cJSON_CreateObject();
cJSON_AddItemToObject(m_JSON, "params", object);
}
return object;
2012-03-28 13:24:49 +02:00
}
2012-03-31 16:01:31 +02:00
void JsonRpcMessage::SetParam(const string& name, const string& value)
{
}
cJSON *JsonRpcMessage::GetParam(const string& name)
{
cJSON *params = GetFieldObject("params");
if (params == NULL)
return NULL;
return cJSON_GetObjectItem(params, name.c_str());
}
bool JsonRpcMessage::GetParamString(const string name, string *value)
{
cJSON *param = GetParam(name);
if (param == NULL || param->type != cJSON_String)
return false;
*value = param->valuestring;
return true;
}
void JsonRpcMessage::ClearResult(void)
2012-03-28 13:24:49 +02:00
{
SetFieldObject("result", NULL);
2012-03-28 13:24:49 +02:00
}
cJSON *JsonRpcMessage::GetResult(void)
2012-03-28 13:24:49 +02:00
{
cJSON *object = GetFieldObject("result");
if (object == NULL) {
object = cJSON_CreateObject();
cJSON_AddItemToObject(m_JSON, "result", object);
}
return object;
2012-03-28 13:24:49 +02:00
}
void JsonRpcMessage::SetError(const string& error)
2012-03-28 13:24:49 +02:00
{
SetFieldString("error", error);
2012-03-28 13:24:49 +02:00
}
string JsonRpcMessage::GetError(void)
{
return GetFieldString("error");
}