1 /* json-types-private.h - JSON data types private header
2  *
3  * This file is part of JSON-GLib
4  * Copyright (C) 2007  OpenedHand Ltd
5  * Copyright (C) 2009  Intel Corp.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  *
20  * Author:
21  *   Emmanuele Bassi  <ebassi@linux.intel.com>
22  */
23 
24 #ifndef __JSON_TYPES_PRIVATE_H__
25 #define __JSON_TYPES_PRIVATE_H__
26 
27 #include "json-types.h"
28 
29 G_BEGIN_DECLS
30 
31 #define JSON_NODE_IS_VALID(n) \
32   ((n) != NULL && \
33    (n)->type >= JSON_NODE_OBJECT && \
34    (n)->type <= JSON_NODE_NULL && \
35    (n)->ref_count >= 1)
36 
37 typedef struct _JsonValue JsonValue;
38 
39 typedef enum {
40   JSON_VALUE_INVALID = 0,
41   JSON_VALUE_INT,
42   JSON_VALUE_DOUBLE,
43   JSON_VALUE_BOOLEAN,
44   JSON_VALUE_STRING,
45   JSON_VALUE_NULL
46 } JsonValueType;
47 
48 struct _JsonNode
49 {
50   /*< private >*/
51   JsonNodeType type;
52 
53   volatile gint ref_count;
54   gboolean immutable : 1;
55   gboolean allocated : 1;
56 
57   union {
58     JsonObject *object;
59     JsonArray *array;
60     JsonValue *value;
61   } data;
62 
63   JsonNode *parent;
64 };
65 
66 #define JSON_VALUE_INIT                 { JSON_VALUE_INVALID, 1, FALSE, { 0 }, NULL }
67 #define JSON_VALUE_INIT_TYPE(t)         { (t), 1, FALSE, { 0 }, NULL }
68 #define JSON_VALUE_IS_VALID(v)          ((v) != NULL && (v)->type != JSON_VALUE_INVALID)
69 #define JSON_VALUE_HOLDS(v,t)           ((v) != NULL && (v)->type == (t))
70 #define JSON_VALUE_HOLDS_INT(v)         (JSON_VALUE_HOLDS((v), JSON_VALUE_INT))
71 #define JSON_VALUE_HOLDS_DOUBLE(v)      (JSON_VALUE_HOLDS((v), JSON_VALUE_DOUBLE))
72 #define JSON_VALUE_HOLDS_BOOLEAN(v)     (JSON_VALUE_HOLDS((v), JSON_VALUE_BOOLEAN))
73 #define JSON_VALUE_HOLDS_STRING(v)      (JSON_VALUE_HOLDS((v), JSON_VALUE_STRING))
74 #define JSON_VALUE_HOLDS_NULL(v)        (JSON_VALUE_HOLDS((v), JSON_VALUE_NULL))
75 #define JSON_VALUE_TYPE(v)              (json_value_type((v)))
76 
77 struct _JsonValue
78 {
79   JsonValueType type;
80 
81   volatile gint ref_count;
82   gboolean immutable : 1;
83 
84   union {
85     gint64 v_int;
86     gdouble v_double;
87     gboolean v_bool;
88     gchar *v_str;
89   } data;
90 };
91 
92 struct _JsonArray
93 {
94   GPtrArray *elements;
95 
96   guint immutable_hash;  /* valid iff immutable */
97   volatile gint ref_count;
98   gboolean immutable : 1;
99 };
100 
101 struct _JsonObject
102 {
103   GHashTable *members;
104 
105   GQueue members_ordered;
106 
107   int age;
108   guint immutable_hash;  /* valid iff immutable */
109   volatile gint ref_count;
110   gboolean immutable : 1;
111 };
112 
113 typedef struct
114 {
115   JsonObject *object;  /* unowned */
116   GHashTableIter members_iter;  /* iterator over @members */
117   gpointer padding[2];  /* for future expansion */
118 } JsonObjectIterReal;
119 
120 G_STATIC_ASSERT (sizeof (JsonObjectIterReal) == sizeof (JsonObjectIter));
121 
122 typedef struct
123 {
124   JsonObject *object; /* unowned */
125   GList *cur_member;
126   GList *next_member;
127   gpointer priv_pointer[3];
128   int age;
129   int priv_int[1];
130   gboolean priv_boolean;
131 } JsonObjectOrderedIterReal;
132 
133 G_STATIC_ASSERT (sizeof (JsonObjectOrderedIterReal) == sizeof (JsonObjectIter));
134 
135 G_GNUC_INTERNAL
136 const gchar *   json_node_type_get_name         (JsonNodeType     node_type);
137 G_GNUC_INTERNAL
138 const gchar *   json_value_type_get_name        (JsonValueType    value_type);
139 
140 G_GNUC_INTERNAL
141 GQueue *        json_object_get_members_internal (JsonObject     *object);
142 
143 G_GNUC_INTERNAL
144 GType           json_value_type                 (const JsonValue *value);
145 
146 G_GNUC_INTERNAL
147 JsonValue *     json_value_alloc                (void);
148 G_GNUC_INTERNAL
149 JsonValue *     json_value_init                 (JsonValue       *value,
150                                                  JsonValueType    value_type);
151 G_GNUC_INTERNAL
152 JsonValue *     json_value_ref                  (JsonValue       *value);
153 G_GNUC_INTERNAL
154 void            json_value_unref                (JsonValue       *value);
155 G_GNUC_INTERNAL
156 void            json_value_unset                (JsonValue       *value);
157 G_GNUC_INTERNAL
158 void            json_value_free                 (JsonValue       *value);
159 G_GNUC_INTERNAL
160 void            json_value_set_int              (JsonValue       *value,
161                                                  gint64           v_int);
162 G_GNUC_INTERNAL
163 gint64          json_value_get_int              (const JsonValue *value);
164 G_GNUC_INTERNAL
165 void            json_value_set_double           (JsonValue       *value,
166                                                  gdouble          v_double);
167 G_GNUC_INTERNAL
168 gdouble         json_value_get_double           (const JsonValue *value);
169 G_GNUC_INTERNAL
170 void            json_value_set_boolean          (JsonValue       *value,
171                                                  gboolean         v_bool);
172 G_GNUC_INTERNAL
173 gboolean        json_value_get_boolean          (const JsonValue *value);
174 G_GNUC_INTERNAL
175 void            json_value_set_string           (JsonValue       *value,
176                                                  const gchar     *v_str);
177 G_GNUC_INTERNAL
178 const gchar *   json_value_get_string           (const JsonValue *value);
179 
180 G_GNUC_INTERNAL
181 void            json_value_seal                 (JsonValue       *value);
182 
183 G_GNUC_INTERNAL
184 guint           json_value_hash                 (gconstpointer    key);
185 
186 G_END_DECLS
187 
188 #endif /* __JSON_TYPES_PRIVATE_H__ */
189