</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
- <IncludePath>$(SolutionDir)\third-party\mmatch;$(IncludePath)</IncludePath>
+ <IncludePath>$(SolutionDir)\third-party\mmatch;$(SolutionDir)\third-party\cJSON;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
- <IncludePath>$(SolutionDir)\third-party\mmatch;$(IncludePath)</IncludePath>
+ <IncludePath>$(SolutionDir)\third-party\mmatch;$(SolutionDir)\third-party\cJSON;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>ws2_32.lib;shlwapi.lib;mmatch.lib;libeay32MTd.lib;ssleay32MTd.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>ws2_32.lib;shlwapi.lib;mmatch.lib;cJSON.lib;libeay32MTd.lib;ssleay32MTd.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Lib>
<AdditionalDependencies>ws2_32.lib;shlwapi.lib</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>ws2_32.lib;shlwapi.lib;mmatch.lib;libeay32MT.lib;ssleay32MT.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>ws2_32.lib;shlwapi.lib;mmatch.lib;cJSON.lib;libeay32MT.lib;ssleay32MT.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Lib>
<AdditionalDependencies>ws2_32.lib;shlwapi.lib</AdditionalDependencies>
#endif /* _WIN32 */
}
-/**
- * Constructor for the Component class.
- */
-Component::Component(void)
- : m_ModuleHandle(0)
-{ }
-
-/**
- * Destructor for the Component class.
- */
-Component::~Component(void)
-{ }
-
/**
* Retrieves the name of the component.
*
typedef shared_ptr<Component> Ptr;
typedef weak_ptr<Component> WeakPtr;
- Component(void);
- virtual ~Component(void);
-
ConfigObject::Ptr GetConfig(void) const;
virtual void Start(void);
******************************************************************************/
#include "i2-base.h"
+#include <cJSON.h>
using namespace icinga;
m_Data.erase(it);
}
+
+/**
+ * Converts a JSON object to a dictionary.
+ *
+ * @param json The JSON object.
+ * @returns A dictionary that is equivalent to the JSON object.
+ */
+Dictionary::Ptr Dictionary::FromJson(cJSON *json)
+{
+ Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
+
+ if (json->type != cJSON_Object)
+ throw invalid_argument("JSON type must be cJSON_Object.");
+
+ for (cJSON *i = json->child; i != NULL; i = i->next) {
+ dictionary->Set(i->string, Variant::FromJson(i));
+ }
+
+ return dictionary;
+}
+
+/**
+ * Converts a dictionary to a JSON object.
+ *
+ * @param dictionary The dictionary.
+ * @returns A JSON object that is equivalent to the dictionary. Values that
+ * cannot be represented in JSON are omitted.
+ */
+cJSON *Dictionary::ToJson(void) const
+{
+ cJSON *json = cJSON_CreateObject();
+
+ try {
+ string key;
+ Variant value;
+ BOOST_FOREACH(tie(key, value), m_Data) {
+ cJSON_AddItemToObject(json, key.c_str(), value.ToJson());
+ }
+ } catch (...) {
+ cJSON_Delete(json);
+ throw;
+ }
+
+ return json;
+}
\ No newline at end of file
void Remove(const string& key);
+ static Dictionary::Ptr FromJson(cJSON *json);
+ cJSON *ToJson(void) const;
+
private:
map<string, Variant> m_Data;
};
return result;
}
+
+/**
+ * Null deleter. Used as a parameter for the shared_ptr constructor.
+ *
+ * @param obj The object that should be deleted.
+ */
+void Utility::NullDeleter(void *obj)
+{
+ /* Nothing to do here. */
+}
static string DirName(const string& path);
static string BaseName(const string& path);
+ static void NullDeleter(void *obj);
+
private:
static bool m_SSLInitialized;
******************************************************************************/
#include "i2-base.h"
+#include <cJSON.h>
using namespace icinga;
{
return !IsEmpty() && (m_Value.type() == typeid(Object::Ptr));
}
+
+Variant Variant::FromJson(cJSON *json)
+{
+ if (json->type == cJSON_Number)
+ return json->valuedouble;
+ else if (json->type == cJSON_String)
+ return json->valuestring;
+ else if (json->type == cJSON_True)
+ return 1;
+ else if (json->type == cJSON_False)
+ return 0;
+ else if (json->type == cJSON_Object)
+ return Dictionary::FromJson(json);
+ else if (json->type == cJSON_NULL)
+ return Variant();
+ else
+ throw invalid_argument("Unsupported JSON type.");
+}
+
+string Variant::Serialize(void) const
+{
+ cJSON *json = ToJson();
+
+ char *jsonString;
+
+ if (!Application::GetInstance()->IsDebugging())
+ jsonString = cJSON_Print(json);
+ else
+ jsonString = cJSON_PrintUnformatted(json);
+
+ cJSON_Delete(json);
+
+ string result = jsonString;
+
+ free(jsonString);
+
+ return result;
+}
+
+cJSON *Variant::ToJson(void) const
+{
+ if (m_Value.type() == typeid(long)) {
+ return cJSON_CreateNumber(boost::get<long>(m_Value));
+ } else if (m_Value.type() == typeid(double)) {
+ return cJSON_CreateNumber(boost::get<double>(m_Value));
+ } else if (m_Value.type() == typeid(string)) {
+ return cJSON_CreateString(boost::get<string>(m_Value).c_str());
+ } else if (m_Value.type() == typeid(Object::Ptr)) {
+ if (IsObjectType<Dictionary>()) {
+ Dictionary::Ptr dictionary = *this;
+ return dictionary->ToJson();
+ } else {
+ Logger::Write(LogDebug, "base", "Ignoring unknown object while converting variant to JSON.");
+ return cJSON_CreateNull();
+ }
+ } else {
+ throw runtime_error("Invalid variant type.");
+ }
+}
+
+Variant Variant::Deserialize(const string& jsonString)
+{
+ cJSON *json = cJSON_Parse(jsonString.c_str());
+
+ if (!json)
+ throw_exception(runtime_error("Invalid JSON string"));
+
+ Variant value = FromJson(json);
+ cJSON_Delete(json);
+
+ return value;
+}
#ifndef VARIANT_H
#define VARIANT_H
+struct cJSON;
+
namespace icinga
{
return (dynamic_pointer_cast<T>(boost::get<Object::Ptr>(m_Value)));
}
+ static Variant FromJson(cJSON *json);
+ cJSON *ToJson(void) const;
+
+ string Serialize(void) const;
+ static Variant Deserialize(const string& jsonString);
+
private:
mutable boost::variant<boost::blank, long, double, string, Object::Ptr> m_Value;
};
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
- <IncludePath>$(SolutionDir)\base;$(SolutionDir)\third-party\cJSON;$(IncludePath)</IncludePath>
+ <IncludePath>$(SolutionDir)\base;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
- <IncludePath>$(SolutionDir)\base;$(SolutionDir)\third-party\cJSON;$(IncludePath)</IncludePath>
+ <IncludePath>$(SolutionDir)\base;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>base.lib;cJSON.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>base.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Lib>
<AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>base.lib;cJSON.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>base.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Lib>
<AdditionalDependencies>
*/
void JsonRpcClient::SendMessage(const MessagePart& message)
{
- Netstring::WriteStringToIOQueue(this, message.ToJsonString());
+ Variant value = message.GetDictionary();
+ Netstring::WriteStringToIOQueue(this, value.Serialize());
}
/**
while (Netstring::ReadStringFromIOQueue(this, &jsonString)) {
try {
- OnNewMessage(GetSelf(), MessagePart(jsonString));
+ Variant value = Variant::Deserialize(jsonString);
+
+ if (!value.IsObjectType<Dictionary>())
+ throw invalid_argument("JSON-RPC message must be a dictionary.");
+
+ OnNewMessage(GetSelf(), MessagePart(value));
} catch (const exception& ex) {
Logger::Write(LogCritical, "jsonrpc", "Exception while processing message from JSON-RPC client: " + string(ex.what()));
}
******************************************************************************/
#include "i2-jsonrpc.h"
-#include <cJSON.h>
using namespace icinga;
m_Dictionary = boost::make_shared<Dictionary>();
}
-/**
- * Constructor for the MessagePart class.
- *
- * @param jsonString The JSON string that should be used to initialize
- * the message.
- */
-MessagePart::MessagePart(string jsonString)
-{
- json_t *json = cJSON_Parse(jsonString.c_str());
-
- if (!json)
- throw_exception(runtime_error("Invalid JSON string"));
-
- m_Dictionary = GetDictionaryFromJson(json);
-
- cJSON_Delete(json);
-}
-
/**
* Constructor for the MessagePart class.
*
m_Dictionary = message.GetDictionary();
}
-/**
- * Converts a JSON object to a dictionary.
- *
- * @param json The JSON object.
- * @returns A dictionary that is equivalent to the JSON object.
- */
-Dictionary::Ptr MessagePart::GetDictionaryFromJson(json_t *json)
-{
- Dictionary::Ptr dictionary = boost::make_shared<Dictionary>();
-
- for (cJSON *i = json->child; i != NULL; i = i->next) {
- switch (i->type) {
- case cJSON_Number:
- dictionary->Set(i->string, i->valueint);
- break;
- case cJSON_String:
- dictionary->Set(i->string, i->valuestring);
- break;
- case cJSON_Object:
- dictionary->Set(i->string, GetDictionaryFromJson(i));
- break;
- default:
- break;
- }
- }
-
- return dictionary;
-}
-
-/**
- * Converts a dictionary to a JSON object.
- *
- * @param dictionary The dictionary.
- * @returns A JSON object that is equivalent to the dictionary. Values that
- * cannot be represented in JSON are omitted.
- */
-json_t *MessagePart::GetJsonFromDictionary(const Dictionary::Ptr& dictionary)
-{
- cJSON *json;
- string valueString;
- Dictionary::Ptr valueDictionary;
-
- json = cJSON_CreateObject();
-
- for (Dictionary::Iterator i = dictionary->Begin(); i != dictionary->End(); i++) {
- if (i->second.IsScalar()) {
- valueString = static_cast<string>(i->second);
- cJSON_AddStringToObject(json, i->first.c_str(), valueString.c_str());
- } else if (i->second.IsObjectType<Dictionary>()) {
- cJSON_AddItemToObject(json, i->first.c_str(), GetJsonFromDictionary(i->second));
- } else {
- stringstream msgbuf;
- msgbuf << "Dictionary serialization: Ignored property '" << i->first << "' (unknown type)";
- Logger::Write(LogDebug, "jsonrpc", msgbuf.str());
- }
- }
-
- return json;
-}
-
-/**
- * Converts a message into a JSON string.
- *
- * @returns A JSON string representing the message.
- */
-string MessagePart::ToJsonString(void) const
-{
- json_t *json = GetJsonFromDictionary(m_Dictionary);
- char *jsonString;
- string result;
-
- if (!Application::GetInstance()->IsDebugging())
- jsonString = cJSON_Print(json);
- else
- jsonString = cJSON_PrintUnformatted(json);
-
- cJSON_Delete(json);
-
- result = jsonString;
-
- free(jsonString);
-
- return result;
-}
-
/**
* Retrieves the underlying dictionary for this message.
*
{
public:
MessagePart(void);
- MessagePart(string json);
MessagePart(const Dictionary::Ptr& dictionary);
MessagePart(const MessagePart& message);
- string ToJsonString(void) const;
-
Dictionary::Ptr GetDictionary(void) const;
/**
private:
Dictionary::Ptr m_Dictionary;
-
- static Dictionary::Ptr GetDictionaryFromJson(json_t *json);
- static json_t *GetJsonFromDictionary(const Dictionary::Ptr& dictionary);
};
}