icinga2/jsonrpc/jsonrpcmessage.cpp

108 lines
1.8 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::SetFieldString(const char *field, const string& value)
{
if (m_JSON == NULL)
m_JSON = cJSON_CreateObject();
cJSON *object = cJSON_CreateString(value.c_str());
cJSON_DeleteItemFromObject(m_JSON, field);
cJSON_AddItemToObject(m_JSON, field, object);
2012-03-28 13:24:49 +02:00
}
string JsonRpcMessage::GetFieldString(const char *field)
{
if (m_JSON == NULL)
m_JSON = cJSON_CreateObject();
2012-03-28 13:24:49 +02:00
cJSON *idObject = cJSON_GetObjectItem(m_JSON, field);
if (idObject == NULL || idObject->type != cJSON_String)
return string();
return string(idObject->valuestring);
}
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::SetParams(const string& params)
2012-03-28 13:24:49 +02:00
{
SetFieldString("params", params);
2012-03-28 13:24:49 +02:00
}
string JsonRpcMessage::GetParams(void)
{
return GetFieldString("params");
}
void JsonRpcMessage::SetResult(const string& result)
2012-03-28 13:24:49 +02:00
{
SetFieldString("result", result);
2012-03-28 13:24:49 +02:00
}
string JsonRpcMessage::GetResult(void)
{
return GetFieldString("result");
}
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");
}