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;
35 struct _TestObjectClass
37 GObjectClass parent_class;
40 GType test_object_get_type (void);
42 /*** implementation ***/
45 test_boxed_copy (gpointer src)
47 return g_slice_dup (TestBoxed, src);
51 test_boxed_free (gpointer boxed)
53 if (G_LIKELY (boxed != NULL))
54 g_slice_free (TestBoxed, boxed);
58 test_boxed_serialize (gconstpointer boxed)
60 const TestBoxed *test = boxed;
65 return json_node_new (JSON_NODE_NULL);
67 object = json_object_new ();
68 node = json_node_new (JSON_NODE_OBJECT);
70 json_object_set_int_member (object, "foo", test->foo);
71 json_object_set_boolean_member (object, "bar", test->bar);
73 json_node_take_object (node, object);
75 if (g_test_verbose ())
77 g_print ("Serialize: { foo: %" G_GINT64_FORMAT ", bar: %s }\n",
78 json_object_get_int_member (object, "foo"),
79 json_object_get_boolean_member (object, "bar") ? "true" : "false");
86 test_boxed_deserialize (JsonNode *node)
91 if (json_node_get_node_type (node) != JSON_NODE_OBJECT)
94 object = json_node_get_object (node);
96 test = g_slice_new (TestBoxed);
97 test->foo = json_object_get_int_member (object, "foo");
98 test->bar = json_object_get_boolean_member (object, "bar");
100 if (g_test_verbose ())
102 g_print ("Deserialize: { foo: %d, bar: %s }\n",
104 test->bar ? "true" : "false");
111 test_boxed_get_type (void)
113 static GType b_type = 0;
115 if (G_UNLIKELY (b_type == 0))
117 b_type = g_boxed_type_register_static ("TestBoxed",
121 if (g_test_verbose ())
122 g_print ("Registering transform functions\n");
124 json_boxed_register_serialize_func (b_type, JSON_NODE_OBJECT,
125 test_boxed_serialize);
126 json_boxed_register_deserialize_func (b_type, JSON_NODE_OBJECT,
127 test_boxed_deserialize);
140 G_DEFINE_TYPE (TestObject, test_object, G_TYPE_OBJECT);
143 test_object_finalize (GObject *gobject)
145 G_OBJECT_CLASS (test_object_parent_class)->finalize (gobject);
149 test_object_set_property (GObject *gobject,
158 const TestBoxed *blah = g_value_get_boxed (value);
160 TEST_OBJECT (gobject)->blah = *blah;
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
170 test_object_get_property (GObject *gobject,
178 g_value_set_boxed (value, &(TEST_OBJECT (gobject)->blah));
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
187 test_object_class_init (TestObjectClass *klass)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
191 gobject_class->set_property = test_object_set_property;
192 gobject_class->get_property = test_object_get_property;
193 gobject_class->finalize = test_object_finalize;
195 g_object_class_install_property (gobject_class,
197 g_param_spec_boxed ("blah", "Blah", "Blah",
203 test_object_init (TestObject *object)
205 object->blah.foo = 0;
206 object->blah.bar = FALSE;
209 static const gchar *serialize_data =
218 test_serialize_boxed (void)
220 TestBoxed boxed = { 42, TRUE };
225 obj = g_object_new (TEST_TYPE_OBJECT, "blah", &boxed, NULL);
227 data = json_gobject_to_data (obj, &len);
229 g_assert_cmpint (len, ==, strlen (serialize_data));
230 g_assert_cmpstr (data, ==, serialize_data);
232 if (g_test_verbose ())
233 g_print ("TestObject:\n%s\n", data);
236 g_object_unref (obj);
240 test_deserialize_boxed (void)
245 obj = json_gobject_from_data (TEST_TYPE_OBJECT, serialize_data, -1, NULL);
246 g_assert (TEST_IS_OBJECT (obj));
247 g_assert_cmpint (TEST_OBJECT (obj)->blah.foo, ==, 42);
248 g_assert (TEST_OBJECT (obj)->blah.bar);
250 g_object_unref (obj);
258 g_test_init (&argc, &argv, NULL);
260 g_test_add_func ("/boxed/serialize-property", test_serialize_boxed);
261 g_test_add_func ("/boxed/deserialize-property", test_deserialize_boxed);
263 return g_test_run ();