5 #include <glib-object.h>
7 #include <json-glib/json-glib.h>
8 #include <json-glib/json-gobject.h>
10 #define TEST_TYPE_BOXED (test_boxed_get_type ())
11 #define TEST_TYPE_OBJECT (test_object_get_type ())
12 #define TEST_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_OBJECT, TestObject))
13 #define TEST_IS_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_OBJECT))
14 #define TEST_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass))
15 #define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT))
16 #define TEST_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass))
18 typedef struct _TestBoxed TestBoxed;
19 typedef struct _TestObject TestObject;
20 typedef struct _TestObjectClass TestObjectClass;
30 GObject parent_instance;
39 struct _TestObjectClass
41 GObjectClass parent_class;
44 GType test_object_get_type (void);
46 /*** implementation ***/
49 test_boxed_copy (const TestBoxed *src)
51 TestBoxed *copy = g_slice_new (TestBoxed);
59 test_boxed_free (TestBoxed *boxed)
63 g_slice_free (TestBoxed, boxed);
68 test_boxed_get_type (void)
70 static GType b_type = 0;
72 if (G_UNLIKELY (b_type == 0))
73 b_type = g_boxed_type_register_static ("TestBoxed",
74 (GBoxedCopyFunc) test_boxed_copy,
75 (GBoxedFreeFunc) test_boxed_free);
91 static JsonSerializableIface *serializable_iface = NULL;
93 static void json_serializable_iface_init (gpointer g_iface);
95 G_DEFINE_TYPE_WITH_CODE (TestObject, test_object, G_TYPE_OBJECT,
96 G_IMPLEMENT_INTERFACE (JSON_TYPE_SERIALIZABLE,
97 json_serializable_iface_init));
100 test_object_serialize_property (JsonSerializable *serializable,
105 JsonNode *retval = NULL;
107 if (strcmp (name, "blah") == 0)
113 retval = json_node_new (JSON_NODE_OBJECT);
114 obj = json_object_new ();
116 boxed = g_value_get_boxed (value);
118 val = json_node_new (JSON_NODE_VALUE);
119 json_node_set_int (val, boxed->foo);
120 json_object_set_member (obj, "foo", val);
122 val = json_node_new (JSON_NODE_VALUE);
123 json_node_set_boolean (val, boxed->bar);
124 json_object_set_member (obj, "bar", val);
126 json_node_take_object (retval, obj);
129 retval = serializable_iface->serialize_property (serializable,
137 json_serializable_iface_init (gpointer g_iface)
139 JsonSerializableIface *iface = g_iface;
141 serializable_iface = g_type_default_interface_peek (JSON_TYPE_SERIALIZABLE);
143 iface->serialize_property = test_object_serialize_property;
147 test_object_finalize (GObject *gobject)
149 g_free (TEST_OBJECT (gobject)->baz);
151 G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
155 test_object_set_property (GObject *gobject,
163 TEST_OBJECT (gobject)->foo = g_value_get_int (value);
166 TEST_OBJECT (gobject)->bar = g_value_get_boolean (value);
169 g_free (TEST_OBJECT (gobject)->baz);
170 TEST_OBJECT (gobject)->baz = g_value_dup_string (value);
173 TEST_OBJECT (gobject)->meh = g_value_get_double (value);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
181 test_object_get_property (GObject *gobject,
189 g_value_set_int (value, TEST_OBJECT (gobject)->foo);
192 g_value_set_boolean (value, TEST_OBJECT (gobject)->bar);
195 g_value_set_string (value, TEST_OBJECT (gobject)->baz);
198 g_value_set_boxed (value, &(TEST_OBJECT (gobject)->blah));
201 g_value_set_double (value, TEST_OBJECT (gobject)->meh);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
209 test_object_class_init (TestObjectClass *klass)
211 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
213 gobject_class->set_property = test_object_set_property;
214 gobject_class->get_property = test_object_get_property;
215 gobject_class->finalize = test_object_finalize;
217 g_object_class_install_property (gobject_class,
219 g_param_spec_int ("foo", "Foo", "Foo",
222 g_object_class_install_property (gobject_class,
224 g_param_spec_boolean ("bar", "Bar", "Bar",
227 g_object_class_install_property (gobject_class,
229 g_param_spec_string ("baz", "Baz", "Baz",
232 g_object_class_install_property (gobject_class,
234 g_param_spec_boxed ("blah", "Blah", "Blah",
237 g_object_class_install_property (gobject_class,
239 g_param_spec_double ("meh", "Meh", "Meh",
246 test_object_init (TestObject *object)
250 object->baz = g_strdup ("Test");
253 object->blah.foo = object->foo;
254 object->blah.bar = object->bar;
258 test_serialize (void)
260 TestObject *obj = g_object_new (TEST_TYPE_OBJECT,
263 "baz", "Hello, World!",
266 JsonParser *parser = json_parser_new ();
267 GError *error = NULL;
273 data = json_gobject_to_data (G_OBJECT (obj), &len);
275 g_assert_cmpint (len, >, 0);
276 if (g_test_verbose ())
277 g_print ("TestObject:\n%s\n", data);
279 parser = json_parser_new ();
280 json_parser_load_from_data (parser, data, -1, &error);
281 g_assert (error == NULL);
283 node = json_parser_get_root (parser);
284 g_assert (json_node_get_node_type (node) == JSON_NODE_OBJECT);
286 object = json_node_get_object (node);
287 g_assert_cmpint (json_object_get_int_member (object, "foo"), ==, 47);
288 g_assert (!json_object_get_boolean_member (object, "bar"));
289 g_assert_cmpstr (json_object_get_string_member (object, "baz"), ==, "Hello, World!");
290 g_assert_cmpfloat (json_object_get_double_member (object, "meh"), ==, 0.5);
292 /* blah is read-only */
293 g_assert (json_object_has_member (object, "blah"));
296 g_object_unref (parser);
297 g_object_unref (obj);
305 g_test_init (&argc, &argv, NULL);
307 g_test_add_func ("/serialize/gobject-boxed", test_serialize);
309 return g_test_run ();