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