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
|
/* json-value.c - JSON value container
*
* This file is part of JSON-GLib
* Copyright (C) 2012 Emmanuele Bassi <ebassi@gnome.org>
*
* 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.1 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Author:
* Emmanuele Bassi <ebassi@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include "json-types-private.h"
const gchar *
json_value_type_get_name (JsonValueType value_type)
{
switch (value_type)
{
case JSON_VALUE_INVALID:
return "Unset";
case JSON_VALUE_INT:
return "Integer";
case JSON_VALUE_DOUBLE:
return "Floating Point";
case JSON_VALUE_BOOLEAN:
return "Boolean";
case JSON_VALUE_STRING:
return "String";
case JSON_VALUE_NULL:
return "Null";
}
return "Undefined";
}
GType
json_value_type (const JsonValue *value)
{
switch (value->type)
{
case JSON_VALUE_INVALID:
return G_TYPE_INVALID;
case JSON_VALUE_INT:
return G_TYPE_INT64;
case JSON_VALUE_DOUBLE:
return G_TYPE_DOUBLE;
case JSON_VALUE_BOOLEAN:
return G_TYPE_BOOLEAN;
case JSON_VALUE_STRING:
return G_TYPE_STRING;
case JSON_VALUE_NULL:
return G_TYPE_INVALID;
}
return G_TYPE_INVALID;
}
JsonValue *
json_value_alloc (void)
{
JsonValue *res = g_slice_new0 (JsonValue);
res->ref_count = 1;
return res;
}
JsonValue *
json_value_init (JsonValue *value,
JsonValueType value_type)
{
g_return_val_if_fail (value != NULL, NULL);
if (value->type != JSON_VALUE_INVALID)
json_value_unset (value);
value->type = value_type;
return value;
}
JsonValue *
json_value_ref (JsonValue *value)
{
g_return_val_if_fail (value != NULL, NULL);
g_atomic_int_add (&value->ref_count, 1);
return value;
}
void
json_value_unref (JsonValue *value)
{
g_return_if_fail (value != NULL);
if (g_atomic_int_dec_and_test (&value->ref_count))
json_value_free (value);
}
void
json_value_unset (JsonValue *value)
{
g_return_if_fail (value != NULL);
switch (value->type)
{
case JSON_VALUE_INVALID:
break;
case JSON_VALUE_INT:
value->data.v_int = 0;
break;
case JSON_VALUE_DOUBLE:
value->data.v_double = 0.0;
break;
case JSON_VALUE_BOOLEAN:
value->data.v_bool = FALSE;
break;
case JSON_VALUE_STRING:
g_free (value->data.v_str);
value->data.v_str = NULL;
break;
case JSON_VALUE_NULL:
break;
}
}
void
json_value_free (JsonValue *value)
{
if (G_LIKELY (value != NULL))
{
json_value_unset (value);
g_slice_free (JsonValue, value);
}
}
#define _JSON_VALUE_DEFINE_SET(Type,EType,CType,VField) \
void \
json_value_set_##Type (JsonValue *value, CType VField) \
{ \
g_return_if_fail (JSON_VALUE_IS_VALID (value)); \
g_return_if_fail (JSON_VALUE_HOLDS (value, JSON_VALUE_##EType)); \
\
value->data.VField = VField; \
\
}
#define _JSON_VALUE_DEFINE_GET(Type,EType,CType,VField) \
CType \
json_value_get_##Type (const JsonValue *value) \
{ \
g_return_val_if_fail (JSON_VALUE_IS_VALID (value), 0); \
g_return_val_if_fail (JSON_VALUE_HOLDS (value, JSON_VALUE_##EType), 0); \
\
return value->data.VField; \
}
#define _JSON_VALUE_DEFINE_SET_GET(Type,EType,CType,VField) \
_JSON_VALUE_DEFINE_SET(Type,EType,CType,VField) \
_JSON_VALUE_DEFINE_GET(Type,EType,CType,VField)
_JSON_VALUE_DEFINE_SET_GET(int, INT, gint64, v_int)
_JSON_VALUE_DEFINE_SET_GET(double, DOUBLE, gdouble, v_double)
_JSON_VALUE_DEFINE_SET_GET(boolean, BOOLEAN, gboolean, v_bool)
void
json_value_set_string (JsonValue *value,
const gchar *v_str)
{
g_return_if_fail (JSON_VALUE_IS_VALID (value));
g_return_if_fail (JSON_VALUE_HOLDS_STRING (value));
g_free (value->data.v_str);
value->data.v_str = g_strdup (v_str);
}
_JSON_VALUE_DEFINE_GET(string, STRING, const gchar *, v_str)
#undef _JSON_VALUE_DEFINE_SET_GET
#undef _JSON_VALUE_DEFINE_GET
#undef _JSON_VALUE_DEFINE_SET
|