Coverage Report

Created: 2025-07-18 06:08

/src/tinysparql/subprojects/json-glib-1.10.6/json-glib/json-value.c
Line
Count
Source (jump to first uncovered line)
1
/* json-value.c - JSON value container
2
 * 
3
 * This file is part of JSON-GLib
4
 *
5
 * SPDX-FileCopyrightText: 2012  Emmanuele Bassi
6
 * SPDX-FileCopyrightText: 2015 Collabora Ltd.
7
 * SPDX-License-Identifier: LGPL-2.1-or-later
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * Author:
23
 *   Emmanuele Bassi  <ebassi@linux.intel.com>
24
 *   Philip Withnall  <philip.withnall@collabora.co.uk>
25
 */
26
27
#include "config.h"
28
29
#include <glib.h>
30
31
#include "json-types-private.h"
32
33
const gchar *
34
json_value_type_get_name (JsonValueType value_type)
35
0
{
36
0
  switch (value_type)
37
0
    {
38
0
    case JSON_VALUE_INVALID:
39
0
      return "Unset";
40
41
0
    case JSON_VALUE_INT:
42
0
      return "Integer";
43
44
0
    case JSON_VALUE_DOUBLE:
45
0
      return "Floating Point";
46
47
0
    case JSON_VALUE_BOOLEAN:
48
0
      return "Boolean";
49
50
0
    case JSON_VALUE_STRING:
51
0
      return "String";
52
53
0
    case JSON_VALUE_NULL:
54
0
      return "Null";
55
0
    }
56
57
0
  return "Undefined";
58
0
}
59
60
GType
61
json_value_type (const JsonValue *value)
62
0
{
63
0
  switch (value->type)
64
0
    {
65
0
    case JSON_VALUE_INVALID:
66
0
      return G_TYPE_INVALID;
67
68
0
    case JSON_VALUE_INT:
69
0
      return G_TYPE_INT64;
70
71
0
    case JSON_VALUE_DOUBLE:
72
0
      return G_TYPE_DOUBLE;
73
74
0
    case JSON_VALUE_BOOLEAN:
75
0
      return G_TYPE_BOOLEAN;
76
77
0
    case JSON_VALUE_STRING:
78
0
      return G_TYPE_STRING;
79
80
0
    case JSON_VALUE_NULL:
81
0
      return G_TYPE_INVALID;
82
0
    }
83
84
0
  return G_TYPE_INVALID;
85
0
}
86
87
JsonValue *
88
json_value_alloc (void)
89
0
{
90
0
  JsonValue *res = g_new0 (JsonValue, 1);
91
92
0
  g_ref_count_init (&res->ref_count);
93
94
0
  return res;
95
0
}
96
97
JsonValue *
98
json_value_init (JsonValue     *value,
99
                 JsonValueType  value_type)
100
0
{
101
0
  g_return_val_if_fail (value != NULL, NULL);
102
103
0
  if (value->type != JSON_VALUE_INVALID)
104
0
    json_value_unset (value);
105
106
0
  value->type = value_type;
107
108
0
  return value;
109
0
}
110
111
JsonValue *
112
json_value_ref (JsonValue *value)
113
0
{
114
0
  g_return_val_if_fail (value != NULL, NULL);
115
116
0
  g_ref_count_inc (&value->ref_count);
117
118
0
  return value;
119
0
}
120
121
void
122
json_value_unref (JsonValue *value)
123
0
{
124
0
  g_return_if_fail (value != NULL);
125
126
0
  if (g_ref_count_dec (&value->ref_count))
127
0
    json_value_free (value);
128
0
}
129
130
void
131
json_value_unset (JsonValue *value)
132
0
{
133
0
  g_return_if_fail (value != NULL);
134
135
0
  switch (value->type)
136
0
    {
137
0
    case JSON_VALUE_INVALID:
138
0
      break;
139
140
0
    case JSON_VALUE_INT:
141
0
      value->data.v_int = 0;
142
0
      break;
143
144
0
    case JSON_VALUE_DOUBLE:
145
0
      value->data.v_double = 0.0;
146
0
      break;
147
148
0
    case JSON_VALUE_BOOLEAN:
149
0
      value->data.v_bool = FALSE;
150
0
      break;
151
152
0
    case JSON_VALUE_STRING:
153
0
      g_free (value->data.v_str);
154
0
      value->data.v_str = NULL;
155
0
      break;
156
157
0
    case JSON_VALUE_NULL:
158
0
      break;
159
0
    }
160
0
}
161
162
void
163
json_value_free (JsonValue *value)
164
0
{
165
0
  if (G_LIKELY (value != NULL))
166
0
    {
167
0
      json_value_unset (value);
168
0
      g_free (value);
169
0
    }
170
0
}
171
172
/*< private >
173
 * json_value_seal:
174
 * @value: a JSON scalar value
175
 *
176
 * Seals the value, making it immutable to further changes.
177
 *
178
 * If the value is already immutable, this is a no-op.
179
 */
180
void
181
json_value_seal (JsonValue *value)
182
0
{
183
0
  g_return_if_fail (JSON_VALUE_IS_VALID (value));
184
185
0
  value->immutable = TRUE;
186
0
}
187
188
guint
189
json_value_hash (gconstpointer key)
190
0
{
191
0
  JsonValue *value;
192
0
  guint value_hash;
193
0
  guint type_hash;
194
195
0
  value = (JsonValue *) key;
196
197
  /* Hash the type and value separately.
198
   * Use the top 3 bits to store the type. */
199
0
  type_hash = value->type << (sizeof (guint) * 8 - 3);
200
201
0
  switch (value->type)
202
0
    {
203
0
    case JSON_VALUE_NULL:
204
0
      value_hash = 0;
205
0
      break;
206
0
    case JSON_VALUE_BOOLEAN:
207
0
      value_hash = json_value_get_boolean (value) ? 1 : 0;
208
0
      break;
209
0
    case JSON_VALUE_STRING:
210
0
      value_hash = json_string_hash (json_value_get_string (value));
211
0
      break;
212
0
    case JSON_VALUE_INT: {
213
0
      gint64 v = json_value_get_int (value);
214
0
      value_hash = g_int64_hash (&v);
215
0
      break;
216
0
    }
217
0
    case JSON_VALUE_DOUBLE: {
218
0
      gdouble v = json_value_get_double (value);
219
0
      value_hash = g_double_hash (&v);
220
0
      break;
221
0
    }
222
0
    case JSON_VALUE_INVALID:
223
0
    default:
224
0
      g_assert_not_reached ();
225
0
    }
226
227
  /* Mask out the top 3 bits of the @value_hash. */
228
0
  value_hash &= ~(7u << (sizeof (guint) * 8 - 3));
229
230
0
  return (type_hash | value_hash);
231
0
}
232
233
#define _JSON_VALUE_DEFINE_SET(Type,EType,CType,VField) \
234
void \
235
0
json_value_set_##Type (JsonValue *value, CType VField) \
236
0
{ \
237
0
  g_return_if_fail (JSON_VALUE_IS_VALID (value)); \
238
0
  g_return_if_fail (JSON_VALUE_HOLDS (value, JSON_VALUE_##EType)); \
239
0
  g_return_if_fail (!value->immutable); \
240
0
\
241
0
  value->data.VField = VField; \
242
0
\
243
0
}
Unexecuted instantiation: json_value_set_int
Unexecuted instantiation: json_value_set_double
Unexecuted instantiation: json_value_set_boolean
244
245
#define _JSON_VALUE_DEFINE_GET(Type,EType,CType,VField) \
246
CType \
247
0
json_value_get_##Type (const JsonValue *value) \
248
0
{ \
249
0
  g_return_val_if_fail (JSON_VALUE_IS_VALID (value), 0); \
250
0
  g_return_val_if_fail (JSON_VALUE_HOLDS (value, JSON_VALUE_##EType), 0); \
251
0
\
252
0
  return value->data.VField; \
253
0
}
Unexecuted instantiation: json_value_get_int
Unexecuted instantiation: json_value_get_double
Unexecuted instantiation: json_value_get_boolean
Unexecuted instantiation: json_value_get_string
254
255
#define _JSON_VALUE_DEFINE_SET_GET(Type,EType,CType,VField) \
256
_JSON_VALUE_DEFINE_SET(Type,EType,CType,VField) \
257
_JSON_VALUE_DEFINE_GET(Type,EType,CType,VField)
258
259
_JSON_VALUE_DEFINE_SET_GET(int, INT, gint64, v_int)
260
261
_JSON_VALUE_DEFINE_SET_GET(double, DOUBLE, gdouble, v_double)
262
263
_JSON_VALUE_DEFINE_SET_GET(boolean, BOOLEAN, gboolean, v_bool)
264
265
void
266
json_value_set_string (JsonValue *value,
267
                       const gchar *v_str)
268
0
{
269
0
  g_return_if_fail (JSON_VALUE_IS_VALID (value));
270
0
  g_return_if_fail (JSON_VALUE_HOLDS_STRING (value));
271
0
  g_return_if_fail (!value->immutable);
272
273
0
  g_free (value->data.v_str);
274
0
  value->data.v_str = g_strdup (v_str);
275
0
}
276
277
_JSON_VALUE_DEFINE_GET(string, STRING, const gchar *, v_str)
278
279
#undef _JSON_VALUE_DEFINE_SET_GET
280
#undef _JSON_VALUE_DEFINE_GET
281
#undef _JSON_VALUE_DEFINE_SET