namespace ctx {
- class EXPORT_API Json {
+ class EXPORT_API CtxJson {
public:
- Json();
- Json(const char *s);
- Json(const std::string &s);
-
- /* This Json(const Json &j) only copies the reference to the underlying Json node.
- * Therefore, changes applied to a Json object affect the other.
- * If you need to create a 'real' copy of a Json, which can be manipulated separately,
- * utilize the str() function, e.g., ctx::Json copy(original.str());
+ CtxJson();
+ CtxJson(const char *s);
+ CtxJson(const std::string &s);
+
+ /* This CtxJson(const CtxJson &j) only copies the reference to the underlying CtxJson node.
+ * Therefore, changes applied to a CtxJson object affect the other.
+ * If you need to create a 'real' copy of a CtxJson, which can be manipulated separately,
+ * utilize the str() function, e.g., ctx::CtxJson copy(original.str());
*/
- Json(const Json &j);
+ CtxJson(const CtxJson &j);
- ~Json();
+ ~CtxJson();
- Json& operator=(const char *s);
- Json& operator=(const std::string &s);
+ CtxJson& operator=(const char *s);
+ CtxJson& operator=(const std::string &s);
- /* This operator=(const Json &j) only copies the reference to the underlying Json node.
- * Therefore, changes applied to a Json object affect the other.
- * If you need to create a 'real' copy of a Json, which can be manipulated separately,
- * utilize the str() function, e.g., ctx::Json copy = original.str();
+ /* This operator=(const CtxJson &j) only copies the reference to the underlying CtxJson node.
+ * Therefore, changes applied to a CtxJson object affect the other.
+ * If you need to create a 'real' copy of a CtxJson, which can be manipulated separately,
+ * utilize the str() function, e.g., ctx::CtxJson copy = original.str();
*/
- Json& operator=(const Json &j);
+ CtxJson& operator=(const CtxJson &j);
- bool operator==(const Json &rhs);
- bool operator!=(const Json &rhs);
+ bool operator==(const CtxJson &rhs);
+ bool operator!=(const CtxJson &rhs);
std::string str();
bool getKeys(std::list<std::string> *list);
bool valid();
- bool set(const char *path, const char *key, Json &val);
+ bool set(const char *path, const char *key, CtxJson &val);
bool set(const char *path, const char *key, int val);
bool set(const char *path, const char *key, int64_t val);
bool set(const char *path, const char *key, double val);
bool set(const char *path, const char *key, std::string val);
bool set(const char *path, const char *key, GVariant *val);
- bool get(const char *path, const char *key, Json *val);
+ bool get(const char *path, const char *key, CtxJson *val);
bool get(const char *path, const char *key, int *val);
bool get(const char *path, const char *key, int64_t *val);
bool get(const char *path, const char *key, double *val);
/* Array operations */
int getSize(const char *path, const char *key);
- bool append(const char *path, const char *key, Json &val);
+ bool append(const char *path, const char *key, CtxJson &val);
bool append(const char *path, const char *key, int val);
bool append(const char *path, const char *key, int64_t val);
bool append(const char *path, const char *key, double val);
bool append(const char *path, const char *key, std::string val);
- bool setAt(const char *path, const char *key, int index, Json &val);
+ bool setAt(const char *path, const char *key, int index, CtxJson &val);
bool setAt(const char *path, const char *key, int index, int val);
bool setAt(const char *path, const char *key, int index, int64_t val);
bool setAt(const char *path, const char *key, int index, double val);
bool setAt(const char *path, const char *key, int index, std::string val);
- bool getAt(const char *path, const char *key, int index, Json *val);
+ bool getAt(const char *path, const char *key, int index, CtxJson *val);
bool getAt(const char *path, const char *key, int index, int *val);
bool getAt(const char *path, const char *key, int index, int64_t *val);
bool getAt(const char *path, const char *key, int index, double *val);
#include <locale>
#include <iomanip>
#include <json-glib/json-glib.h>
-#include <Json.h>
+#include <CtxJson.h>
#define PATH_DELIM '.'
#define MAX_PRECISION 15
return out;
}
-Json::Json() :
+CtxJson::CtxJson() :
__jsonNode(NULL)
{
JsonObject *obj = json_object_new();
- IF_FAIL_VOID_TAG(obj, _E, "Json object construction failed");
+ IF_FAIL_VOID_TAG(obj, _E, "CtxJson object construction failed");
__jsonNode = json_node_new(JSON_NODE_OBJECT);
if (!__jsonNode) {
json_object_unref(obj);
- _E("Json object construction failed");
+ _E("CtxJson object construction failed");
}
json_node_set_object(__jsonNode, obj);
json_object_unref(obj);
}
-Json::Json(const Json &j)
+CtxJson::CtxJson(const CtxJson &j)
: __jsonNode(NULL)
{
__jsonNode = json_node_copy(j.__jsonNode);
- IF_FAIL_VOID_TAG(__jsonNode, _E, "Json object construction failed");
+ IF_FAIL_VOID_TAG(__jsonNode, _E, "CtxJson object construction failed");
}
-Json::Json(const char *s)
+CtxJson::CtxJson(const char *s)
: __jsonNode(NULL)
{
if (s) {
}
}
-Json::Json(const std::string &s)
+CtxJson::CtxJson(const std::string &s)
: __jsonNode(NULL)
{
if (s.empty()) {
}
}
-Json::~Json()
+CtxJson::~CtxJson()
{
__release();
}
-void Json::__parse(const char *s)
+void CtxJson::__parse(const char *s)
{
gboolean result;
JsonParser *parser = NULL;
g_object_unref(parser);
}
-void Json::__release()
+void CtxJson::__release()
{
if (__jsonNode) {
json_node_free(__jsonNode);
}
}
-bool Json::valid()
+bool CtxJson::valid()
{
return (__jsonNode != NULL);
}
-Json& Json::operator=(const Json &j)
+CtxJson& CtxJson::operator=(const CtxJson &j)
{
__release();
__jsonNode = json_node_copy(j.__jsonNode);
if (!__jsonNode) {
- _E("Json object copy failed");
+ _E("CtxJson object copy failed");
}
return *this;
}
-Json& Json::operator=(const char *s)
+CtxJson& CtxJson::operator=(const char *s)
{
__release();
if (s) {
return *this;
}
-Json& Json::operator=(const std::string &s)
+CtxJson& CtxJson::operator=(const std::string &s)
{
__release();
if (s.empty()) {
return *this;
}
-bool Json::operator==(const Json &rhs)
+bool CtxJson::operator==(const CtxJson &rhs)
{
return __nodeEq(__jsonNode, rhs.__jsonNode);
}
-bool Json::operator!=(const Json &rhs)
+bool CtxJson::operator!=(const CtxJson &rhs)
{
return !operator==(rhs);
}
-char* Json::__strDup()
+char* CtxJson::__strDup()
{
- IF_FAIL_RETURN_TAG(__jsonNode, NULL, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(__jsonNode, NULL, _E, "CtxJson object not initialized");
JsonGenerator *jgen = NULL;
char *output = NULL;
return NULL;
}
-std::string Json::str()
+std::string CtxJson::str()
{
std::string output;
char *_s = __strDup();
return jobj;
}
-bool Json::set(const char *path, const char *key, Json &val)
+bool CtxJson::set(const char *path, const char *key, CtxJson &val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val.__jsonNode, false, _E, "Invalid parameter");
JsonObject *jobj = __traverse(__jsonNode, path, true);
json_object_set_member(jobj, key, val.__jsonNode);
val.__jsonNode = NULL;
- val = Json();
+ val = CtxJson();
return true;
}
-bool Json::set(const char *path, const char *key, int val)
+bool CtxJson::set(const char *path, const char *key, int val)
{
return set(path, key, static_cast<int64_t>(val));
}
-bool Json::set(const char *path, const char *key, int64_t val)
+bool CtxJson::set(const char *path, const char *key, int64_t val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key, false, _E, "Invalid parameter");
JsonObject *jobj = __traverse(__jsonNode, path, true);
return true;
}
-bool Json::set(const char *path, const char *key, double val)
+bool CtxJson::set(const char *path, const char *key, double val)
{
return set(path, key, __double_to_string(val));
}
-bool Json::set(const char *path, const char *key, std::string val)
+bool CtxJson::set(const char *path, const char *key, std::string val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key, false, _E, "Invalid parameter");
JsonObject *jobj = __traverse(__jsonNode, path, true);
return true;
}
-bool Json::set(const char *path, const char *key, GVariant *val)
+bool CtxJson::set(const char *path, const char *key, GVariant *val)
{
#if JSON_CHECK_VERSION(0, 14, 0)
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val, false, _E, "Invalid parameter");
const gchar *typeStr = g_variant_get_type_string(val);
json_node_t *node = json_gvariant_serialize(val);
IF_FAIL_RETURN_TAG(node, false, _E, "GVariant manipulation failed");
- Json gvarJson;
+ CtxJson gvarJson;
gvarJson.set(NULL, GVAR_TYPES, std::string(typeStr));
json_object_set_member(json_node_get_object(gvarJson.__jsonNode), GVAR_VALUES, node);
#endif
}
-bool Json::get(const char *path, const char *key, Json *val)
+bool CtxJson::get(const char *path, const char *key, CtxJson *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val, false, _E, "Invalid parameter");
JsonObject *jobj = NULL;
return node;
}
-bool Json::get(const char *path, const char *key, int *val)
+bool CtxJson::get(const char *path, const char *key, int *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val, false, _E, "Invalid parameter");
int64_t v;
return false;
}
-bool Json::get(const char *path, const char *key, int64_t *val)
+bool CtxJson::get(const char *path, const char *key, int64_t *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val, false, _E, "Invalid parameter");
JsonNode *node = __get_value_node(__jsonNode, path, key);
return true;
}
-bool Json::get(const char *path, const char *key, double *val)
+bool CtxJson::get(const char *path, const char *key, double *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val, false, _E, "Invalid parameter");
JsonNode *node = __get_value_node(__jsonNode, path, key);
return true;
}
-bool Json::get(const char *path, const char *key, std::string *val)
+bool CtxJson::get(const char *path, const char *key, std::string *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val, false, _E, "Invalid parameter");
JsonNode *node = __get_value_node(__jsonNode, path, key);
return true;
}
-bool Json::get(const char *path, const char *key, GVariant **val)
+bool CtxJson::get(const char *path, const char *key, GVariant **val)
{
#if JSON_CHECK_VERSION(0, 14, 0)
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val, false, _E, "Invalid parameter");
bool ret;
- Json gvarJson;
+ CtxJson gvarJson;
ret = get(path, key, &gvarJson);
IF_FAIL_RETURN(ret, false);
ret = gvarJson.get(NULL, GVAR_TYPES, &gvarTypes);
IF_FAIL_RETURN(ret, false);
- Json gvarValues;
+ CtxJson gvarValues;
ret = gvarJson.get(NULL, GVAR_VALUES, &gvarValues);
IF_FAIL_RETURN(ret, false);
#endif
}
-bool Json::remove(const char *path, const char *key)
+bool CtxJson::remove(const char *path, const char *key)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key, false, _E, "Invalid parameter");
JsonObject *jobj = __traverse(__jsonNode, path, true);
return json_node_get_array(node);
}
-int Json::getSize(const char *path, const char *key)
+int CtxJson::getSize(const char *path, const char *key)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, -1, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, -1, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key, -1, _E, "Invalid parameter");
JsonArray *jarr = __get_array(__jsonNode, path, key, false);
return json_array_get_length(jarr);
}
-bool Json::append(const char *path, const char *key, Json &val)
+bool CtxJson::append(const char *path, const char *key, CtxJson &val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val.__jsonNode, false, _E, "Invalid parameter");
JsonArray *arr = __get_array(__jsonNode, path, key, true);
json_array_add_element(arr, val.__jsonNode);
val.__jsonNode = NULL;
- val = Json();
+ val = CtxJson();
return true;
}
-bool Json::append(const char *path, const char *key, int val)
+bool CtxJson::append(const char *path, const char *key, int val)
{
return append(path, key, static_cast<int64_t>(val));
}
-bool Json::append(const char *path, const char *key, int64_t val)
+bool CtxJson::append(const char *path, const char *key, int64_t val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key, false, _E, "Invalid parameter");
JsonArray *arr = __get_array(__jsonNode, path, key, true);
return true;
}
-bool Json::append(const char *path, const char *key, double val)
+bool CtxJson::append(const char *path, const char *key, double val)
{
return append(path, key, __double_to_string(val));
}
-bool Json::append(const char *path, const char *key, std::string val)
+bool CtxJson::append(const char *path, const char *key, std::string val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key, false, _E, "Invalid parameter");
JsonArray *arr = __get_array(__jsonNode, path, key, true);
return node;
}
-bool Json::setAt(const char *path, const char *key, int index, Json &val)
+bool CtxJson::setAt(const char *path, const char *key, int index, CtxJson &val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(val.__jsonNode && key && index >= 0, false, _E, "Invalid parameter");
JsonNode *node = __get_array_elem(__jsonNode, path, key, index);
json_node_set_object(node, obj);
json_node_free(val.__jsonNode);
val.__jsonNode = NULL;
- val = Json();
+ val = CtxJson();
return true;
}
-bool Json::setAt(const char *path, const char *key, int index, int val)
+bool CtxJson::setAt(const char *path, const char *key, int index, int val)
{
return setAt(path, key, index, static_cast<int64_t>(val));
}
-bool Json::setAt(const char *path, const char *key, int index, int64_t val)
+bool CtxJson::setAt(const char *path, const char *key, int index, int64_t val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && index >= 0, false, _E, "Invalid parameter");
JsonNode *node = __get_array_elem(__jsonNode, path, key, index);
return true;
}
-bool Json::setAt(const char *path, const char *key, int index, double val)
+bool CtxJson::setAt(const char *path, const char *key, int index, double val)
{
return setAt(path, key, index, __double_to_string(val));
}
-bool Json::setAt(const char *path, const char *key, int index, std::string val)
+bool CtxJson::setAt(const char *path, const char *key, int index, std::string val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && index >= 0, false, _E, "Invalid parameter");
JsonNode *node = __get_array_elem(__jsonNode, path, key, index);
return true;
}
-bool Json::getAt(const char *path, const char *key, int index, Json *val)
+bool CtxJson::getAt(const char *path, const char *key, int index, CtxJson *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val && index >= 0, false, _E, "Invalid parameter");
JsonNode *node = __get_array_elem(__jsonNode, path, key, index);
return true;
}
-bool Json::getAt(const char *path, const char *key, int index, int *val)
+bool CtxJson::getAt(const char *path, const char *key, int index, int *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val && index >= 0, false, _E, "Invalid parameter");
int64_t v;
return false;
}
-bool Json::getAt(const char *path, const char *key, int index, int64_t *val)
+bool CtxJson::getAt(const char *path, const char *key, int index, int64_t *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val && index >= 0, false, _E, "Invalid parameter");
JsonNode *node = __get_array_elem(__jsonNode, path, key, index);
return true;
}
-bool Json::getAt(const char *path, const char *key, int index, double *val)
+bool CtxJson::getAt(const char *path, const char *key, int index, double *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val && index >= 0, false, _E, "Invalid parameter");
JsonNode *node = __get_array_elem(__jsonNode, path, key, index);
return true;
}
-bool Json::getAt(const char *path, const char *key, int index, std::string *val)
+bool CtxJson::getAt(const char *path, const char *key, int index, std::string *val)
{
- IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "Json object not initialized");
+ IF_FAIL_RETURN_TAG(this->__jsonNode, false, _E, "CtxJson object not initialized");
IF_FAIL_RETURN_TAG(key && val && index >= 0, false, _E, "Invalid parameter");
JsonNode *node = __get_array_elem(__jsonNode, path, key, index);
return true;
}
-bool Json::__getMembers(json_node_t *node, std::list<std::string> &list)
+bool CtxJson::__getMembers(json_node_t *node, std::list<std::string> &list)
{
IF_FAIL_RETURN(node, false);
list.clear();
JsonObject *jobj = json_node_get_object(node);
- IF_FAIL_RETURN_TAG(jobj, false, _E, "Getting Json object failed");
+ IF_FAIL_RETURN_TAG(jobj, false, _E, "Getting CtxJson object failed");
GList *members = json_object_get_members(jobj);
IF_FAIL_RETURN(members, true);
return true;
}
-bool Json::getKeys(std::list<std::string>* list)
+bool CtxJson::getKeys(std::list<std::string>* list)
{
IF_FAIL_RETURN_TAG(list, false, _E, "Invalid parameter");
return __getMembers(__jsonNode, *list);
}
-bool Json::__nodeEq(json_node_t *lhs, json_node_t *rhs)
+bool CtxJson::__nodeEq(json_node_t *lhs, json_node_t *rhs)
{
IF_FAIL_RETURN(lhs && rhs, false);
return true;
}
-bool Json::__valueEq(json_node_t *lhs, json_node_t *rhs)
+bool CtxJson::__valueEq(json_node_t *lhs, json_node_t *rhs)
{
GType ltype = json_node_get_value_type(lhs);
GType rtype = json_node_get_value_type(rhs);
}
}
-bool Json::__objectEq(json_node_t *lhs, json_node_t *rhs)
+bool CtxJson::__objectEq(json_node_t *lhs, json_node_t *rhs)
{
std::list<std::string> lm, rm;
IF_FAIL_RETURN(__getMembers(lhs, lm), false);
return true;
}
-bool Json::__arrayEq(json_node_t *lhs, json_node_t *rhs)
+bool CtxJson::__arrayEq(json_node_t *lhs, json_node_t *rhs)
{
JsonArray *larr = json_node_get_array(lhs);
JsonArray *rarr = json_node_get_array(rhs);