1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-06-20 19:01:56 +00:00
libsearpc/lib/searpc-utils.h

75 lines
2.1 KiB
C
Raw Normal View History

2013-08-08 08:24:20 +00:00
#include <glib.h>
#include <glib-object.h>
#include <jansson.h>
2011-04-08 12:58:15 +00:00
2013-08-09 04:56:37 +00:00
#define SEARPC_JSON_DOMAIN g_quark_from_string("SEARPC_JSON")
2011-04-08 12:58:15 +00:00
2013-08-08 08:24:20 +00:00
typedef enum {
2013-08-09 04:56:37 +00:00
SEARPC_JSON_ERROR_LOAD,
SEARPC_JSON_ERROR_PACK,
SEARPC_JSON_ERROR_UPACK
} SEARPCJSONERROR;
2013-08-08 08:24:20 +00:00
json_t *json_gobject_serialize (GObject *);
GObject *json_gobject_deserialize (GType , json_t *);
2013-11-05 08:45:10 +00:00
inline static void setjetoge(const json_error_t *jerror, GError **error)
2011-04-08 12:58:15 +00:00
{
2013-08-09 04:56:37 +00:00
/* Load is the only function I use which reports errors */
2013-11-05 08:45:10 +00:00
g_set_error(error, SEARPC_JSON_DOMAIN, SEARPC_JSON_ERROR_LOAD, "%s", jerror->text);
2011-04-08 12:58:15 +00:00
}
2013-08-08 08:24:20 +00:00
inline static const char *json_object_get_string_or_null_member (json_t *object,const char *member_name)
2011-04-08 12:58:15 +00:00
{
2013-08-09 04:56:37 +00:00
json_t *ret = json_object_get (object, member_name);
if (ret)
return json_string_value(ret);
else
return NULL;
2011-04-08 12:58:15 +00:00
}
2013-08-08 08:24:20 +00:00
inline static void json_object_set_string_or_null_member (json_t *object,const char *member_name,const char *value)
2011-04-08 12:58:15 +00:00
{
2013-08-09 04:56:37 +00:00
if (value)
json_object_set_new(object,member_name, json_string(value));
else
json_object_set_new(object,member_name, json_null());
2011-04-08 12:58:15 +00:00
}
2013-08-08 08:24:20 +00:00
inline static const char *json_array_get_string_or_null_element (json_t *array, size_t index)
2011-04-08 12:58:15 +00:00
{
2013-08-09 04:56:37 +00:00
json_t *ret=json_array_get (array,index);
if (ret)
return json_string_value (ret);
else
return NULL;
2011-04-08 12:58:15 +00:00
}
2013-08-08 08:24:20 +00:00
inline static void json_array_add_string_or_null_element (json_t *array, const char *value)
{
2013-08-09 04:56:37 +00:00
if (value)
json_array_append_new (array, json_string (value));
else
json_array_append_new (array, json_null ());
2013-08-08 08:24:20 +00:00
}
2011-04-08 12:58:15 +00:00
2013-08-08 08:24:20 +00:00
inline static json_int_t json_array_get_int_element (json_t *array, size_t index)
{
2013-08-09 04:56:37 +00:00
return json_integer_value (json_array_get (array, index));
2013-08-08 08:24:20 +00:00
}
2016-06-28 04:00:44 +00:00
inline static const json_t *json_array_get_json_or_null_element (json_t *array, size_t index)
{
return json_array_get (array, index);
}
inline static void json_array_add_json_or_null_element (json_t *array, const json_t *value)
{
if (value) {
json_t *obj = json_deep_copy(value);
json_array_append_new (array, obj);
} else {
json_array_append_new (array, json_null ());
}
}