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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#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;
gint foo;
gboolean bar;
gchar *baz;
TestBoxed blah;
};
struct _TestObjectClass
{
GObjectClass parent_class;
};
GType test_object_get_type (void);
/*** implementation ***/
static TestBoxed *
test_boxed_copy (const TestBoxed *src)
{
TestBoxed *copy = g_slice_new (TestBoxed);
*copy = *src;
return copy;
}
static void
test_boxed_free (TestBoxed *boxed)
{
if (G_LIKELY (boxed))
{
g_slice_free (TestBoxed, boxed);
}
}
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",
(GBoxedCopyFunc) test_boxed_copy,
(GBoxedFreeFunc) test_boxed_free);
return b_type;
}
enum
{
PROP_0,
PROP_FOO,
PROP_BAR,
PROP_BAZ,
PROP_BLAH
};
static JsonSerializableIface *serializable_iface = NULL;
static void json_serializable_iface_init (gpointer g_iface);
G_DEFINE_TYPE_WITH_CODE (TestObject, test_object, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (JSON_TYPE_SERIALIZABLE,
json_serializable_iface_init));
static JsonNode *
test_object_serialize_property (JsonSerializable *serializable,
const gchar *name,
const GValue *value,
GParamSpec *pspec)
{
JsonNode *retval = NULL;
if (strcmp (name, "blah") == 0)
{
TestBoxed *boxed;
JsonObject *obj;
JsonNode *val;
retval = json_node_new (JSON_NODE_OBJECT);
obj = json_object_new ();
boxed = g_value_get_boxed (value);
val = json_node_new (JSON_NODE_VALUE);
json_node_set_int (val, boxed->foo);
json_object_set_member (obj, "foo", val);
val = json_node_new (JSON_NODE_VALUE);
json_node_set_boolean (val, boxed->bar);
json_object_set_member (obj, "bar", val);
json_node_take_object (retval, obj);
}
else
retval = serializable_iface->serialize_property (serializable,
name,
value, pspec);
return retval;
}
static void
json_serializable_iface_init (gpointer g_iface)
{
JsonSerializableIface *iface = g_iface;
serializable_iface = g_type_default_interface_peek (JSON_TYPE_SERIALIZABLE);
iface->serialize_property = test_object_serialize_property;
}
static void
test_object_finalize (GObject *gobject)
{
g_free (TEST_OBJECT (gobject)->baz);
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_FOO:
TEST_OBJECT (gobject)->foo = g_value_get_int (value);
break;
case PROP_BAR:
TEST_OBJECT (gobject)->bar = g_value_get_boolean (value);
break;
case PROP_BAZ:
g_free (TEST_OBJECT (gobject)->baz);
TEST_OBJECT (gobject)->baz = g_value_dup_string (value);
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_FOO:
g_value_set_int (value, TEST_OBJECT (gobject)->foo);
break;
case PROP_BAR:
g_value_set_boolean (value, TEST_OBJECT (gobject)->bar);
break;
case PROP_BAZ:
g_value_set_string (value, TEST_OBJECT (gobject)->baz);
break;
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_FOO,
g_param_spec_int ("foo", "Foo", "Foo",
0, G_MAXINT, 42,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_BAR,
g_param_spec_boolean ("bar", "Bar", "Bar",
FALSE,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_BAZ,
g_param_spec_string ("baz", "Baz", "Baz",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_BLAH,
g_param_spec_boxed ("blah", "Blah", "Blah",
TEST_TYPE_BOXED,
G_PARAM_READABLE));
}
static void
test_object_init (TestObject *object)
{
object->foo = 42;
object->bar = TRUE;
object->baz = g_strdup ("Test");
object->blah.foo = object->foo;
object->blah.bar = object->bar;
}
static void
test_serialize (void)
{
TestObject *obj = g_object_new (TEST_TYPE_OBJECT, NULL);
JsonParser *parser = json_parser_new ();
GError *error = NULL;
JsonObject *object;
JsonNode *node;
gchar *data;
gsize len;
data = json_gobject_to_data (G_OBJECT (obj), &len);
g_assert_cmpint (len, >, 0);
if (g_test_verbose ())
g_print ("TestObject:\n%s\n", data);
parser = json_parser_new ();
json_parser_load_from_data (parser, data, -1, &error);
g_assert (error == NULL);
node = json_parser_get_root (parser);
g_assert (json_node_get_node_type (node) == JSON_NODE_OBJECT);
object = json_node_get_object (node);
g_assert_cmpint (json_object_get_int_member (object, "foo"), ==, 42);
g_assert (json_object_get_boolean_member (object, "bar"));
g_assert_cmpstr (json_object_get_string_member (object, "baz"), ==, "Test");
node = json_object_get_member (object, "blah");
g_assert (json_node_get_node_type (node) == JSON_NODE_OBJECT);
object = json_node_get_object (node);
g_assert_cmpint (json_object_get_int_member (object, "foo"), ==, 42);
g_assert (json_object_get_boolean_member (object, "bar"));
g_free (data);
g_object_unref (parser);
g_object_unref (obj);
}
int
main (int argc,
char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/serialize/gobject-boxed", test_serialize);
return g_test_run ();
}
|