| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2022 Collabora Ltd. |
| 4 |
* @author Julian Bouzas <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "spa-json.h" |
| 10 |
#include "log.h" |
| 11 |
|
| 12 |
#include <spa/utils/defs.h> |
| 13 |
#include <spa/utils/json.h> |
| 14 |
|
| 15 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-spa-json") |
| 16 |
|
| 17 |
#define WP_SPA_JSON_STRING_INIT_SIZE 64 |
| 18 |
#define WP_SPA_JSON_BUILDER_INIT_SIZE 64 |
| 19 |
|
| 20 |
/*! \defgroup wpspajson WpSpaJson */ |
| 21 |
/*! |
| 22 |
* \struct WpSpaJson |
| 23 |
* |
| 24 |
* \since 0.4.8 |
| 25 |
*/ |
| 26 |
/*! |
| 27 |
* \struct WpSpaJsonBuilder |
| 28 |
* |
| 29 |
* \since 0.4.8 |
| 30 |
*/ |
| 31 |
/*! |
| 32 |
* \struct WpSpaJsonParser |
| 33 |
* |
| 34 |
* \since 0.4.8 |
| 35 |
*/ |
| 36 |
|
| 37 |
static void |
| 38 |
builder_add_formatted (WpSpaJsonBuilder *self, const gchar *fmt, ...) |
| 39 |
G_GNUC_PRINTF (2, 3); |
| 40 |
|
| 41 |
static WpSpaJsonBuilder * |
| 42 |
wp_spa_json_builder_new_formatted (const gchar *fmt, ...) |
| 43 |
G_GNUC_PRINTF (1, 2); |
| 44 |
|
| 45 |
enum { |
| 46 |
FLAG_NO_OWNERSHIP = (1 << 0), |
| 47 |
}; |
| 48 |
|
| 49 |
struct _WpSpaJson |
| 50 |
{ |
| 51 |
grefcount ref; |
| 52 |
guint32 flags; |
| 53 |
|
| 54 |
/* only used if FLAG_NO_OWNERSHIP is not set */ |
| 55 |
WpSpaJsonBuilder *builder; |
| 56 |
|
| 57 |
/* not used if constructed with _new_wrap() */ |
| 58 |
struct spa_json json_data; |
| 59 |
|
| 60 |
/* json data */ |
| 61 |
gchar *data; |
| 62 |
size_t size; |
| 63 |
struct spa_json *json; |
| 64 |
}; |
| 65 |
|
| 66 |
G_DEFINE_BOXED_TYPE (WpSpaJson, wp_spa_json, wp_spa_json_ref, wp_spa_json_unref) |
| 67 |
|
| 68 |
struct _WpSpaJsonBuilder |
| 69 |
{ |
| 70 |
gboolean add_separator; |
| 71 |
gchar *data; |
| 72 |
size_t size; |
| 73 |
size_t max_size; |
| 74 |
}; |
| 75 |
|
| 76 |
G_DEFINE_BOXED_TYPE (WpSpaJsonBuilder, wp_spa_json_builder, |
| 77 |
wp_spa_json_builder_ref, wp_spa_json_builder_unref) |
| 78 |
|
| 79 |
struct _WpSpaJsonParser |
| 80 |
{ |
| 81 |
WpSpaJson *json; |
| 82 |
struct spa_json data[2]; |
| 83 |
struct spa_json *pos; |
| 84 |
struct spa_json curr; |
| 85 |
}; |
| 86 |
|
| 87 |
G_DEFINE_BOXED_TYPE (WpSpaJsonParser, wp_spa_json_parser, |
| 88 |
wp_spa_json_parser_ref, wp_spa_json_parser_unref) |
| 89 |
|
| 90 |
/*! |
| 91 |
* \brief Increases the reference count of a spa json object |
| 92 |
* \ingroup wpspajson |
| 93 |
* \param self a spa json object |
| 94 |
* \returns (transfer full): \a self with an additional reference count on it |
| 95 |
*/ |
| 96 |
WpSpaJson * |
| 97 |
wp_spa_json_ref (WpSpaJson *self) |
| 98 |
{ |
| 99 |
g_ref_count_inc (&self->ref); |
| 100 |
return self; |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
wp_spa_json_free (WpSpaJson *self) |
| 105 |
{ |
| 106 |
g_clear_pointer (&self->builder, wp_spa_json_builder_unref); |
| 107 |
g_slice_free (WpSpaJson, self); |
| 108 |
} |
| 109 |
|
| 110 |
/*! |
| 111 |
* \brief Decreases the reference count on \a self and frees it when the ref |
| 112 |
* count reaches zero. |
| 113 |
* \ingroup wpspajson |
| 114 |
* \param self (transfer full): a spa json object |
| 115 |
*/ |
| 116 |
void |
| 117 |
wp_spa_json_unref (WpSpaJson *self) |
| 118 |
{ |
| 119 |
if (g_ref_count_dec (&self->ref)) |
| 120 |
wp_spa_json_free (self); |
| 121 |
} |
| 122 |
|
| 123 |
static WpSpaJsonBuilder * |
| 124 |
wp_spa_json_builder_new (const gchar *data, size_t size) |
| 125 |
{ |
| 126 |
WpSpaJsonBuilder *self = g_rc_box_new0 (WpSpaJsonBuilder); |
| 127 |
self->add_separator = FALSE; |
| 128 |
self->data = g_new0 (gchar, size + 1); |
| 129 |
self->max_size = size; |
| 130 |
memcpy (self->data, data, size), |
| 131 |
self->data[size] = '\0'; |
| 132 |
self->size = size; |
| 133 |
return self; |
| 134 |
} |
| 135 |
|
| 136 |
static WpSpaJsonBuilder * |
| 137 |
wp_spa_json_builder_new_empty (size_t size) |
| 138 |
{ |
| 139 |
WpSpaJsonBuilder *self = g_rc_box_new0 (WpSpaJsonBuilder); |
| 140 |
self->add_separator = FALSE; |
| 141 |
self->data = g_new0 (gchar, size + 1); |
| 142 |
self->max_size = size; |
| 143 |
self->data[0] = '\0'; |
| 144 |
self->size = 0; |
| 145 |
return self; |
| 146 |
} |
| 147 |
|
| 148 |
static WpSpaJsonBuilder * |
| 149 |
wp_spa_json_builder_new_formatted (const gchar *fmt, ...) |
| 150 |
{ |
| 151 |
va_list args; |
| 152 |
WpSpaJsonBuilder *self = g_rc_box_new0 (WpSpaJsonBuilder); |
| 153 |
self->add_separator = FALSE; |
| 154 |
va_start (args, fmt); |
| 155 |
self->data = g_strdup_vprintf (fmt, args); |
| 156 |
va_end (args); |
| 157 |
self->size = strlen (self->data); |
| 158 |
self->max_size = self->size; |
| 159 |
return self; |
| 160 |
} |
| 161 |
|
| 162 |
static WpSpaJson * |
| 163 |
wp_spa_json_new_from_builder (WpSpaJsonBuilder *builder) |
| 164 |
{ |
| 165 |
WpSpaJson *self = g_slice_new0 (WpSpaJson); |
| 166 |
g_ref_count_init (&self->ref); |
| 167 |
self->flags = 0; |
| 168 |
self->builder = builder; |
| 169 |
self->data = builder->data; |
| 170 |
self->size = builder->size; |
| 171 |
spa_json_init (&self->json_data, self->data, self->size); |
| 172 |
self->json = &self->json_data; |
| 173 |
return self; |
| 174 |
} |
| 175 |
|
| 176 |
static WpSpaJson * |
| 177 |
wp_spa_json_new (const gchar *data, size_t size) |
| 178 |
{ |
| 179 |
return wp_spa_json_new_from_builder (wp_spa_json_builder_new (data, size)); |
| 180 |
} |
| 181 |
|
| 182 |
/*! |
| 183 |
* \brief Constructs a new WpSpaJson from a JSON string. |
| 184 |
* |
| 185 |
* \ingroup wpspajson |
| 186 |
* \param json_str a JSON string |
| 187 |
* \returns a new WpSpaJson; unlike the "wrap" variants, this function copies |
| 188 |
* the data in \a json_str, so it does not need to stay alive. |
| 189 |
* \since 0.5.0 |
| 190 |
*/ |
| 191 |
WP_API |
| 192 |
WpSpaJson * wp_spa_json_new_from_string (const gchar *json_str) |
| 193 |
{ |
| 194 |
return wp_spa_json_new (json_str, strlen (json_str)); |
| 195 |
} |
| 196 |
|
| 197 |
/*! |
| 198 |
* \brief Constructs a new WpSpaJson from a JSON string with specific length. |
| 199 |
* |
| 200 |
* \ingroup wpspajson |
| 201 |
* \param json_str a JSON string |
| 202 |
* \param len the specific length of the string |
| 203 |
* \returns a new WpSpaJson; unlike the "wrap" variants, this function copies |
| 204 |
* the data in \a json_str, so it does not need to stay alive. |
| 205 |
* \since 0.5.0 |
| 206 |
*/ |
| 207 |
WP_API |
| 208 |
WpSpaJson * wp_spa_json_new_from_stringn (const gchar *json_str, size_t len) |
| 209 |
{ |
| 210 |
return wp_spa_json_new (json_str, len); |
| 211 |
} |
| 212 |
|
| 213 |
/*! |
| 214 |
* \brief Constructs a new WpSpaJson that wraps a JSON string. |
| 215 |
* |
| 216 |
* \ingroup wpspajson |
| 217 |
* \param json_str a JSON string |
| 218 |
* \returns a new WpSpaJson that references the data in \a json_str. \a json_str |
| 219 |
* is not copied, so it needs to stay alive. |
| 220 |
* \since 0.5.0 |
| 221 |
*/ |
| 222 |
WpSpaJson * |
| 223 |
wp_spa_json_new_wrap_string (const gchar *json_str) |
| 224 |
{ |
| 225 |
return wp_spa_json_new_wrap_stringn(json_str, strlen (json_str)); |
| 226 |
} |
| 227 |
|
| 228 |
/*! |
| 229 |
* \brief Constructs a new WpSpaJson that wraps a JSON string with specific length. |
| 230 |
* |
| 231 |
* \ingroup wpspajson |
| 232 |
* \param json_str a JSON string |
| 233 |
* \param len the specific length of the string |
| 234 |
* \returns a new WpSpaJson that references the data in \a json_str. \a json_str |
| 235 |
* is not copied, so it needs to stay alive. |
| 236 |
* \since 0.5.0 |
| 237 |
*/ |
| 238 |
WpSpaJson * |
| 239 |
wp_spa_json_new_wrap_stringn (const gchar *json_str, size_t len) |
| 240 |
{ |
| 241 |
WpSpaJson *self = g_slice_new0 (WpSpaJson); |
| 242 |
g_ref_count_init (&self->ref); |
| 243 |
self->flags = FLAG_NO_OWNERSHIP; |
| 244 |
spa_json_init (&self->json_data, json_str, len); |
| 245 |
self->builder = NULL; |
| 246 |
self->data = (gchar *)self->json_data.cur; |
| 247 |
self->size = self->json_data.end - self->json_data.cur; |
| 248 |
self->json = &self->json_data; |
| 249 |
return self; |
| 250 |
} |
| 251 |
|
| 252 |
/*! |
| 253 |
* \brief Constructs a new WpSpaJson that wraps the given `spa_json`. |
| 254 |
* |
| 255 |
* \ingroup wpspajson |
| 256 |
* \param json a spa_json |
| 257 |
* \returns a new WpSpaJson that references the data in \a json. \a json is not |
| 258 |
* copied, so it needs to stay alive. |
| 259 |
*/ |
| 260 |
WpSpaJson * |
| 261 |
wp_spa_json_new_wrap (struct spa_json *json) |
| 262 |
{ |
| 263 |
WpSpaJson *self = g_slice_new0 (WpSpaJson); |
| 264 |
g_ref_count_init (&self->ref); |
| 265 |
self->flags = FLAG_NO_OWNERSHIP; |
| 266 |
self->builder = NULL; |
| 267 |
self->data = (gchar *)json->cur; |
| 268 |
self->size = json->end - json->cur; |
| 269 |
self->json = json; |
| 270 |
return self; |
| 271 |
} |
| 272 |
|
| 273 |
/*! |
| 274 |
* \brief Converts a WpSpaJson pointer to a `struct spa_json` one, for use with |
| 275 |
* native pipewire & spa functions. The returned pointer is owned by WpSpaJson |
| 276 |
* and may not be modified or freed. |
| 277 |
* |
| 278 |
* \ingroup wpspajson |
| 279 |
* \param self a spa json object |
| 280 |
* \returns a const pointer to the underlying spa_json structure |
| 281 |
*/ |
| 282 |
const struct spa_json * |
| 283 |
wp_spa_json_get_spa_json (const WpSpaJson *self) |
| 284 |
{ |
| 285 |
return self->json; |
| 286 |
} |
| 287 |
|
| 288 |
/*! |
| 289 |
* \brief Returns the json data |
| 290 |
* |
| 291 |
* \ingroup wpspajson |
| 292 |
* \param self a spa json object |
| 293 |
* \returns a const pointer to the json data |
| 294 |
*/ |
| 295 |
const gchar * |
| 296 |
wp_spa_json_get_data (const WpSpaJson *self) |
| 297 |
{ |
| 298 |
return self->data; |
| 299 |
} |
| 300 |
|
| 301 |
/*! |
| 302 |
* \brief Returns the json data size |
| 303 |
* |
| 304 |
* \ingroup wpspajson |
| 305 |
* \param self a spa json object |
| 306 |
* \returns the json data size |
| 307 |
*/ |
| 308 |
size_t |
| 309 |
wp_spa_json_get_size (const WpSpaJson *self) |
| 310 |
{ |
| 311 |
return self->size; |
| 312 |
} |
| 313 |
|
| 314 |
/*! |
| 315 |
* \brief Returns a newly allocated json string with length matching the size |
| 316 |
* |
| 317 |
* \ingroup wpspajson |
| 318 |
* \param self a spa json object |
| 319 |
* \returns (transfer full): the json string with length matching the size |
| 320 |
* \since 0.4.11 |
| 321 |
*/ |
| 322 |
gchar * |
| 323 |
wp_spa_json_to_string (const WpSpaJson *self) |
| 324 |
{ |
| 325 |
return g_strndup (self->data, self->size); |
| 326 |
} |
| 327 |
|
| 328 |
/*! |
| 329 |
* \brief Copies a spa json object |
| 330 |
* |
| 331 |
* \ingroup wpspajson |
| 332 |
* \param other a spa json object |
| 333 |
* \returns (transfer full): The newly copied spa json |
| 334 |
*/ |
| 335 |
WpSpaJson * |
| 336 |
wp_spa_json_copy (WpSpaJson *other) |
| 337 |
{ |
| 338 |
g_return_val_if_fail (other, NULL); |
| 339 |
g_return_val_if_fail (other->json, NULL); |
| 340 |
return wp_spa_json_new (other->data, other->size); |
| 341 |
} |
| 342 |
|
| 343 |
/*! |
| 344 |
* \brief Checks if the json is the unique owner of its data or not |
| 345 |
* |
| 346 |
* \ingroup wpspajson |
| 347 |
* \param self a spa json object |
| 348 |
* \returns TRUE if the json owns the data, FALSE otherwise. |
| 349 |
*/ |
| 350 |
gboolean |
| 351 |
wp_spa_json_is_unique_owner (WpSpaJson *self) |
| 352 |
{ |
| 353 |
return g_ref_count_compare (&self->ref, 1) && |
| 354 |
!(self->flags & FLAG_NO_OWNERSHIP); |
| 355 |
} |
| 356 |
|
| 357 |
/*! |
| 358 |
* \brief If \a self is not uniquely owned already, then it is unrefed and a |
| 359 |
* copy of it is returned instead. You should always consider \a self as unsafe |
| 360 |
* to use after this call and you should use the returned object instead. |
| 361 |
* |
| 362 |
* \ingroup wpspajson |
| 363 |
* \param self (transfer full): a spa json object |
| 364 |
* \returns (transfer full): the uniquely owned spa json object which may or may |
| 365 |
* not be the same as \a self. |
| 366 |
*/ |
| 367 |
WpSpaJson * |
| 368 |
wp_spa_json_ensure_unique_owner (WpSpaJson *self) |
| 369 |
{ |
| 370 |
WpSpaJson *copy = NULL; |
| 371 |
|
| 372 |
if (wp_spa_json_is_unique_owner (self)) |
| 373 |
return self; |
| 374 |
|
| 375 |
copy = wp_spa_json_copy (self); |
| 376 |
wp_spa_json_unref (self); |
| 377 |
return copy; |
| 378 |
} |
| 379 |
|
| 380 |
/*! |
| 381 |
* \brief Creates a spa json of type NULL |
| 382 |
* \ingroup wpspajson |
| 383 |
* \returns (transfer full): The new spa json |
| 384 |
*/ |
| 385 |
WpSpaJson * |
| 386 |
wp_spa_json_new_null (void) |
| 387 |
{ |
| 388 |
return wp_spa_json_new_from_builder ( |
| 389 |
wp_spa_json_builder_new_formatted ("%s", "null")); |
| 390 |
} |
| 391 |
|
| 392 |
/*! |
| 393 |
* \brief Creates a spa json of type boolean |
| 394 |
* |
| 395 |
* \ingroup wpspajson |
| 396 |
* \param value the boolean value |
| 397 |
* \returns (transfer full): The new spa json |
| 398 |
*/ |
| 399 |
WpSpaJson * |
| 400 |
wp_spa_json_new_boolean (gboolean value) |
| 401 |
{ |
| 402 |
return wp_spa_json_new_from_builder ( |
| 403 |
wp_spa_json_builder_new_formatted ("%s", value ? "true" : "false")); |
| 404 |
} |
| 405 |
|
| 406 |
/*! |
| 407 |
* \brief Creates a spa json of type int |
| 408 |
* |
| 409 |
* \ingroup wpspajson |
| 410 |
* \param value the int value |
| 411 |
* \returns (transfer full): The new spa json |
| 412 |
*/ |
| 413 |
WpSpaJson * |
| 414 |
wp_spa_json_new_int (gint value) |
| 415 |
{ |
| 416 |
return wp_spa_json_new_from_builder ( |
| 417 |
wp_spa_json_builder_new_formatted ("%d", value)); |
| 418 |
} |
| 419 |
|
| 420 |
/*! |
| 421 |
* \brief Creates a spa json of type float |
| 422 |
* |
| 423 |
* \ingroup wpspajson |
| 424 |
* \param value the float value |
| 425 |
* \returns (transfer full): The new spa json |
| 426 |
*/ |
| 427 |
WpSpaJson * |
| 428 |
wp_spa_json_new_float (float value) |
| 429 |
{ |
| 430 |
return wp_spa_json_new_from_builder ( |
| 431 |
wp_spa_json_builder_new_formatted ("%.6f", value)); |
| 432 |
} |
| 433 |
|
| 434 |
static void |
| 435 |
ensure_allocated_max_size (WpSpaJsonBuilder *self, size_t size) |
| 436 |
{ |
| 437 |
size_t new_size = self->size + size + 1; /* '\0' because of vsnprintf */ |
| 438 |
if (new_size > self->max_size) { |
| 439 |
size_t next_size = new_size * 2; |
| 440 |
self->data = g_realloc (self->data, next_size); |
| 441 |
self->max_size = next_size; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
/*! |
| 446 |
* \brief Creates a spa json of type string |
| 447 |
* |
| 448 |
* \ingroup wpspajson |
| 449 |
* \param value the string value |
| 450 |
* \returns (transfer full): The new spa json |
| 451 |
*/ |
| 452 |
WpSpaJson * |
| 453 |
wp_spa_json_new_string (const gchar *value) |
| 454 |
{ |
| 455 |
WpSpaJsonBuilder *b = wp_spa_json_builder_new_empty (strlen (value)); |
| 456 |
size_t enc_size = spa_json_encode_string (b->data + b->size, |
| 457 |
b->max_size - b->size, value); |
| 458 |
if (enc_size + 1 > b->max_size - b->size) { |
| 459 |
ensure_allocated_max_size (b, enc_size); |
| 460 |
enc_size = spa_json_encode_string (b->data + b->size, |
| 461 |
b->max_size - b->size, value); |
| 462 |
g_assert (enc_size < b->max_size - b->size); |
| 463 |
} |
| 464 |
b->size += enc_size; |
| 465 |
return wp_spa_json_new_from_builder (b); |
| 466 |
} |
| 467 |
|
| 468 |
/* Args is not a pointer in some architectures, so this needs to be a macro to |
| 469 |
* avoid args being copied */ |
| 470 |
#define wp_spa_json_builder_add_value(self,fmt,args) \ |
| 471 |
do { \ |
| 472 |
switch (*fmt) { \ |
| 473 |
case 'n': \ |
| 474 |
wp_spa_json_builder_add_null (self); \ |
| 475 |
break; \ |
| 476 |
case 'b': \ |
| 477 |
wp_spa_json_builder_add_boolean (self, va_arg(args, gboolean)); \ |
| 478 |
break; \ |
| 479 |
case 'i': \ |
| 480 |
wp_spa_json_builder_add_int (self, va_arg(args, gint)); \ |
| 481 |
break; \ |
| 482 |
case 'f': \ |
| 483 |
wp_spa_json_builder_add_float (self, (float)va_arg(args, double)); \ |
| 484 |
break; \ |
| 485 |
case 's': \ |
| 486 |
wp_spa_json_builder_add_string (self, va_arg(args, const gchar *)); \ |
| 487 |
break; \ |
| 488 |
case 'J': \ |
| 489 |
wp_spa_json_builder_add_json (self, va_arg(args, WpSpaJson *)); \ |
| 490 |
break; \ |
| 491 |
default: \ |
| 492 |
break; \ |
| 493 |
} \ |
| 494 |
} while(false) |
| 495 |
|
| 496 |
/*! |
| 497 |
* \brief Creates a spa json of type array |
| 498 |
* |
| 499 |
* \ingroup wpspajson |
| 500 |
* \param format (nullable): the first value format ("n", "b", "i", "f", "s" or "J") |
| 501 |
* \param ... a list of array types and values, followed by NULL |
| 502 |
* \returns (transfer full): The new spa json |
| 503 |
*/ |
| 504 |
WpSpaJson * |
| 505 |
wp_spa_json_new_array (const gchar *format, ...) |
| 506 |
{ |
| 507 |
va_list args; |
| 508 |
WpSpaJson *res; |
| 509 |
|
| 510 |
va_start (args, format); |
| 511 |
res = wp_spa_json_new_array_valist (format, args); |
| 512 |
va_end (args); |
| 513 |
return res; |
| 514 |
} |
| 515 |
|
| 516 |
/*! |
| 517 |
* \brief This is the `va_list` version of wp_spa_json_new_array() |
| 518 |
* |
| 519 |
* \ingroup wpspajson |
| 520 |
* \param format (nullable): the first value format ("n", "b", "i", "f", "s" or "J") |
| 521 |
* \param args the variable arguments passed to wp_spa_json_new_array() |
| 522 |
* \returns (transfer full): The new spa json |
| 523 |
*/ |
| 524 |
WpSpaJson * |
| 525 |
wp_spa_json_new_array_valist (const gchar *format, va_list args) |
| 526 |
{ |
| 527 |
g_autoptr (WpSpaJsonBuilder) b = wp_spa_json_builder_new_array (); |
| 528 |
|
| 529 |
if (!format) |
| 530 |
return wp_spa_json_builder_end (b); |
| 531 |
|
| 532 |
wp_spa_json_builder_add_value (b, format, args); |
| 533 |
wp_spa_json_builder_add_valist (b, args); |
| 534 |
return wp_spa_json_builder_end (b); |
| 535 |
} |
| 536 |
|
| 537 |
/*! |
| 538 |
* \brief Creates a spa json of type object |
| 539 |
* |
| 540 |
* \ingroup wpspajson |
| 541 |
* \param key (nullable): the first object property key |
| 542 |
* \param format (nullable): the first property format ("n", "b", "i", "f", "s" or "J") |
| 543 |
* \param ... a list of object properties and values, followed by NULL |
| 544 |
* \returns (transfer full): The new spa json |
| 545 |
*/ |
| 546 |
WpSpaJson * |
| 547 |
wp_spa_json_new_object (const gchar *key, const gchar *format, ...) |
| 548 |
{ |
| 549 |
va_list args; |
| 550 |
WpSpaJson *res; |
| 551 |
|
| 552 |
va_start (args, format); |
| 553 |
res = wp_spa_json_new_object_valist (key, format, args); |
| 554 |
va_end (args); |
| 555 |
return res; |
| 556 |
} |
| 557 |
|
| 558 |
/*! |
| 559 |
* \brief This is the `va_list` version of wp_spa_json_new_object() |
| 560 |
* |
| 561 |
* \ingroup wpspajson |
| 562 |
* \param key (nullable): the first object property key |
| 563 |
* \param format (nullable): the first property format ("n", "b", "i", "f", "s" or "J") |
| 564 |
* \param args the variable arguments passed to wp_spa_json_new_object() |
| 565 |
* \returns (transfer full): The new spa json |
| 566 |
*/ |
| 567 |
WpSpaJson * |
| 568 |
wp_spa_json_new_object_valist (const gchar *key, const gchar *format, |
| 569 |
va_list args) |
| 570 |
{ |
| 571 |
g_autoptr (WpSpaJsonBuilder) b = wp_spa_json_builder_new_object (); |
| 572 |
|
| 573 |
if (!key || !format) |
| 574 |
return wp_spa_json_builder_end (b); |
| 575 |
|
| 576 |
wp_spa_json_builder_add_property (b, key); |
| 577 |
wp_spa_json_builder_add_value (b, format, args); |
| 578 |
wp_spa_json_builder_add_valist (b, args); |
| 579 |
return wp_spa_json_builder_end (b); |
| 580 |
} |
| 581 |
|
| 582 |
/*! |
| 583 |
* \brief Checks whether the spa json is of type null or not |
| 584 |
* |
| 585 |
* \ingroup wpspajson |
| 586 |
* \param self the spa json object |
| 587 |
* \returns TRUE if it is of type null, FALSE otherwise |
| 588 |
*/ |
| 589 |
gboolean |
| 590 |
wp_spa_json_is_null (WpSpaJson *self) |
| 591 |
{ |
| 592 |
return spa_json_is_null (self->data, self->size); |
| 593 |
} |
| 594 |
|
| 595 |
/*! |
| 596 |
* \brief Checks whether the spa json is of type boolean or not |
| 597 |
* |
| 598 |
* \ingroup wpspajson |
| 599 |
* \param self the spa json object |
| 600 |
* \returns TRUE if it is of type boolean, FALSE otherwise |
| 601 |
*/ |
| 602 |
gboolean |
| 603 |
wp_spa_json_is_boolean (WpSpaJson *self) |
| 604 |
{ |
| 605 |
return spa_json_is_bool (self->data, self->size); |
| 606 |
} |
| 607 |
|
| 608 |
/*! |
| 609 |
* \brief Checks whether the spa json is of type int or not |
| 610 |
* |
| 611 |
* \ingroup wpspajson |
| 612 |
* \param self the spa json object |
| 613 |
* \returns TRUE if it is of type int, FALSE otherwise |
| 614 |
*/ |
| 615 |
gboolean |
| 616 |
wp_spa_json_is_int (WpSpaJson *self) |
| 617 |
{ |
| 618 |
return spa_json_is_int (self->data, self->size); |
| 619 |
} |
| 620 |
|
| 621 |
/*! |
| 622 |
* \brief Checks whether the spa json is of type float or not |
| 623 |
* |
| 624 |
* \ingroup wpspajson |
| 625 |
* \param self the spa json object |
| 626 |
* \returns TRUE if it is of type float, FALSE otherwise |
| 627 |
*/ |
| 628 |
gboolean |
| 629 |
wp_spa_json_is_float (WpSpaJson *self) |
| 630 |
{ |
| 631 |
return spa_json_is_float (self->data, self->size); |
| 632 |
} |
| 633 |
|
| 634 |
/*! |
| 635 |
* \brief Checks whether the spa json is of type string or not |
| 636 |
* |
| 637 |
* \ingroup wpspajson |
| 638 |
* \param self the spa json object |
| 639 |
* \returns TRUE if it is of type string, FALSE otherwise |
| 640 |
*/ |
| 641 |
gboolean |
| 642 |
wp_spa_json_is_string (WpSpaJson *self) |
| 643 |
{ |
| 644 |
return spa_json_is_string (self->data, self->size); |
| 645 |
} |
| 646 |
|
| 647 |
/*! |
| 648 |
* \brief Checks whether the spa json is of type array or not |
| 649 |
* |
| 650 |
* \ingroup wpspajson |
| 651 |
* \param self the spa json object |
| 652 |
* \returns TRUE if it is of type array, FALSE otherwise |
| 653 |
*/ |
| 654 |
gboolean |
| 655 |
wp_spa_json_is_array (WpSpaJson *self) |
| 656 |
{ |
| 657 |
return spa_json_is_array (self->data, self->size); |
| 658 |
} |
| 659 |
|
| 660 |
/*! |
| 661 |
* \brief Checks whether the spa json is of type container or not |
| 662 |
* |
| 663 |
* \ingroup wpspajson |
| 664 |
* \param self the spa json object |
| 665 |
* \returns TRUE if it is of type container, FALSE otherwise |
| 666 |
*/ |
| 667 |
gboolean |
| 668 |
wp_spa_json_is_container (WpSpaJson *self) |
| 669 |
{ |
| 670 |
return spa_json_is_container (self->data, self->size); |
| 671 |
} |
| 672 |
|
| 673 |
/*! |
| 674 |
* \brief Checks whether the spa json is of type object or not |
| 675 |
* |
| 676 |
* \ingroup wpspajson |
| 677 |
* \param self the spa json object |
| 678 |
* \returns TRUE if it is of type object, FALSE otherwise |
| 679 |
*/ |
| 680 |
gboolean |
| 681 |
wp_spa_json_is_object (WpSpaJson *self) |
| 682 |
{ |
| 683 |
return spa_json_is_object (self->data, self->size); |
| 684 |
} |
| 685 |
|
| 686 |
gboolean |
| 687 |
wp_spa_json_parse_boolean_internal (const gchar *data, int len, gboolean *value) |
| 688 |
{ |
| 689 |
bool v = false; |
| 690 |
if (spa_json_parse_bool (data, len, &v) < 0) |
| 691 |
return FALSE; |
| 692 |
*value = v ? TRUE : FALSE; |
| 693 |
return TRUE; |
| 694 |
} |
| 695 |
|
| 696 |
/*! |
| 697 |
* \brief Parses the boolean value of a spa json object |
| 698 |
* |
| 699 |
* \ingroup wpspajson |
| 700 |
* \param self the spa json object |
| 701 |
* \param value (out): the boolean value |
| 702 |
* \returns TRUE if the value was obtained, FALSE otherwise |
| 703 |
*/ |
| 704 |
gboolean |
| 705 |
wp_spa_json_parse_boolean (WpSpaJson *self, gboolean *value) |
| 706 |
{ |
| 707 |
return wp_spa_json_parse_boolean_internal (self->data, self->size, value); |
| 708 |
} |
| 709 |
|
| 710 |
/*! |
| 711 |
* \brief Parses the int value of a spa json object |
| 712 |
* |
| 713 |
* \ingroup wpspajson |
| 714 |
* \param self the spa json object |
| 715 |
* \param value (out): the int value |
| 716 |
* \returns TRUE if the value was obtained, FALSE otherwise |
| 717 |
*/ |
| 718 |
gboolean |
| 719 |
wp_spa_json_parse_int (WpSpaJson *self, gint *value) |
| 720 |
{ |
| 721 |
return spa_json_parse_int (self->data, self->size, value) >= 0; |
| 722 |
} |
| 723 |
|
| 724 |
/*! |
| 725 |
* \brief Parses the float value of a spa json object |
| 726 |
* |
| 727 |
* \ingroup wpspajson |
| 728 |
* \param self the spa json object |
| 729 |
* \param value (out): the float value |
| 730 |
* \returns TRUE if the value was obtained, FALSE otherwise |
| 731 |
*/ |
| 732 |
gboolean |
| 733 |
wp_spa_json_parse_float (WpSpaJson *self, float *value) |
| 734 |
{ |
| 735 |
return spa_json_parse_float (self->data, self->size, value) >= 0; |
| 736 |
} |
| 737 |
|
| 738 |
static gchar * |
| 739 |
wp_spa_json_parse_string_internal (const gchar *data, int len) |
| 740 |
{ |
| 741 |
gchar *res = g_new0 (gchar, len+1); |
| 742 |
if (res) |
| 743 |
spa_json_parse_string (data, len, res); |
| 744 |
return res; |
| 745 |
} |
| 746 |
|
| 747 |
/*! |
| 748 |
* \brief Parses the string value of a spa json object |
| 749 |
* |
| 750 |
* \ingroup wpspajson |
| 751 |
* \param self the spa json object |
| 752 |
* \returns (transfer full): The newly allocated parsed string |
| 753 |
*/ |
| 754 |
gchar * |
| 755 |
wp_spa_json_parse_string (WpSpaJson *self) |
| 756 |
{ |
| 757 |
return wp_spa_json_parse_string_internal (self->data, self->size); |
| 758 |
} |
| 759 |
|
| 760 |
/*! |
| 761 |
* \brief Parses the array types and values of a spa json object |
| 762 |
* |
| 763 |
* \ingroup wpspajson |
| 764 |
* \param self the spa json object |
| 765 |
* \param ... (out): the list of array types and values, followed by NULL |
| 766 |
* \returns TRUE if the types and values were obtained, FALSE otherwise |
| 767 |
*/ |
| 768 |
gboolean |
| 769 |
wp_spa_json_parse_array (WpSpaJson *self, ...) |
| 770 |
{ |
| 771 |
va_list args; |
| 772 |
gboolean res; |
| 773 |
va_start (args, self); |
| 774 |
res = wp_spa_json_parse_array_valist (self, args); |
| 775 |
va_end (args); |
| 776 |
return res; |
| 777 |
} |
| 778 |
|
| 779 |
/*! |
| 780 |
* \brief This is the `va_list` version of wp_spa_json_parse_array() |
| 781 |
* |
| 782 |
* \ingroup wpspajson |
| 783 |
* \param self the spa json object |
| 784 |
* \param args (out): the variable arguments passed to wp_spa_json_parse_array() |
| 785 |
* \returns TRUE if the types and values were obtained, FALSE otherwise |
| 786 |
*/ |
| 787 |
gboolean |
| 788 |
wp_spa_json_parse_array_valist (WpSpaJson *self, va_list args) |
| 789 |
{ |
| 790 |
g_autoptr (WpSpaJsonParser) p = wp_spa_json_parser_new_array (self); |
| 791 |
gboolean res = wp_spa_json_parser_get_valist (p, args); |
| 792 |
if (res) |
| 793 |
wp_spa_json_parser_end (p); |
| 794 |
return res; |
| 795 |
} |
| 796 |
|
| 797 |
/*! |
| 798 |
* \brief Parses the object properties and values of a spa json object |
| 799 |
* |
| 800 |
* \ingroup wpspajson |
| 801 |
* \param self the spa json object |
| 802 |
* \param ... (out): the list of object properties and values, followed by NULL |
| 803 |
* \returns TRUE if the properties and values were obtained, FALSE otherwise |
| 804 |
*/ |
| 805 |
gboolean |
| 806 |
wp_spa_json_parse_object (WpSpaJson *self, ...) |
| 807 |
{ |
| 808 |
va_list args; |
| 809 |
gboolean res; |
| 810 |
va_start (args, self); |
| 811 |
res = wp_spa_json_parse_object_valist (self, args); |
| 812 |
va_end (args); |
| 813 |
return res; |
| 814 |
} |
| 815 |
|
| 816 |
/*! |
| 817 |
* \brief This is the `va_list` version of wp_spa_json_parse_object() |
| 818 |
* |
| 819 |
* \ingroup wpspajson |
| 820 |
* \param self the spa json object |
| 821 |
* \param args (out): the variable arguments passed to wp_spa_json_parse_object() |
| 822 |
* \returns TRUE if the properties and values were obtained, FALSE otherwise |
| 823 |
*/ |
| 824 |
gboolean |
| 825 |
wp_spa_json_parse_object_valist (WpSpaJson *self, va_list args) |
| 826 |
{ |
| 827 |
g_autoptr (WpSpaJsonParser) p = wp_spa_json_parser_new_object (self); |
| 828 |
gboolean res = wp_spa_json_parser_get_valist (p, args); |
| 829 |
if (res) |
| 830 |
wp_spa_json_parser_end (p); |
| 831 |
return res; |
| 832 |
} |
| 833 |
|
| 834 |
/* Args is not a pointer in some architectures, so this needs to be a macro to |
| 835 |
* avoid args being copied */ |
| 836 |
#define wp_spa_json_parse_value(data,len,fmt,args) \ |
| 837 |
do { \ |
| 838 |
switch (*fmt) { \ |
| 839 |
case 'n': \ |
| 840 |
if (!spa_json_is_null (data, len)) \ |
| 841 |
return FALSE; \ |
| 842 |
break; \ |
| 843 |
case 'b': \ |
| 844 |
if (!wp_spa_json_parse_boolean_internal (data, len, \ |
| 845 |
va_arg(args, gboolean *))) \ |
| 846 |
return FALSE; \ |
| 847 |
break; \ |
| 848 |
case 'i': \ |
| 849 |
if (spa_json_parse_int (data, len, va_arg(args, gint *)) < 0) \ |
| 850 |
return FALSE; \ |
| 851 |
break; \ |
| 852 |
case 'f': \ |
| 853 |
if (spa_json_parse_float (data, len, va_arg(args, float *)) < 0) \ |
| 854 |
return FALSE; \ |
| 855 |
break; \ |
| 856 |
case 's': { \ |
| 857 |
gchar *str = wp_spa_json_parse_string_internal (data, len); \ |
| 858 |
if (!str) \ |
| 859 |
return FALSE; \ |
| 860 |
*va_arg(args, gchar **) = str; \ |
| 861 |
break; \ |
| 862 |
} \ |
| 863 |
case 'J': { \ |
| 864 |
WpSpaJson *j = wp_spa_json_new (data, len); \ |
| 865 |
if (!j) \ |
| 866 |
return FALSE; \ |
| 867 |
*va_arg(args, WpSpaJson **) = j; \ |
| 868 |
break; \ |
| 869 |
} \ |
| 870 |
default: \ |
| 871 |
return FALSE; \ |
| 872 |
} \ |
| 873 |
} while(false) |
| 874 |
|
| 875 |
/*! |
| 876 |
* \brief Parses the object property values of a spa json object |
| 877 |
* |
| 878 |
* \ingroup wpspajson |
| 879 |
* \param self the spa json object |
| 880 |
* \param ... the list of property names, formats and values, followed by NULL |
| 881 |
* \returns TRUE if the properties and values were obtained, FALSE otherwise |
| 882 |
*/ |
| 883 |
gboolean |
| 884 |
wp_spa_json_object_get (WpSpaJson *self, ...) |
| 885 |
{ |
| 886 |
va_list args; |
| 887 |
gboolean res; |
| 888 |
va_start (args, self); |
| 889 |
res = wp_spa_json_object_get_valist (self, args); |
| 890 |
va_end (args); |
| 891 |
return res; |
| 892 |
} |
| 893 |
|
| 894 |
/*! |
| 895 |
* \brief This is the `va_list` version of wp_spa_json_object_get() |
| 896 |
* |
| 897 |
* \ingroup wpspajson |
| 898 |
* \param self the spa json object |
| 899 |
* \param args the variable arguments passed to wp_spa_json_object_get() |
| 900 |
* \returns TRUE if the properties and values were obtained, FALSE otherwise |
| 901 |
*/ |
| 902 |
gboolean |
| 903 |
wp_spa_json_object_get_valist (WpSpaJson *self, va_list args) |
| 904 |
{ |
| 905 |
g_auto (GValue) item = G_VALUE_INIT; |
| 906 |
g_autoptr (WpIterator) it = NULL; |
| 907 |
const gchar *lookup_key = NULL; |
| 908 |
const gchar *lookup_fmt = NULL; |
| 909 |
|
| 910 |
g_return_val_if_fail (wp_spa_json_is_object (self), FALSE); |
| 911 |
|
| 912 |
lookup_key = va_arg(args, const gchar *); |
| 913 |
if (!lookup_key) |
| 914 |
return TRUE; |
| 915 |
lookup_fmt = va_arg(args, const gchar *); |
| 916 |
if (!lookup_fmt) |
| 917 |
return FALSE; |
| 918 |
|
| 919 |
it = wp_spa_json_new_iterator (self); |
| 920 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 921 |
WpSpaJson *key = g_value_get_boxed (&item); |
| 922 |
g_autofree gchar *key_str = NULL; |
| 923 |
WpSpaJson *value = NULL; |
| 924 |
|
| 925 |
key_str = wp_spa_json_parse_string (key); |
| 926 |
g_return_val_if_fail (key_str, FALSE); |
| 927 |
|
| 928 |
g_value_unset (&item); |
| 929 |
if (!wp_iterator_next (it, &item)) |
| 930 |
return FALSE; |
| 931 |
value = g_value_get_boxed (&item); |
| 932 |
|
| 933 |
if (g_strcmp0 (key_str, lookup_key) == 0) { |
| 934 |
wp_spa_json_parse_value (value->data, value->size, lookup_fmt, args); |
| 935 |
lookup_key = va_arg(args, const gchar *); |
| 936 |
if (!lookup_key) |
| 937 |
return TRUE; |
| 938 |
lookup_fmt = va_arg(args, const gchar *); |
| 939 |
if (!lookup_fmt) |
| 940 |
return FALSE; |
| 941 |
wp_iterator_reset (it); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
return FALSE; |
| 946 |
} |
| 947 |
|
| 948 |
/*! |
| 949 |
* \brief Increases the reference count of a spa json builder |
| 950 |
* |
| 951 |
* \ingroup wpspajson |
| 952 |
* \param self a spa json builder object |
| 953 |
* \returns (transfer full): \a self with an additional reference count on it |
| 954 |
*/ |
| 955 |
WpSpaJsonBuilder * |
| 956 |
wp_spa_json_builder_ref (WpSpaJsonBuilder *self) |
| 957 |
{ |
| 958 |
return (WpSpaJsonBuilder *) g_rc_box_acquire ((gpointer) self); |
| 959 |
} |
| 960 |
|
| 961 |
static void |
| 962 |
wp_spa_json_builder_free (WpSpaJsonBuilder *self) |
| 963 |
{ |
| 964 |
g_clear_pointer (&self->data, g_free); |
| 965 |
} |
| 966 |
|
| 967 |
/*! |
| 968 |
* \brief Decreases the reference count on \a self and frees it when the ref |
| 969 |
* count reaches zero. |
| 970 |
* |
| 971 |
* \ingroup wpspajson |
| 972 |
* \param self (transfer full): a spa json builder object |
| 973 |
*/ |
| 974 |
void |
| 975 |
wp_spa_json_builder_unref (WpSpaJsonBuilder *self) |
| 976 |
{ |
| 977 |
g_rc_box_release_full (self, (GDestroyNotify) wp_spa_json_builder_free); |
| 978 |
} |
| 979 |
|
| 980 |
/*! |
| 981 |
* \brief Creates a spa json builder of type array |
| 982 |
* |
| 983 |
* \ingroup wpspajson |
| 984 |
* \returns (transfer full): the new spa json builder |
| 985 |
*/ |
| 986 |
WpSpaJsonBuilder * |
| 987 |
wp_spa_json_builder_new_array (void) |
| 988 |
{ |
| 989 |
WpSpaJsonBuilder *self = g_rc_box_new0 (WpSpaJsonBuilder); |
| 990 |
self->add_separator = FALSE; |
| 991 |
self->data = g_new0 (gchar, WP_SPA_JSON_BUILDER_INIT_SIZE); |
| 992 |
self->max_size = WP_SPA_JSON_BUILDER_INIT_SIZE; |
| 993 |
self->data[0] = '['; |
| 994 |
self->size = 1; |
| 995 |
return self; |
| 996 |
} |
| 997 |
|
| 998 |
/*! |
| 999 |
* \brief Creates a spa json builder of type object |
| 1000 |
* |
| 1001 |
* \ingroup wpspajson |
| 1002 |
* \returns (transfer full): the new spa json builder |
| 1003 |
*/ |
| 1004 |
WpSpaJsonBuilder * |
| 1005 |
wp_spa_json_builder_new_object (void) |
| 1006 |
{ |
| 1007 |
WpSpaJsonBuilder *self = g_rc_box_new0 (WpSpaJsonBuilder); |
| 1008 |
self->add_separator = FALSE; |
| 1009 |
self->data = g_new0 (gchar, WP_SPA_JSON_BUILDER_INIT_SIZE); |
| 1010 |
self->max_size = WP_SPA_JSON_BUILDER_INIT_SIZE; |
| 1011 |
self->data[0] = '{'; |
| 1012 |
self->size = 1; |
| 1013 |
return self; |
| 1014 |
} |
| 1015 |
|
| 1016 |
static void |
| 1017 |
ensure_separator (WpSpaJsonBuilder *self, gboolean for_property) |
| 1018 |
{ |
| 1019 |
gboolean insert = (self->data[0] == '{' && for_property) || |
| 1020 |
(self->data[0] == '[' && !for_property); |
| 1021 |
if (insert) { |
| 1022 |
if (self->add_separator) { |
| 1023 |
ensure_allocated_max_size (self, 2); |
| 1024 |
self->data[self->size++] = ','; |
| 1025 |
self->data[self->size++] = ' '; |
| 1026 |
} else { |
| 1027 |
self->add_separator = TRUE; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
static void |
| 1033 |
builder_add_formatted (WpSpaJsonBuilder *self, const gchar *fmt, ...) |
| 1034 |
{ |
| 1035 |
int s; |
| 1036 |
va_list args; |
| 1037 |
va_start (args, fmt); |
| 1038 |
s = vsnprintf (self->data + self->size, self->max_size - self->size, fmt, args); |
| 1039 |
va_end (args); |
| 1040 |
g_return_if_fail (s > 0); |
| 1041 |
self->size += s; |
| 1042 |
} |
| 1043 |
|
| 1044 |
static void |
| 1045 |
builder_add (WpSpaJsonBuilder *self, const gchar *data, size_t size) |
| 1046 |
{ |
| 1047 |
g_return_if_fail (self->max_size - self->size >= size + 1); |
| 1048 |
snprintf (self->data + self->size, size + 1, "%s", data); |
| 1049 |
self->size += size; |
| 1050 |
} |
| 1051 |
|
| 1052 |
/*! |
| 1053 |
* \brief Adds a property into the builder |
| 1054 |
* |
| 1055 |
* \ingroup wpspajson |
| 1056 |
* \param self the spa json builder object |
| 1057 |
* \param key the name of the property |
| 1058 |
*/ |
| 1059 |
void |
| 1060 |
wp_spa_json_builder_add_property (WpSpaJsonBuilder *self, const gchar *key) |
| 1061 |
{ |
| 1062 |
size_t enc_size; |
| 1063 |
ensure_separator (self, TRUE); |
| 1064 |
enc_size = spa_json_encode_string (self->data + self->size, |
| 1065 |
self->max_size - self->size, key); |
| 1066 |
if (enc_size + 2 > self->max_size - self->size) { |
| 1067 |
ensure_allocated_max_size (self, enc_size + 1); |
| 1068 |
enc_size = spa_json_encode_string (self->data + self->size, |
| 1069 |
self->max_size - self->size, key); |
| 1070 |
g_assert (enc_size + 1 < self->max_size - self->size); |
| 1071 |
} |
| 1072 |
self->data[self->size + enc_size] = ':'; |
| 1073 |
self->size += enc_size + 1; |
| 1074 |
} |
| 1075 |
|
| 1076 |
/*! |
| 1077 |
* \brief Adds a null value into the builder |
| 1078 |
* |
| 1079 |
* \ingroup wpspajson |
| 1080 |
* \param self the spa json builder object |
| 1081 |
*/ |
| 1082 |
void |
| 1083 |
wp_spa_json_builder_add_null (WpSpaJsonBuilder *self) |
| 1084 |
{ |
| 1085 |
ensure_separator (self, FALSE); |
| 1086 |
ensure_allocated_max_size (self, 4); |
| 1087 |
builder_add_formatted (self, "%s", "null"); |
| 1088 |
} |
| 1089 |
|
| 1090 |
/*! |
| 1091 |
* \brief Adds a boolean value into the builder |
| 1092 |
* |
| 1093 |
* \ingroup wpspajson |
| 1094 |
* \param self the spa json builder object |
| 1095 |
* \param value the boolean value |
| 1096 |
*/ |
| 1097 |
void |
| 1098 |
wp_spa_json_builder_add_boolean (WpSpaJsonBuilder *self, gboolean value) |
| 1099 |
{ |
| 1100 |
ensure_separator (self, FALSE); |
| 1101 |
ensure_allocated_max_size (self, value ? 4 : 5); |
| 1102 |
builder_add_formatted (self, "%s", value ? "true" : "false"); |
| 1103 |
} |
| 1104 |
|
| 1105 |
/*! |
| 1106 |
* \brief Adds a int value into the builder |
| 1107 |
* |
| 1108 |
* \ingroup wpspajson |
| 1109 |
* \param self the spa json builder object |
| 1110 |
* \param value the int value |
| 1111 |
*/ |
| 1112 |
void |
| 1113 |
wp_spa_json_builder_add_int (WpSpaJsonBuilder *self, gint value) |
| 1114 |
{ |
| 1115 |
ensure_separator (self, FALSE); |
| 1116 |
ensure_allocated_max_size (self, 16); |
| 1117 |
builder_add_formatted (self, "%d", value); |
| 1118 |
} |
| 1119 |
|
| 1120 |
/*! |
| 1121 |
* \brief Adds a float value into the builder |
| 1122 |
* |
| 1123 |
* \ingroup wpspajson |
| 1124 |
* \param self the spa json builder object |
| 1125 |
* \param value the float value |
| 1126 |
*/ |
| 1127 |
void |
| 1128 |
wp_spa_json_builder_add_float (WpSpaJsonBuilder *self, float value) |
| 1129 |
{ |
| 1130 |
ensure_separator (self, FALSE); |
| 1131 |
ensure_allocated_max_size (self, 32); |
| 1132 |
builder_add_formatted (self, "%.6f", value); |
| 1133 |
} |
| 1134 |
|
| 1135 |
/*! |
| 1136 |
* \brief Adds a string value into the builder |
| 1137 |
* |
| 1138 |
* \ingroup wpspajson |
| 1139 |
* \param self the spa json builder object |
| 1140 |
* \param value the string value |
| 1141 |
*/ |
| 1142 |
void |
| 1143 |
wp_spa_json_builder_add_string (WpSpaJsonBuilder *self, const gchar *value) |
| 1144 |
{ |
| 1145 |
size_t enc_size; |
| 1146 |
ensure_separator (self, FALSE); |
| 1147 |
enc_size = spa_json_encode_string (self->data + self->size, |
| 1148 |
self->max_size - self->size, value); |
| 1149 |
if (enc_size + 1 > self->max_size - self->size) { |
| 1150 |
ensure_allocated_max_size (self, enc_size); |
| 1151 |
enc_size = spa_json_encode_string (self->data + self->size, |
| 1152 |
self->max_size - self->size, value); |
| 1153 |
g_assert (enc_size < self->max_size - self->size); |
| 1154 |
} |
| 1155 |
self->size += enc_size; |
| 1156 |
} |
| 1157 |
|
| 1158 |
/*! |
| 1159 |
* \brief Adds a json value into the builder |
| 1160 |
* |
| 1161 |
* \ingroup wpspajson |
| 1162 |
* \param self the spa json builder object |
| 1163 |
* \param json (transfer none): the json value |
| 1164 |
*/ |
| 1165 |
void |
| 1166 |
wp_spa_json_builder_add_json (WpSpaJsonBuilder *self, WpSpaJson *json) |
| 1167 |
{ |
| 1168 |
ensure_separator (self, FALSE); |
| 1169 |
ensure_allocated_max_size (self, json->size); |
| 1170 |
builder_add (self, json->data, json->size); |
| 1171 |
} |
| 1172 |
|
| 1173 |
/*! |
| 1174 |
* \brief Adds a json string into the builder |
| 1175 |
* |
| 1176 |
* \ingroup wpspajson |
| 1177 |
* \param self the spa json builder object |
| 1178 |
* \param json_str the json string |
| 1179 |
*/ |
| 1180 |
void |
| 1181 |
wp_spa_json_builder_add_from_string (WpSpaJsonBuilder *self, |
| 1182 |
const gchar *json_str) |
| 1183 |
{ |
| 1184 |
wp_spa_json_builder_add_from_stringn (self, json_str, strlen (json_str)); |
| 1185 |
} |
| 1186 |
|
| 1187 |
/*! |
| 1188 |
* \brief Adds a json string with specific length into the builder |
| 1189 |
* |
| 1190 |
* \ingroup wpspajson |
| 1191 |
* \param self the spa json builder object |
| 1192 |
* \param json_str the json string |
| 1193 |
* \param len the specific length of the json string |
| 1194 |
*/ |
| 1195 |
void |
| 1196 |
wp_spa_json_builder_add_from_stringn (WpSpaJsonBuilder *self, |
| 1197 |
const gchar *json_str, size_t len) |
| 1198 |
{ |
| 1199 |
ensure_separator (self, FALSE); |
| 1200 |
ensure_allocated_max_size (self, len); |
| 1201 |
builder_add (self, json_str, len); |
| 1202 |
} |
| 1203 |
|
| 1204 |
/*! |
| 1205 |
* \brief Adds values into the builder |
| 1206 |
* |
| 1207 |
* \ingroup wpspajson |
| 1208 |
* \param self the spa json builder object |
| 1209 |
* \param ... the json values |
| 1210 |
*/ |
| 1211 |
void |
| 1212 |
wp_spa_json_builder_add (WpSpaJsonBuilder *self, ...) |
| 1213 |
{ |
| 1214 |
va_list args; |
| 1215 |
va_start (args, self); |
| 1216 |
wp_spa_json_builder_add_valist (self, args); |
| 1217 |
va_end (args); |
| 1218 |
} |
| 1219 |
|
| 1220 |
/*! |
| 1221 |
* \brief This is the `va_list` version of wp_spa_json_builder_add() |
| 1222 |
* |
| 1223 |
* \ingroup wpspajson |
| 1224 |
* \param self the spa json builder object |
| 1225 |
* \param args the variable arguments passed to wp_spa_json_builder_add() |
| 1226 |
*/ |
| 1227 |
void |
| 1228 |
wp_spa_json_builder_add_valist (WpSpaJsonBuilder *self, va_list args) |
| 1229 |
{ |
| 1230 |
do { |
| 1231 |
const char *format = NULL; |
| 1232 |
|
| 1233 |
/* add property key if object */ |
| 1234 |
if (self->data[0] == '{') { |
| 1235 |
const gchar *key = va_arg(args, const gchar *); |
| 1236 |
if (!key) |
| 1237 |
return; |
| 1238 |
wp_spa_json_builder_add_property (self, key); |
| 1239 |
} |
| 1240 |
|
| 1241 |
/* get value format */ |
| 1242 |
format = va_arg(args, const gchar *); |
| 1243 |
if (!format) |
| 1244 |
return; |
| 1245 |
|
| 1246 |
/* add value */ |
| 1247 |
wp_spa_json_builder_add_value (self, format, args); |
| 1248 |
} while (TRUE); |
| 1249 |
} |
| 1250 |
|
| 1251 |
/*! |
| 1252 |
* \brief Ends the builder process and returns the constructed spa json object |
| 1253 |
* |
| 1254 |
* \ingroup wpspajson |
| 1255 |
* \param self the spa json builder object |
| 1256 |
* \returns (transfer full): the constructed spa json object |
| 1257 |
*/ |
| 1258 |
WpSpaJson * |
| 1259 |
wp_spa_json_builder_end (WpSpaJsonBuilder *self) |
| 1260 |
{ |
| 1261 |
/* close */ |
| 1262 |
switch (self->data[0]) { |
| 1263 |
case '[': /* array */ |
| 1264 |
ensure_allocated_max_size (self, 2); |
| 1265 |
self->data[self->size++] = ']'; |
| 1266 |
self->data[self->size] = '\0'; |
| 1267 |
break; |
| 1268 |
case '{': /* object */ |
| 1269 |
ensure_allocated_max_size (self, 2); |
| 1270 |
self->data[self->size++] = '}'; |
| 1271 |
self->data[self->size] = '\0'; |
| 1272 |
break; |
| 1273 |
default: |
| 1274 |
break; |
| 1275 |
} |
| 1276 |
|
| 1277 |
return wp_spa_json_new_from_builder (wp_spa_json_builder_ref (self)); |
| 1278 |
} |
| 1279 |
|
| 1280 |
/*! |
| 1281 |
* \brief Increases the reference count of a spa json parser |
| 1282 |
* |
| 1283 |
* \ingroup wpspajson |
| 1284 |
* \param self a spa json parser object |
| 1285 |
* \returns (transfer full): \a self with an additional reference count on it |
| 1286 |
*/ |
| 1287 |
WpSpaJsonParser * |
| 1288 |
wp_spa_json_parser_ref (WpSpaJsonParser *self) |
| 1289 |
{ |
| 1290 |
return (WpSpaJsonParser *) g_rc_box_acquire ((gpointer) self); |
| 1291 |
} |
| 1292 |
|
| 1293 |
static void |
| 1294 |
wp_spa_json_parser_free (WpSpaJsonParser *self) |
| 1295 |
{ |
| 1296 |
self->json = NULL; |
| 1297 |
} |
| 1298 |
|
| 1299 |
/*! |
| 1300 |
* \brief Decreases the reference count on \a self and frees it when the ref |
| 1301 |
* count reaches zero. |
| 1302 |
* |
| 1303 |
* \ingroup wpspajson |
| 1304 |
* \param self (transfer full): a spa json parser object |
| 1305 |
*/ |
| 1306 |
void |
| 1307 |
wp_spa_json_parser_unref (WpSpaJsonParser *self) |
| 1308 |
{ |
| 1309 |
g_rc_box_release_full (self, (GDestroyNotify) wp_spa_json_parser_free); |
| 1310 |
} |
| 1311 |
|
| 1312 |
/*! |
| 1313 |
* \brief Creates a spa json array parser. The \a json object must be valid for |
| 1314 |
* the entire life-cycle of the returned parser. |
| 1315 |
* |
| 1316 |
* \ingroup wpspajson |
| 1317 |
* \param json the spa json array to parse |
| 1318 |
* \returns (transfer full): The new spa json parser |
| 1319 |
*/ |
| 1320 |
WpSpaJsonParser * |
| 1321 |
wp_spa_json_parser_new_array (WpSpaJson *json) |
| 1322 |
{ |
| 1323 |
WpSpaJsonParser *self; |
| 1324 |
|
| 1325 |
g_return_val_if_fail (wp_spa_json_is_array (json), NULL); |
| 1326 |
|
| 1327 |
self = g_rc_box_new0 (WpSpaJsonParser); |
| 1328 |
self->json = json; |
| 1329 |
self->data[0] = *json->json; |
| 1330 |
spa_json_enter_array (&self->data[0], &self->data[1]); |
| 1331 |
self->pos = &self->data[1]; |
| 1332 |
return self; |
| 1333 |
} |
| 1334 |
|
| 1335 |
/*! |
| 1336 |
* \brief Creates a spa json object parser. The \a json object must be valid for |
| 1337 |
* the entire life-cycle of the returned parser. |
| 1338 |
* |
| 1339 |
* \ingroup wpspajson |
| 1340 |
* \param json the spa json object to parse |
| 1341 |
* \returns (transfer full): The new spa json parser |
| 1342 |
*/ |
| 1343 |
WpSpaJsonParser * |
| 1344 |
wp_spa_json_parser_new_object (WpSpaJson *json) |
| 1345 |
{ |
| 1346 |
WpSpaJsonParser *self; |
| 1347 |
|
| 1348 |
g_return_val_if_fail (wp_spa_json_is_object (json), NULL); |
| 1349 |
|
| 1350 |
self = g_rc_box_new0 (WpSpaJsonParser); |
| 1351 |
self->json = json; |
| 1352 |
self->data[0] = *json->json; |
| 1353 |
spa_json_enter_object (&self->data[0], &self->data[1]); |
| 1354 |
self->pos = &self->data[1]; |
| 1355 |
return self; |
| 1356 |
} |
| 1357 |
|
| 1358 |
/*! |
| 1359 |
* \brief Creates a new spa json parser for undefined type of data. The \a json |
| 1360 |
* object must be valid for the entire life-cycle of the returned parser. |
| 1361 |
* |
| 1362 |
* This function allows creating a parser object for any type of spa json and is |
| 1363 |
* mostly useful to parse non-standard JSON data that should be treated as if it |
| 1364 |
* were an object or array, but does not start with a '{' or '[' character. Such |
| 1365 |
* data can be for instance a comma-separated list of single values (array) or |
| 1366 |
* key-value pairs (object). Such data is also the main configuration file, |
| 1367 |
* which is an object but doesn't start with a '{' character. |
| 1368 |
* |
| 1369 |
* \note If the data is an array or object, the parser will not enter it and the |
| 1370 |
* only token it will be able to parse is the same \a json object that is passed |
| 1371 |
* in as an argument. Use wp_spa_json_parser_new_array() or |
| 1372 |
* wp_spa_json_parser_new_object() to parse arrays or objects. |
| 1373 |
* |
| 1374 |
* \ingroup wpspajson |
| 1375 |
* \param json the spa json to parse |
| 1376 |
* \returns (transfer full): The new spa json parser |
| 1377 |
* \since 0.5.0 |
| 1378 |
*/ |
| 1379 |
WpSpaJsonParser * |
| 1380 |
wp_spa_json_parser_new_undefined (WpSpaJson *json) |
| 1381 |
{ |
| 1382 |
WpSpaJsonParser *self; |
| 1383 |
|
| 1384 |
self = g_rc_box_new0 (WpSpaJsonParser); |
| 1385 |
self->json = json; |
| 1386 |
self->data[0] = *json->json; |
| 1387 |
self->pos = &self->data[0]; |
| 1388 |
return self; |
| 1389 |
} |
| 1390 |
|
| 1391 |
static int |
| 1392 |
check_nested_size (struct spa_json *parent, const gchar *data, int size) |
| 1393 |
{ |
| 1394 |
const gchar *nested_data; |
| 1395 |
int nested_size; |
| 1396 |
struct spa_json nested[2]; |
| 1397 |
|
| 1398 |
/* only arrays and objects are considered nested data */ |
| 1399 |
if (!spa_json_is_array (data, size) && !spa_json_is_object (data, size)) |
| 1400 |
return 0; |
| 1401 |
|
| 1402 |
/* enter */ |
| 1403 |
nested[0] = *parent; |
| 1404 |
spa_json_enter (&nested[0], &nested[1]); |
| 1405 |
|
| 1406 |
/* recursively advance */ |
| 1407 |
while ((nested_size = spa_json_next (&nested[1], &nested_data)) > 0) { |
| 1408 |
if (check_nested_size (&nested[1], nested_data, nested_size) < 0) |
| 1409 |
return -1; |
| 1410 |
} |
| 1411 |
if (nested_size < 0) |
| 1412 |
return -1; |
| 1413 |
|
| 1414 |
/* advance one more time to reach end of nested data */ |
| 1415 |
if (spa_json_next (&nested[1], &nested_data) < 0) |
| 1416 |
return -1; |
| 1417 |
|
| 1418 |
return nested_data - data; |
| 1419 |
} |
| 1420 |
|
| 1421 |
static gboolean |
| 1422 |
wp_spa_json_parser_advance (WpSpaJsonParser *self) |
| 1423 |
{ |
| 1424 |
const gchar *data = NULL; |
| 1425 |
int size, nested_size; |
| 1426 |
|
| 1427 |
if (!self->pos) |
| 1428 |
return FALSE; |
| 1429 |
|
| 1430 |
/* advance */ |
| 1431 |
size = spa_json_next (self->pos, &data); |
| 1432 |
if (size <= 0) |
| 1433 |
return FALSE; |
| 1434 |
g_return_val_if_fail (data != NULL, FALSE); |
| 1435 |
|
| 1436 |
/* if array or object, add the nested size */ |
| 1437 |
nested_size = check_nested_size (self->pos, data, size); |
| 1438 |
if (nested_size < 0) |
| 1439 |
return FALSE; |
| 1440 |
size += nested_size; |
| 1441 |
|
| 1442 |
/* update current */ |
| 1443 |
spa_json_init (&self->curr, data, size); |
| 1444 |
return TRUE; |
| 1445 |
} |
| 1446 |
|
| 1447 |
/*! |
| 1448 |
* \brief Gets the null value from a spa json parser |
| 1449 |
* |
| 1450 |
* \ingroup wpspajson |
| 1451 |
* \param self the spa json parser object |
| 1452 |
* \returns TRUE if the null value is present, FALSE otherwise |
| 1453 |
*/ |
| 1454 |
gboolean |
| 1455 |
wp_spa_json_parser_get_null (WpSpaJsonParser *self) |
| 1456 |
{ |
| 1457 |
return wp_spa_json_parser_advance (self) && |
| 1458 |
spa_json_is_null (self->curr.cur, self->curr.end - self->curr.cur); |
| 1459 |
} |
| 1460 |
|
| 1461 |
/*! |
| 1462 |
* \brief Gets the boolean value from a spa json parser |
| 1463 |
* |
| 1464 |
* \ingroup wpspajson |
| 1465 |
* \param self the spa json parser object |
| 1466 |
* \param value (out): the boolean value |
| 1467 |
* \returns TRUE if the value was obtained, FALSE otherwise |
| 1468 |
*/ |
| 1469 |
gboolean |
| 1470 |
wp_spa_json_parser_get_boolean (WpSpaJsonParser *self, gboolean *value) |
| 1471 |
{ |
| 1472 |
return wp_spa_json_parser_advance (self) && |
| 1473 |
wp_spa_json_parse_boolean_internal (self->curr.cur, |
| 1474 |
self->curr.end - self->curr.cur, value); |
| 1475 |
} |
| 1476 |
|
| 1477 |
/*! |
| 1478 |
* \brief Gets the int value from a spa json parser object |
| 1479 |
* |
| 1480 |
* \ingroup wpspajson |
| 1481 |
* \param self the spa json parser object |
| 1482 |
* \param value (out): the int value |
| 1483 |
* \returns TRUE if the value was obtained, FALSE otherwise |
| 1484 |
*/ |
| 1485 |
gboolean |
| 1486 |
wp_spa_json_parser_get_int (WpSpaJsonParser *self, gint *value) |
| 1487 |
{ |
| 1488 |
return wp_spa_json_parser_advance (self) && |
| 1489 |
spa_json_parse_int (self->curr.cur, |
| 1490 |
self->curr.end - self->curr.cur, value) >= 0; |
| 1491 |
} |
| 1492 |
|
| 1493 |
/*! |
| 1494 |
* \brief Gets the float value from a spa json parser object |
| 1495 |
* |
| 1496 |
* \ingroup wpspajson |
| 1497 |
* \param self the spa json parser object |
| 1498 |
* \param value (out): the float value |
| 1499 |
* \returns TRUE if the value was obtained, FALSE otherwise |
| 1500 |
*/ |
| 1501 |
gboolean |
| 1502 |
wp_spa_json_parser_get_float (WpSpaJsonParser *self, float *value) |
| 1503 |
{ |
| 1504 |
return wp_spa_json_parser_advance (self) && |
| 1505 |
spa_json_parse_float (self->curr.cur, |
| 1506 |
self->curr.end - self->curr.cur, value) >= 0; |
| 1507 |
} |
| 1508 |
|
| 1509 |
/*! |
| 1510 |
* \brief Gets the string value from a spa json parser object |
| 1511 |
* |
| 1512 |
* \ingroup wpspajson |
| 1513 |
* \param self the spa json parser object |
| 1514 |
* \returns (transfer full): The newly allocated parsed string |
| 1515 |
*/ |
| 1516 |
gchar * |
| 1517 |
wp_spa_json_parser_get_string (WpSpaJsonParser *self) |
| 1518 |
{ |
| 1519 |
return wp_spa_json_parser_advance (self) ? |
| 1520 |
wp_spa_json_parse_string_internal (self->curr.cur, |
| 1521 |
self->curr.end - self->curr.cur) : NULL; |
| 1522 |
} |
| 1523 |
|
| 1524 |
/*! |
| 1525 |
* \brief Gets the spa json value from a spa json parser object |
| 1526 |
* |
| 1527 |
* \note the returned spa json object references the original data instead |
| 1528 |
* of copying it, therefore the original data must be valid for the entire |
| 1529 |
* life-cycle of the returned object |
| 1530 |
* |
| 1531 |
* \ingroup wpspajson |
| 1532 |
* \param self the spa json parser object |
| 1533 |
* \returns (transfer full): The spa json value or NULL if it could not be |
| 1534 |
* obtained |
| 1535 |
*/ |
| 1536 |
WpSpaJson * |
| 1537 |
wp_spa_json_parser_get_json (WpSpaJsonParser *self) |
| 1538 |
{ |
| 1539 |
return wp_spa_json_parser_advance (self) ? |
| 1540 |
wp_spa_json_new_wrap_stringn (self->curr.cur, |
| 1541 |
self->curr.end - self->curr.cur) : NULL; |
| 1542 |
} |
| 1543 |
|
| 1544 |
gboolean |
| 1545 |
wp_spa_json_parser_get_value (WpSpaJsonParser *self, const gchar *fmt, |
| 1546 |
va_list args) |
| 1547 |
{ |
| 1548 |
if (wp_spa_json_parser_advance (self)) { |
| 1549 |
wp_spa_json_parse_value (self->curr.cur, self->curr.end - self->curr.cur, |
| 1550 |
fmt, args); |
| 1551 |
return TRUE; |
| 1552 |
} |
| 1553 |
return FALSE; |
| 1554 |
} |
| 1555 |
|
| 1556 |
/*! |
| 1557 |
* \brief Gets the values from a spa json parser object |
| 1558 |
* |
| 1559 |
* \ingroup wpspajson |
| 1560 |
* \param self the spa json parser object |
| 1561 |
* \param ... (out): a list of values to get, followed by NULL |
| 1562 |
* \returns TRUE if the value was obtained, FALSE otherwise |
| 1563 |
*/ |
| 1564 |
gboolean |
| 1565 |
wp_spa_json_parser_get (WpSpaJsonParser *self, ...) |
| 1566 |
{ |
| 1567 |
gboolean res; |
| 1568 |
va_list args; |
| 1569 |
va_start (args, self); |
| 1570 |
res = wp_spa_json_parser_get_valist (self, args); |
| 1571 |
va_end (args); |
| 1572 |
return res; |
| 1573 |
} |
| 1574 |
|
| 1575 |
/*! |
| 1576 |
* \brief This is the `va_list` version of wp_spa_json_parser_get() |
| 1577 |
* |
| 1578 |
* \ingroup wpspajson |
| 1579 |
* \param self the spa json parser object |
| 1580 |
* \param args the variable arguments passed to wp_spa_json_parser_get() |
| 1581 |
* \returns TRUE if the value was obtained, FALSE otherwise |
| 1582 |
*/ |
| 1583 |
gboolean |
| 1584 |
wp_spa_json_parser_get_valist (WpSpaJsonParser *self, va_list args) |
| 1585 |
{ |
| 1586 |
do { |
| 1587 |
const char *format = NULL; |
| 1588 |
|
| 1589 |
/* parse property key if object */ |
| 1590 |
if (self->json->data[0] == '{') { |
| 1591 |
gchar **key = va_arg(args, gchar **); |
| 1592 |
if (!key) |
| 1593 |
return TRUE; |
| 1594 |
*key = wp_spa_json_parser_get_string (self); |
| 1595 |
if (!(*key)) |
| 1596 |
return FALSE; |
| 1597 |
} |
| 1598 |
|
| 1599 |
/* parse format */ |
| 1600 |
format = va_arg(args, const gchar *); |
| 1601 |
if (!format) |
| 1602 |
return TRUE; |
| 1603 |
|
| 1604 |
/* advance */ |
| 1605 |
if (!wp_spa_json_parser_advance (self)) |
| 1606 |
return FALSE; |
| 1607 |
|
| 1608 |
/* parse value */ |
| 1609 |
wp_spa_json_parse_value (self->curr.cur, self->curr.end - self->curr.cur, |
| 1610 |
format, args); |
| 1611 |
} while (TRUE); |
| 1612 |
|
| 1613 |
return FALSE; |
| 1614 |
} |
| 1615 |
|
| 1616 |
void |
| 1617 |
wp_spa_json_parser_end (WpSpaJsonParser *self) |
| 1618 |
{ |
| 1619 |
self->pos = NULL; |
| 1620 |
} |
| 1621 |
|
| 1622 |
struct _WpSpaJsonIterator |
| 1623 |
{ |
| 1624 |
WpSpaJson *json; |
| 1625 |
WpSpaJsonParser *parser; |
| 1626 |
}; |
| 1627 |
typedef struct _WpSpaJsonIterator WpSpaJsonIterator; |
| 1628 |
|
| 1629 |
static void |
| 1630 |
wp_spa_json_iterator_reset (WpIterator *iterator) |
| 1631 |
{ |
| 1632 |
WpSpaJsonIterator *self = wp_iterator_get_user_data (iterator); |
| 1633 |
g_clear_pointer (&self->parser, wp_spa_json_parser_unref); |
| 1634 |
} |
| 1635 |
|
| 1636 |
static gboolean |
| 1637 |
wp_spa_json_iterator_next (WpIterator *iterator, GValue *item) |
| 1638 |
{ |
| 1639 |
WpSpaJsonIterator *self = wp_iterator_get_user_data (iterator); |
| 1640 |
|
| 1641 |
/* init iterator if first time */ |
| 1642 |
if (!self->parser) { |
| 1643 |
switch (self->json->json->cur[0]) { |
| 1644 |
case '[': /* array */ |
| 1645 |
self->parser = wp_spa_json_parser_new_array (self->json); |
| 1646 |
break; |
| 1647 |
case '{': /* object */ |
| 1648 |
self->parser = wp_spa_json_parser_new_object (self->json); |
| 1649 |
break; |
| 1650 |
default: |
| 1651 |
return FALSE; |
| 1652 |
} |
| 1653 |
} |
| 1654 |
|
| 1655 |
/* advance */ |
| 1656 |
if (!wp_spa_json_parser_advance (self->parser)) |
| 1657 |
return FALSE; |
| 1658 |
|
| 1659 |
if (item) { |
| 1660 |
g_value_init (item, WP_TYPE_SPA_JSON); |
| 1661 |
g_value_take_boxed (item, wp_spa_json_new_wrap (&self->parser->curr)); |
| 1662 |
} |
| 1663 |
|
| 1664 |
return TRUE; |
| 1665 |
} |
| 1666 |
|
| 1667 |
static void |
| 1668 |
wp_spa_json_iterator_finalize (WpIterator *iterator) |
| 1669 |
{ |
| 1670 |
WpSpaJsonIterator *self = wp_iterator_get_user_data (iterator); |
| 1671 |
g_clear_pointer (&self->parser, wp_spa_json_parser_unref); |
| 1672 |
g_clear_pointer (&self->json, wp_spa_json_unref); |
| 1673 |
} |
| 1674 |
|
| 1675 |
/*! |
| 1676 |
* \brief Creates a new iterator for a spa json object. |
| 1677 |
* |
| 1678 |
* \ingroup wpspajson |
| 1679 |
* \param self the spa json object |
| 1680 |
* \returns (transfer full): the new spa json iterator |
| 1681 |
*/ |
| 1682 |
WpIterator * |
| 1683 |
wp_spa_json_new_iterator (WpSpaJson *self) |
| 1684 |
{ |
| 1685 |
static const WpIteratorMethods methods = { |
| 1686 |
.version = WP_ITERATOR_METHODS_VERSION, |
| 1687 |
.reset = wp_spa_json_iterator_reset, |
| 1688 |
.next = wp_spa_json_iterator_next, |
| 1689 |
.fold = NULL, |
| 1690 |
.foreach = NULL, |
| 1691 |
.finalize = wp_spa_json_iterator_finalize |
| 1692 |
}; |
| 1693 |
WpIterator *it = wp_iterator_new (&methods, sizeof (WpSpaJsonIterator)); |
| 1694 |
WpSpaJsonIterator *jit = wp_iterator_get_user_data (it); |
| 1695 |
|
| 1696 |
jit->json = wp_spa_json_ref (self); |
| 1697 |
jit->parser = NULL; |
| 1698 |
|
| 1699 |
return it; |
| 1700 |
} |