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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/* json-object.c - JSON object implementation
*
* This file is part of JSON-GLib
* Copyright (C) 2007 OpenedHand Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Author:
* Emmanuele Bassi <ebassi@openedhand.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include "json-types.h"
/**
* SECTION:json-object
* @short_description: a JSON object representation
*
* #JsonArray is the representation of the object type inside JSON. It contains
* #JsonNode<!-- -->s, which may contain fundamental types, arrays or other
* objects. Each member of an object is accessed using its name. Please note
* that the member names are normalized internally before being used; every
* delimiter matching the %G_STR_DELIMITER macro will be transformed into an
* underscore, so for instance "member-name" and "member_name" are equivalent
* for a #JsonObject.
*
* Since objects can be expensive, they are reference counted. You can control
* the lifetime of a #JsonObject using json_object_ref() and json_object_unref().
*
* To add a member with a given name, use json_object_add_member().
* To extract a member with a given name, use json_object_get_member().
* To retrieve the list of members, use json_object_get_members().
* To retrieve the size of the object (that is, the number of members it has), use
* json_object_get_size().
*/
struct _JsonObject
{
GHashTable *members;
volatile gint ref_count;
};
GType
json_object_get_type (void)
{
static GType object_type = 0;
if (G_UNLIKELY (!object_type))
object_type = g_boxed_type_register_static ("JsonObject",
(GBoxedCopyFunc) json_object_ref,
(GBoxedFreeFunc) json_object_unref);
return object_type;
}
/**
* json_object_new:
*
* Creates a new #JsonObject, an JSON object type representation.
*
* Return value: the newly created #JsonObject
*/
JsonObject *
json_object_new (void)
{
JsonObject *object;
object = g_slice_new (JsonObject);
object->ref_count = 1;
object->members = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free,
(GDestroyNotify) json_node_free);
return object;
}
/**
* json_object_ref:
* @object: a #JsonObject
*
* Increase by one the reference count of a #JsonObject.
*
* Return value: the passed #JsonObject, with the reference count
* increased by one.
*/
JsonObject *
json_object_ref (JsonObject *object)
{
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (object->ref_count > 0, NULL);
g_atomic_int_exchange_and_add (&object->ref_count, 1);
return object;
}
/**
* json_object_unref:
* @object: a #JsonObject
*
* Decreases by one the reference count of a #JsonObject. If the
* reference count reaches zero, the object is destroyed and all
* its allocated resources are freed.
*/
void
json_object_unref (JsonObject *object)
{
gint old_ref;
g_return_if_fail (object != NULL);
g_return_if_fail (object->ref_count > 0);
old_ref = g_atomic_int_get (&object->ref_count);
if (old_ref > 1)
g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1);
else
{
g_hash_table_destroy (object->members);
object->members = NULL;
g_slice_free (JsonObject, object);
}
}
/**
* json_object_add_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @node: the value of the member
*
* Adds a member named @member_name and containing @node into a #JsonObject.
* The object will take ownership of the #JsonNode.
*/
void
json_object_add_member (JsonObject *object,
const gchar *member_name,
JsonNode *node)
{
gchar *name;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
g_return_if_fail (node != NULL);
if (json_object_has_member (object, member_name))
{
g_warning ("JsonObject already has a `%s' member of type `%s'",
member_name,
json_node_type_name (node));
return;
}
name = g_strdelimit (g_strdup (member_name), G_STR_DELIMITERS, '_');
g_hash_table_replace (object->members, name, node);
}
/* FIXME: yuck */
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 14
static void
get_keys (gpointer key,
gpointer value,
gpointer user_data)
{
GList **keys = user_data;
*keys = g_list_prepend (*keys, key);
}
static void
get_values (gpointer key,
gpointer value,
gpointer user_data)
{
GList **values = user_data;
*values = g_list_prepend (*values, value);
}
static GList *
g_hash_table_get_keys (GHashTable *hash_table)
{
GList *retval = NULL;
g_return_val_if_fail (hash_table != NULL, NULL);
g_hash_table_foreach (hash_table, get_keys, &retval);
return retval;
}
static GList *
g_hash_table_get_values (GHashTable *hash_table)
{
GList *retval = NULL;
g_return_val_if_fail (hash_table != NULL, NULL);
g_hash_table_foreach (hash_table, get_values, &retval);
return retval;
}
#endif /* GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 14 */
/**
* json_object_get_members:
* @object: a #JsonObject
*
* Retrieves all the names of the members of a #JsonObject. You can
* obtain the value for each member using json_object_get_member().
*
* Return value: a #GList of member names. The content of the list
* is owned by the #JsonObject and should never be modified or
* freed. When you have finished using the returned list, use
* g_list_free() to free the resources it has allocated.
*/
GList *
json_object_get_members (JsonObject *object)
{
g_return_val_if_fail (object != NULL, NULL);
return g_hash_table_get_keys (object->members);
}
/**
* json_object_get_values:
* @object: a #JsonObject
*
* Retrieves all the values of the members of a #JsonObject.
*
* Return value: a #GList of #JsonNode<!-- -->s. The content of the
* list is owned by the #JsonObject and should never be modified
* or freed. When you have finished using the returned list, use
* g_list_free() to free the resources it has allocated.
*/
GList *
json_object_get_values (JsonObject *object)
{
g_return_val_if_fail (object != NULL, NULL);
return g_hash_table_get_values (object->members);
}
/**
* json_object_dup_member:
* @object: a #JsonObject
* @member_name: the name of the JSON object member to access
*
* Retrieves a copy of the #JsonNode containing the value of @member_name
* inside a #JsonObject
*
* Return value: a copy of the node for the requested object member
* or %NULL. Use json_node_free() when done.
*
* Since: 0.6
*/
JsonNode *
json_object_dup_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *retval;
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (member_name != NULL, NULL);
retval = json_object_get_member (object, member_name);
if (!retval)
return NULL;
return json_node_copy (retval);
}
/**
* json_object_get_member:
* @object: a #JsonObject
* @member_name: the name of the JSON object member to access
*
* Retrieves the #JsonNode containing the value of @member_name inside
* a #JsonObject.
*
* Return value: a pointer to the node for the requested object
* member, or %NULL
*/
JsonNode *
json_object_get_member (JsonObject *object,
const gchar *member_name)
{
gchar *name;
JsonNode *retval;
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (member_name != NULL, NULL);
name = g_strdelimit (g_strdup (member_name), G_STR_DELIMITERS, '_');
retval = g_hash_table_lookup (object->members, name);
g_free (name);
return retval;
}
/**
* json_object_has_member:
* @object: a #JsonObject
* @member_name: the name of a JSON object member
*
* Checks whether @object has a member named @member_name.
*
* Return value: %TRUE if the JSON object has the requested member
*/
gboolean
json_object_has_member (JsonObject *object,
const gchar *member_name)
{
gchar *name;
gboolean retval;
g_return_val_if_fail (object != NULL, FALSE);
g_return_val_if_fail (member_name != NULL, FALSE);
name = g_strdelimit (g_strdup (member_name), G_STR_DELIMITERS, '_');
retval = (g_hash_table_lookup (object->members, name) != NULL);
g_free (name);
return retval;
}
/**
* json_object_get_size:
* @object: a #JsonObject
*
* Retrieves the number of members of a #JsonObject.
*
* Return value: the number of members
*/
guint
json_object_get_size (JsonObject *object)
{
g_return_val_if_fail (object != NULL, 0);
return g_hash_table_size (object->members);
}
/**
* json_object_remove_member:
* @object: a #JsonObject
* @member_name: the name of the member to remove
*
* Removes @member_name from @object, freeing its allocated resources.
*/
void
json_object_remove_member (JsonObject *object,
const gchar *member_name)
{
gchar *name;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
name = g_strdelimit (g_strdup (member_name), G_STR_DELIMITERS, '_');
g_hash_table_remove (object->members, name);
g_free (name);
}
|