1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib-object.h>
#include <json-glib/json-glib.h>
#include <json-glib/json-gobject.h>
#define TEST_TYPE_BOXED (test_boxed_get_type ())
#define TEST_TYPE_OBJECT (test_object_get_type ())
#define TEST_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_OBJECT, TestObject))
#define TEST_IS_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_OBJECT))
#define TEST_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass))
#define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT))
#define TEST_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass))
typedef struct _TestBoxed TestBoxed;
typedef struct _TestObject TestObject;
typedef struct _TestObjectClass TestObjectClass;
struct _TestBoxed
{
gint foo;
gboolean bar;
};
struct _TestObject
{
GObject parent_instance;
TestBoxed blah;
};
struct _TestObjectClass
{
GObjectClass parent_class;
};
GType test_object_get_type (void);
/*** implementation ***/
static gpointer
test_boxed_copy (gpointer src)
{
return g_slice_dup (TestBoxed, src);
}
static void
test_boxed_free (gpointer boxed)
{
if (G_LIKELY (boxed != NULL))
g_slice_free (TestBoxed, boxed);
}
static JsonNode *
test_boxed_serialize (gconstpointer boxed)
{
const TestBoxed *test = boxed;
JsonObject *object;
JsonNode *node;
if (boxed == NULL)
return json_node_new (JSON_NODE_NULL);
object = json_object_new ();
node = json_node_new (JSON_NODE_OBJECT);
json_object_set_int_member (object, "foo", test->foo);
json_object_set_boolean_member (object, "bar", test->bar);
json_node_take_object (node, object);
if (g_test_verbose ())
{
g_print ("Serialize: { foo: %" G_GINT64_FORMAT ", bar: %s }\n",
json_object_get_int_member (object, "foo"),
json_object_get_boolean_member (object, "bar") ? "true" : "false");
}
return node;
}
static gpointer
test_boxed_deserialize (JsonNode *node)
{
JsonObject *object;
TestBoxed *test;
if (json_node_get_node_type (node) != JSON_NODE_OBJECT)
return NULL;
object = json_node_get_object (node);
test = g_slice_new (TestBoxed);
test->foo = json_object_get_int_member (object, "foo");
test->bar = json_object_get_boolean_member (object, "bar");
if (g_test_verbose ())
{
g_print ("Deserialize: { foo: %d, bar: %s }\n",
test->foo,
test->bar ? "true" : "false");
}
return test;
}
GType
test_boxed_get_type (void)
{
static GType b_type = 0;
if (G_UNLIKELY (b_type == 0))
{
b_type = g_boxed_type_register_static ("TestBoxed",
test_boxed_copy,
test_boxed_free);
if (g_test_verbose ())
g_print ("Registering transform functions\n");
json_boxed_register_serialize_func (b_type, JSON_NODE_OBJECT,
test_boxed_serialize);
json_boxed_register_deserialize_func (b_type, JSON_NODE_OBJECT,
test_boxed_deserialize);
}
return b_type;
}
enum
{
PROP_0,
PROP_BLAH
};
G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
static void
test_object_finalize (GObject *gobject)
{
G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
}
static void
test_object_set_property (GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
case PROP_BLAH:
{
const TestBoxed *blah = g_value_get_boxed (value);
TEST_OBJECT (gobject)->blah = *blah;
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
}
static void
test_object_get_property (GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
case PROP_BLAH:
g_value_set_boxed (value, &(TEST_OBJECT (gobject)->blah));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
}
static void
test_object_class_init (TestObjectClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->set_property = test_object_set_property;
gobject_class->get_property = test_object_get_property;
gobject_class->finalize = test_object_finalize;
g_object_class_install_property (gobject_class,
PROP_BLAH,
g_param_spec_boxed ("blah", "Blah", "Blah",
TEST_TYPE_BOXED,
G_PARAM_READWRITE));
}
static void
test_object_init (TestObject *object)
{
object->blah.foo = 0;
object->blah.bar = FALSE;
}
static const gchar *serialize_data =
"{\n"
" \"blah\" : {\n"
" \"foo\" : 42,\n"
" \"bar\" : true\n"
" }\n"
"}";
static void
test_serialize_boxed (void)
{
TestBoxed boxed = { 42, TRUE };
GObject *obj;
gchar *data;
gsize len;
obj = g_object_new (TEST_TYPE_OBJECT, "blah", &boxed, NULL);
data = json_gobject_to_data (obj, &len);
g_assert_cmpint (len, ==, strlen (serialize_data));
g_assert_cmpstr (data, ==, serialize_data);
if (g_test_verbose ())
g_print ("TestObject:\n%s\n", data);
g_free (data);
g_object_unref (obj);
}
static void
test_deserialize_boxed (void)
{
GObject *obj;
obj = json_gobject_from_data (TEST_TYPE_OBJECT, serialize_data, -1, NULL);
g_assert (TEST_IS_OBJECT (obj));
g_assert_cmpint (TEST_OBJECT (obj)->blah.foo, ==, 42);
g_assert (TEST_OBJECT (obj)->blah.bar);
g_object_unref (obj);
}
int
main (int argc,
char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/boxed/serialize-property", test_serialize_boxed);
g_test_add_func ("/boxed/deserialize-property", test_deserialize_boxed);
return g_test_run ();
}
|