| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
| 3 |
|
|
* Michael Clark <michael@metaparadigm.com> |
| 4 |
|
|
* Copyright (c) 2009 Hewlett-Packard Development Company, L.P. |
| 5 |
|
|
* |
| 6 |
|
|
* This library is free software; you can redistribute it and/or modify |
| 7 |
|
|
* it under the terms of the MIT license. See COPYING for details. |
| 8 |
|
|
* |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
#include "config.h" |
| 12 |
|
|
|
| 13 |
|
|
#include "strerror_override.h" |
| 14 |
|
|
|
| 15 |
|
|
#include <assert.h> |
| 16 |
|
|
#ifdef HAVE_LIMITS_H |
| 17 |
|
|
#include <limits.h> |
| 18 |
|
|
#endif |
| 19 |
|
|
#include <math.h> |
| 20 |
|
|
#include <stddef.h> |
| 21 |
|
|
#include <stdio.h> |
| 22 |
|
|
#include <stdlib.h> |
| 23 |
|
|
#include <string.h> |
| 24 |
|
|
|
| 25 |
|
|
#include "arraylist.h" |
| 26 |
|
|
#include "debug.h" |
| 27 |
|
|
#include "json_inttypes.h" |
| 28 |
|
|
#include "json_object.h" |
| 29 |
|
|
#include "json_object_private.h" |
| 30 |
|
|
#include "json_util.h" |
| 31 |
|
|
#include "linkhash.h" |
| 32 |
|
|
#include "math_compat.h" |
| 33 |
|
|
#include "printbuf.h" |
| 34 |
|
|
#include "snprintf_compat.h" |
| 35 |
|
|
#include "strdup_compat.h" |
| 36 |
|
|
|
| 37 |
|
|
/* Avoid ctype.h and locale overhead */ |
| 38 |
|
|
#define is_plain_digit(c) ((c) >= '0' && (c) <= '9') |
| 39 |
|
|
|
| 40 |
|
|
#if SIZEOF_LONG_LONG != SIZEOF_INT64_T |
| 41 |
|
|
#error The long long type is not 64-bits |
| 42 |
|
|
#endif |
| 43 |
|
|
|
| 44 |
|
|
#ifndef SSIZE_T_MAX |
| 45 |
|
|
#if SIZEOF_SSIZE_T == SIZEOF_INT |
| 46 |
|
|
#define SSIZE_T_MAX INT_MAX |
| 47 |
|
|
#elif SIZEOF_SSIZE_T == SIZEOF_LONG |
| 48 |
|
|
#define SSIZE_T_MAX LONG_MAX |
| 49 |
|
|
#elif SIZEOF_SSIZE_T == SIZEOF_LONG_LONG |
| 50 |
|
|
#define SSIZE_T_MAX LLONG_MAX |
| 51 |
|
|
#else |
| 52 |
|
|
#error Unable to determine size of ssize_t |
| 53 |
|
|
#endif |
| 54 |
|
|
#endif |
| 55 |
|
|
|
| 56 |
|
|
const char *json_hex_chars = "0123456789abcdefABCDEF"; |
| 57 |
|
|
|
| 58 |
|
|
static void json_object_generic_delete(struct json_object *jso); |
| 59 |
|
|
|
| 60 |
|
|
#if defined(_MSC_VER) && (_MSC_VER <= 1800) |
| 61 |
|
|
/* VS2013 doesn't know about "inline" */ |
| 62 |
|
|
#define inline __inline |
| 63 |
|
|
#elif defined(AIX_CC) |
| 64 |
|
|
#define inline |
| 65 |
|
|
#endif |
| 66 |
|
|
|
| 67 |
|
|
/* |
| 68 |
|
|
* Helper functions to more safely cast to a particular type of json_object |
| 69 |
|
|
*/ |
| 70 |
|
|
static inline struct json_object_object *JC_OBJECT(struct json_object *jso) |
| 71 |
|
|
{ |
| 72 |
|
|
return (void *)jso; |
| 73 |
|
|
} |
| 74 |
|
|
static inline const struct json_object_object *JC_OBJECT_C(const struct json_object *jso) |
| 75 |
|
|
{ |
| 76 |
|
|
return (const void *)jso; |
| 77 |
|
|
} |
| 78 |
|
|
static inline struct json_object_array *JC_ARRAY(struct json_object *jso) |
| 79 |
|
|
{ |
| 80 |
|
|
return (void *)jso; |
| 81 |
|
|
} |
| 82 |
|
|
static inline const struct json_object_array *JC_ARRAY_C(const struct json_object *jso) |
| 83 |
|
|
{ |
| 84 |
|
|
return (const void *)jso; |
| 85 |
|
|
} |
| 86 |
|
|
static inline struct json_object_boolean *JC_BOOL(struct json_object *jso) |
| 87 |
|
|
{ |
| 88 |
|
|
return (void *)jso; |
| 89 |
|
|
} |
| 90 |
|
|
static inline const struct json_object_boolean *JC_BOOL_C(const struct json_object *jso) |
| 91 |
|
|
{ |
| 92 |
|
|
return (const void *)jso; |
| 93 |
|
|
} |
| 94 |
|
|
static inline struct json_object_double *JC_DOUBLE(struct json_object *jso) |
| 95 |
|
|
{ |
| 96 |
|
|
return (void *)jso; |
| 97 |
|
|
} |
| 98 |
|
|
static inline const struct json_object_double *JC_DOUBLE_C(const struct json_object *jso) |
| 99 |
|
|
{ |
| 100 |
|
|
return (const void *)jso; |
| 101 |
|
|
} |
| 102 |
|
|
static inline struct json_object_int *JC_INT(struct json_object *jso) |
| 103 |
|
|
{ |
| 104 |
|
|
return (void *)jso; |
| 105 |
|
|
} |
| 106 |
|
|
static inline const struct json_object_int *JC_INT_C(const struct json_object *jso) |
| 107 |
|
|
{ |
| 108 |
|
|
return (const void *)jso; |
| 109 |
|
|
} |
| 110 |
|
|
static inline struct json_object_string *JC_STRING(struct json_object *jso) |
| 111 |
|
|
{ |
| 112 |
|
|
return (void *)jso; |
| 113 |
|
|
} |
| 114 |
|
|
static inline const struct json_object_string *JC_STRING_C(const struct json_object *jso) |
| 115 |
|
|
{ |
| 116 |
|
|
return (const void *)jso; |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
#define JC_CONCAT(a, b) a##b |
| 120 |
|
|
#define JC_CONCAT3(a, b, c) a##b##c |
| 121 |
|
|
|
| 122 |
|
|
#define JSON_OBJECT_NEW(jtype) \ |
| 123 |
|
|
(struct JC_CONCAT(json_object_, jtype) *)json_object_new( \ |
| 124 |
|
|
JC_CONCAT(json_type_, jtype), sizeof(struct JC_CONCAT(json_object_, jtype)), \ |
| 125 |
|
|
&JC_CONCAT3(json_object_, jtype, _to_json_string)) |
| 126 |
|
|
|
| 127 |
|
|
static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size, |
| 128 |
|
|
json_object_to_json_string_fn *to_json_string); |
| 129 |
|
|
|
| 130 |
|
|
static void json_object_object_delete(struct json_object *jso_base); |
| 131 |
|
|
static void json_object_string_delete(struct json_object *jso); |
| 132 |
|
|
static void json_object_array_delete(struct json_object *jso); |
| 133 |
|
|
|
| 134 |
|
|
static json_object_to_json_string_fn json_object_object_to_json_string; |
| 135 |
|
|
static json_object_to_json_string_fn json_object_boolean_to_json_string; |
| 136 |
|
|
static json_object_to_json_string_fn json_object_double_to_json_string_default; |
| 137 |
|
|
static json_object_to_json_string_fn json_object_int_to_json_string; |
| 138 |
|
|
static json_object_to_json_string_fn json_object_string_to_json_string; |
| 139 |
|
|
static json_object_to_json_string_fn json_object_array_to_json_string; |
| 140 |
|
|
static json_object_to_json_string_fn _json_object_userdata_to_json_string; |
| 141 |
|
|
|
| 142 |
|
|
#ifndef JSON_NORETURN |
| 143 |
|
|
#if defined(_MSC_VER) |
| 144 |
|
|
#define JSON_NORETURN __declspec(noreturn) |
| 145 |
|
|
#elif defined(__OS400__) |
| 146 |
|
|
#define JSON_NORETURN |
| 147 |
|
|
#else |
| 148 |
|
|
/* 'cold' attribute is for optimization, telling the computer this code |
| 149 |
|
|
* path is unlikely. |
| 150 |
|
|
*/ |
| 151 |
|
|
#define JSON_NORETURN __attribute__((noreturn, cold)) |
| 152 |
|
|
#endif |
| 153 |
|
|
#endif |
| 154 |
|
|
/** |
| 155 |
|
|
* Abort and optionally print a message on standard error. |
| 156 |
|
|
* This should be used rather than assert() for unconditional abortion |
| 157 |
|
|
* (in particular for code paths which are never supposed to be run). |
| 158 |
|
|
* */ |
| 159 |
|
|
JSON_NORETURN static void json_abort(const char *message); |
| 160 |
|
|
|
| 161 |
|
|
/* helper for accessing the optimized string data component in json_object |
| 162 |
|
|
*/ |
| 163 |
|
|
static inline char *get_string_component_mutable(struct json_object *jso) |
| 164 |
|
|
{ |
| 165 |
|
✗ |
if (JC_STRING_C(jso)->len < 0) |
| 166 |
|
|
{ |
| 167 |
|
|
/* Due to json_object_set_string(), we might have a pointer */ |
| 168 |
|
✗ |
return JC_STRING(jso)->c_string.pdata; |
| 169 |
|
|
} |
| 170 |
|
✗ |
return JC_STRING(jso)->c_string.idata; |
| 171 |
|
|
} |
| 172 |
|
|
static inline const char *get_string_component(const struct json_object *jso) |
| 173 |
|
|
{ |
| 174 |
|
|
return get_string_component_mutable((void *)(uintptr_t)(const void *)jso); |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
/* string escaping */ |
| 178 |
|
|
|
| 179 |
|
✗ |
static int json_escape_str(struct printbuf *pb, const char *str, size_t len, int flags) |
| 180 |
|
|
{ |
| 181 |
|
|
size_t pos = 0, start_offset = 0; |
| 182 |
|
|
unsigned char c; |
| 183 |
|
✗ |
while (len) |
| 184 |
|
|
{ |
| 185 |
|
✗ |
--len; |
| 186 |
|
✗ |
c = str[pos]; |
| 187 |
|
✗ |
switch (c) |
| 188 |
|
|
{ |
| 189 |
|
✗ |
case '\b': |
| 190 |
|
|
case '\n': |
| 191 |
|
|
case '\r': |
| 192 |
|
|
case '\t': |
| 193 |
|
|
case '\f': |
| 194 |
|
|
case '"': |
| 195 |
|
|
case '\\': |
| 196 |
|
|
case '/': |
| 197 |
|
✗ |
if ((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/') |
| 198 |
|
|
{ |
| 199 |
|
✗ |
pos++; |
| 200 |
|
✗ |
break; |
| 201 |
|
|
} |
| 202 |
|
|
|
| 203 |
|
✗ |
if (pos > start_offset) |
| 204 |
|
✗ |
printbuf_memappend(pb, str + start_offset, pos - start_offset); |
| 205 |
|
|
|
| 206 |
|
✗ |
if (c == '\b') |
| 207 |
|
✗ |
printbuf_memappend(pb, "\\b", 2); |
| 208 |
|
|
else if (c == '\n') |
| 209 |
|
✗ |
printbuf_memappend(pb, "\\n", 2); |
| 210 |
|
|
else if (c == '\r') |
| 211 |
|
✗ |
printbuf_memappend(pb, "\\r", 2); |
| 212 |
|
|
else if (c == '\t') |
| 213 |
|
✗ |
printbuf_memappend(pb, "\\t", 2); |
| 214 |
|
|
else if (c == '\f') |
| 215 |
|
✗ |
printbuf_memappend(pb, "\\f", 2); |
| 216 |
|
|
else if (c == '"') |
| 217 |
|
✗ |
printbuf_memappend(pb, "\\\"", 2); |
| 218 |
|
|
else if (c == '\\') |
| 219 |
|
✗ |
printbuf_memappend(pb, "\\\\", 2); |
| 220 |
|
|
else if (c == '/') |
| 221 |
|
✗ |
printbuf_memappend(pb, "\\/", 2); |
| 222 |
|
|
|
| 223 |
|
✗ |
start_offset = ++pos; |
| 224 |
|
✗ |
break; |
| 225 |
|
✗ |
default: |
| 226 |
|
✗ |
if (c < ' ') |
| 227 |
|
|
{ |
| 228 |
|
|
char sbuf[7]; |
| 229 |
|
✗ |
if (pos > start_offset) |
| 230 |
|
✗ |
printbuf_memappend(pb, str + start_offset, |
| 231 |
|
✗ |
pos - start_offset); |
| 232 |
|
✗ |
snprintf(sbuf, sizeof(sbuf), "\\u00%c%c", json_hex_chars[c >> 4], |
| 233 |
|
✗ |
json_hex_chars[c & 0xf]); |
| 234 |
|
✗ |
printbuf_memappend_fast(pb, sbuf, (int)sizeof(sbuf) - 1); |
| 235 |
|
✗ |
start_offset = ++pos; |
| 236 |
|
|
} |
| 237 |
|
|
else |
| 238 |
|
✗ |
pos++; |
| 239 |
|
|
} |
| 240 |
|
|
} |
| 241 |
|
✗ |
if (pos > start_offset) |
| 242 |
|
✗ |
printbuf_memappend(pb, str + start_offset, pos - start_offset); |
| 243 |
|
✗ |
return 0; |
| 244 |
|
|
} |
| 245 |
|
|
|
| 246 |
|
|
/* reference counting */ |
| 247 |
|
|
|
| 248 |
|
✗ |
struct json_object *json_object_get(struct json_object *jso) |
| 249 |
|
|
{ |
| 250 |
|
✗ |
if (!jso) |
| 251 |
|
|
return jso; |
| 252 |
|
|
|
| 253 |
|
|
// Don't overflow the refcounter. |
| 254 |
|
|
assert(jso->_ref_count < UINT32_MAX); |
| 255 |
|
|
|
| 256 |
|
|
#if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING) |
| 257 |
|
|
__sync_add_and_fetch(&jso->_ref_count, 1); |
| 258 |
|
|
#else |
| 259 |
|
✗ |
++jso->_ref_count; |
| 260 |
|
|
#endif |
| 261 |
|
|
|
| 262 |
|
✗ |
return jso; |
| 263 |
|
|
} |
| 264 |
|
|
|
| 265 |
|
✗ |
int json_object_put(struct json_object *jso) |
| 266 |
|
|
{ |
| 267 |
|
✗ |
if (!jso) |
| 268 |
|
|
return 0; |
| 269 |
|
|
|
| 270 |
|
|
/* Avoid invalid free and crash explicitly instead of (silently) |
| 271 |
|
|
* segfaulting. |
| 272 |
|
|
*/ |
| 273 |
|
|
assert(jso->_ref_count > 0); |
| 274 |
|
|
|
| 275 |
|
|
#if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING) |
| 276 |
|
|
/* Note: this only allow the refcount to remain correct |
| 277 |
|
|
* when multiple threads are adjusting it. It is still an error |
| 278 |
|
|
* for a thread to decrement the refcount if it doesn't "own" it, |
| 279 |
|
|
* as that can result in the thread that loses the race to 0 |
| 280 |
|
|
* operating on an already-freed object. |
| 281 |
|
|
*/ |
| 282 |
|
|
if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) |
| 283 |
|
|
return 0; |
| 284 |
|
|
#else |
| 285 |
|
✗ |
if (--jso->_ref_count > 0) |
| 286 |
|
|
return 0; |
| 287 |
|
|
#endif |
| 288 |
|
|
|
| 289 |
|
✗ |
if (jso->_user_delete) |
| 290 |
|
✗ |
jso->_user_delete(jso, jso->_userdata); |
| 291 |
|
✗ |
switch (jso->o_type) |
| 292 |
|
|
{ |
| 293 |
|
✗ |
case json_type_object: json_object_object_delete(jso); break; |
| 294 |
|
✗ |
case json_type_array: json_object_array_delete(jso); break; |
| 295 |
|
✗ |
case json_type_string: json_object_string_delete(jso); break; |
| 296 |
|
✗ |
default: json_object_generic_delete(jso); break; |
| 297 |
|
|
} |
| 298 |
|
|
return 1; |
| 299 |
|
|
} |
| 300 |
|
|
|
| 301 |
|
|
/* generic object construction and destruction parts */ |
| 302 |
|
|
|
| 303 |
|
✗ |
static void json_object_generic_delete(struct json_object *jso) |
| 304 |
|
|
{ |
| 305 |
|
✗ |
printbuf_free(jso->_pb); |
| 306 |
|
✗ |
free(jso); |
| 307 |
|
|
} |
| 308 |
|
|
|
| 309 |
|
✗ |
static inline struct json_object *json_object_new(enum json_type o_type, size_t alloc_size, |
| 310 |
|
|
json_object_to_json_string_fn *to_json_string) |
| 311 |
|
|
{ |
| 312 |
|
|
struct json_object *jso; |
| 313 |
|
|
|
| 314 |
|
✗ |
jso = (struct json_object *)malloc(alloc_size); |
| 315 |
|
✗ |
if (!jso) |
| 316 |
|
|
return NULL; |
| 317 |
|
|
|
| 318 |
|
✗ |
jso->o_type = o_type; |
| 319 |
|
✗ |
jso->_ref_count = 1; |
| 320 |
|
✗ |
jso->_to_json_string = to_json_string; |
| 321 |
|
✗ |
jso->_pb = NULL; |
| 322 |
|
✗ |
jso->_user_delete = NULL; |
| 323 |
|
✗ |
jso->_userdata = NULL; |
| 324 |
|
|
//jso->... // Type-specific fields must be set by caller |
| 325 |
|
|
|
| 326 |
|
✗ |
return jso; |
| 327 |
|
|
} |
| 328 |
|
|
|
| 329 |
|
|
/* type checking functions */ |
| 330 |
|
|
|
| 331 |
|
✗ |
int json_object_is_type(const struct json_object *jso, enum json_type type) |
| 332 |
|
|
{ |
| 333 |
|
✗ |
if (!jso) |
| 334 |
|
✗ |
return (type == json_type_null); |
| 335 |
|
✗ |
return (jso->o_type == type); |
| 336 |
|
|
} |
| 337 |
|
|
|
| 338 |
|
✗ |
enum json_type json_object_get_type(const struct json_object *jso) |
| 339 |
|
|
{ |
| 340 |
|
✗ |
if (!jso) |
| 341 |
|
|
return json_type_null; |
| 342 |
|
✗ |
return jso->o_type; |
| 343 |
|
|
} |
| 344 |
|
|
|
| 345 |
|
✗ |
void *json_object_get_userdata(json_object *jso) |
| 346 |
|
|
{ |
| 347 |
|
✗ |
return jso ? jso->_userdata : NULL; |
| 348 |
|
|
} |
| 349 |
|
|
|
| 350 |
|
✗ |
void json_object_set_userdata(json_object *jso, void *userdata, json_object_delete_fn *user_delete) |
| 351 |
|
|
{ |
| 352 |
|
|
// Can't return failure, so abort if we can't perform the operation. |
| 353 |
|
|
assert(jso != NULL); |
| 354 |
|
|
|
| 355 |
|
|
// First, clean up any previously existing user info |
| 356 |
|
✗ |
if (jso->_user_delete) |
| 357 |
|
✗ |
jso->_user_delete(jso, jso->_userdata); |
| 358 |
|
|
|
| 359 |
|
✗ |
jso->_userdata = userdata; |
| 360 |
|
✗ |
jso->_user_delete = user_delete; |
| 361 |
|
|
} |
| 362 |
|
|
|
| 363 |
|
|
/* set a custom conversion to string */ |
| 364 |
|
|
|
| 365 |
|
✗ |
void json_object_set_serializer(json_object *jso, json_object_to_json_string_fn *to_string_func, |
| 366 |
|
|
void *userdata, json_object_delete_fn *user_delete) |
| 367 |
|
|
{ |
| 368 |
|
✗ |
json_object_set_userdata(jso, userdata, user_delete); |
| 369 |
|
|
|
| 370 |
|
✗ |
if (to_string_func == NULL) |
| 371 |
|
|
{ |
| 372 |
|
|
// Reset to the standard serialization function |
| 373 |
|
✗ |
switch (jso->o_type) |
| 374 |
|
|
{ |
| 375 |
|
✗ |
case json_type_null: jso->_to_json_string = NULL; break; |
| 376 |
|
✗ |
case json_type_boolean: |
| 377 |
|
✗ |
jso->_to_json_string = &json_object_boolean_to_json_string; |
| 378 |
|
✗ |
break; |
| 379 |
|
✗ |
case json_type_double: |
| 380 |
|
✗ |
jso->_to_json_string = &json_object_double_to_json_string_default; |
| 381 |
|
✗ |
break; |
| 382 |
|
✗ |
case json_type_int: jso->_to_json_string = &json_object_int_to_json_string; break; |
| 383 |
|
✗ |
case json_type_object: |
| 384 |
|
✗ |
jso->_to_json_string = &json_object_object_to_json_string; |
| 385 |
|
✗ |
break; |
| 386 |
|
✗ |
case json_type_array: |
| 387 |
|
✗ |
jso->_to_json_string = &json_object_array_to_json_string; |
| 388 |
|
✗ |
break; |
| 389 |
|
✗ |
case json_type_string: |
| 390 |
|
✗ |
jso->_to_json_string = &json_object_string_to_json_string; |
| 391 |
|
✗ |
break; |
| 392 |
|
|
} |
| 393 |
|
✗ |
return; |
| 394 |
|
|
} |
| 395 |
|
|
|
| 396 |
|
✗ |
jso->_to_json_string = to_string_func; |
| 397 |
|
|
} |
| 398 |
|
|
|
| 399 |
|
|
/* extended conversion to string */ |
| 400 |
|
|
|
| 401 |
|
✗ |
const char *json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length) |
| 402 |
|
|
{ |
| 403 |
|
|
const char *r = NULL; |
| 404 |
|
|
size_t s = 0; |
| 405 |
|
|
|
| 406 |
|
✗ |
if (!jso) |
| 407 |
|
|
{ |
| 408 |
|
|
s = 4; |
| 409 |
|
|
r = "null"; |
| 410 |
|
|
} |
| 411 |
|
✗ |
else if ((jso->_pb) || (jso->_pb = printbuf_new())) |
| 412 |
|
|
{ |
| 413 |
|
✗ |
printbuf_reset(jso->_pb); |
| 414 |
|
|
|
| 415 |
|
✗ |
if (jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0) |
| 416 |
|
|
{ |
| 417 |
|
✗ |
s = (size_t)jso->_pb->bpos; |
| 418 |
|
✗ |
r = jso->_pb->buf; |
| 419 |
|
|
} |
| 420 |
|
|
} |
| 421 |
|
|
|
| 422 |
|
✗ |
if (length) |
| 423 |
|
✗ |
*length = s; |
| 424 |
|
✗ |
return r; |
| 425 |
|
|
} |
| 426 |
|
|
|
| 427 |
|
✗ |
const char *json_object_to_json_string_ext(struct json_object *jso, int flags) |
| 428 |
|
|
{ |
| 429 |
|
✗ |
return json_object_to_json_string_length(jso, flags, NULL); |
| 430 |
|
|
} |
| 431 |
|
|
|
| 432 |
|
|
/* backwards-compatible conversion to string */ |
| 433 |
|
|
|
| 434 |
|
✗ |
const char *json_object_to_json_string(struct json_object *jso) |
| 435 |
|
|
{ |
| 436 |
|
✗ |
return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED); |
| 437 |
|
|
} |
| 438 |
|
|
|
| 439 |
|
✗ |
static void indent(struct printbuf *pb, int level, int flags) |
| 440 |
|
|
{ |
| 441 |
|
✗ |
if (flags & JSON_C_TO_STRING_PRETTY) |
| 442 |
|
|
{ |
| 443 |
|
✗ |
if (flags & JSON_C_TO_STRING_PRETTY_TAB) |
| 444 |
|
|
{ |
| 445 |
|
✗ |
printbuf_memset(pb, -1, '\t', level); |
| 446 |
|
|
} |
| 447 |
|
|
else |
| 448 |
|
|
{ |
| 449 |
|
✗ |
printbuf_memset(pb, -1, ' ', level * 2); |
| 450 |
|
|
} |
| 451 |
|
|
} |
| 452 |
|
|
} |
| 453 |
|
|
|
| 454 |
|
|
/* json_object_object */ |
| 455 |
|
|
|
| 456 |
|
✗ |
static int json_object_object_to_json_string(struct json_object *jso, struct printbuf *pb, |
| 457 |
|
|
int level, int flags) |
| 458 |
|
|
{ |
| 459 |
|
|
int had_children = 0; |
| 460 |
|
|
struct json_object_iter iter; |
| 461 |
|
|
|
| 462 |
|
✗ |
printbuf_strappend(pb, "{" /*}*/); |
| 463 |
|
✗ |
json_object_object_foreachC(jso, iter) |
| 464 |
|
|
{ |
| 465 |
|
✗ |
if (had_children) |
| 466 |
|
|
{ |
| 467 |
|
✗ |
printbuf_strappend(pb, ","); |
| 468 |
|
|
} |
| 469 |
|
✗ |
if (flags & JSON_C_TO_STRING_PRETTY) |
| 470 |
|
✗ |
printbuf_strappend(pb, "\n"); |
| 471 |
|
|
had_children = 1; |
| 472 |
|
✗ |
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY)) |
| 473 |
|
✗ |
printbuf_strappend(pb, " "); |
| 474 |
|
✗ |
indent(pb, level + 1, flags); |
| 475 |
|
✗ |
printbuf_strappend(pb, "\""); |
| 476 |
|
✗ |
json_escape_str(pb, iter.key, strlen(iter.key), flags); |
| 477 |
|
✗ |
if (flags & JSON_C_TO_STRING_SPACED) |
| 478 |
|
✗ |
printbuf_strappend(pb, "\": "); |
| 479 |
|
|
else |
| 480 |
|
✗ |
printbuf_strappend(pb, "\":"); |
| 481 |
|
✗ |
if (iter.val == NULL) |
| 482 |
|
✗ |
printbuf_strappend(pb, "null"); |
| 483 |
|
✗ |
else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0) |
| 484 |
|
|
return -1; |
| 485 |
|
|
} |
| 486 |
|
✗ |
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children) |
| 487 |
|
|
{ |
| 488 |
|
✗ |
printbuf_strappend(pb, "\n"); |
| 489 |
|
✗ |
indent(pb, level, flags); |
| 490 |
|
|
} |
| 491 |
|
✗ |
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY)) |
| 492 |
|
✗ |
return printbuf_strappend(pb, /*{*/ " }"); |
| 493 |
|
|
else |
| 494 |
|
✗ |
return printbuf_strappend(pb, /*{*/ "}"); |
| 495 |
|
|
} |
| 496 |
|
|
|
| 497 |
|
✗ |
static void json_object_lh_entry_free(struct lh_entry *ent) |
| 498 |
|
|
{ |
| 499 |
|
✗ |
if (!lh_entry_k_is_constant(ent)) |
| 500 |
|
✗ |
free(lh_entry_k(ent)); |
| 501 |
|
✗ |
json_object_put((struct json_object *)lh_entry_v(ent)); |
| 502 |
|
|
} |
| 503 |
|
|
|
| 504 |
|
✗ |
static void json_object_object_delete(struct json_object *jso_base) |
| 505 |
|
|
{ |
| 506 |
|
✗ |
lh_table_free(JC_OBJECT(jso_base)->c_object); |
| 507 |
|
✗ |
json_object_generic_delete(jso_base); |
| 508 |
|
|
} |
| 509 |
|
|
|
| 510 |
|
✗ |
struct json_object *json_object_new_object(void) |
| 511 |
|
|
{ |
| 512 |
|
✗ |
struct json_object_object *jso = JSON_OBJECT_NEW(object); |
| 513 |
|
✗ |
if (!jso) |
| 514 |
|
|
return NULL; |
| 515 |
|
✗ |
jso->c_object = |
| 516 |
|
✗ |
lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free); |
| 517 |
|
✗ |
if (!jso->c_object) |
| 518 |
|
|
{ |
| 519 |
|
✗ |
json_object_generic_delete(&jso->base); |
| 520 |
|
✗ |
errno = ENOMEM; |
| 521 |
|
✗ |
return NULL; |
| 522 |
|
|
} |
| 523 |
|
✗ |
return &jso->base; |
| 524 |
|
|
} |
| 525 |
|
|
|
| 526 |
|
✗ |
struct lh_table *json_object_get_object(const struct json_object *jso) |
| 527 |
|
|
{ |
| 528 |
|
✗ |
if (!jso) |
| 529 |
|
|
return NULL; |
| 530 |
|
✗ |
switch (jso->o_type) |
| 531 |
|
|
{ |
| 532 |
|
✗ |
case json_type_object: return JC_OBJECT_C(jso)->c_object; |
| 533 |
|
|
default: return NULL; |
| 534 |
|
|
} |
| 535 |
|
|
} |
| 536 |
|
|
|
| 537 |
|
✗ |
int json_object_object_add_ex(struct json_object *jso, const char *const key, |
| 538 |
|
|
struct json_object *const val, const unsigned opts) |
| 539 |
|
|
{ |
| 540 |
|
|
struct json_object *existing_value = NULL; |
| 541 |
|
|
struct lh_entry *existing_entry; |
| 542 |
|
|
unsigned long hash; |
| 543 |
|
|
|
| 544 |
|
|
assert(json_object_get_type(jso) == json_type_object); |
| 545 |
|
|
|
| 546 |
|
|
// We lookup the entry and replace the value, rather than just deleting |
| 547 |
|
|
// and re-adding it, so the existing key remains valid. |
| 548 |
|
✗ |
hash = lh_get_hash(JC_OBJECT(jso)->c_object, (const void *)key); |
| 549 |
|
|
existing_entry = |
| 550 |
|
✗ |
(opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) |
| 551 |
|
|
? NULL |
| 552 |
|
✗ |
: lh_table_lookup_entry_w_hash(JC_OBJECT(jso)->c_object, (const void *)key, hash); |
| 553 |
|
|
|
| 554 |
|
|
// The caller must avoid creating loops in the object tree, but do a |
| 555 |
|
|
// quick check anyway to make sure we're not creating a trivial loop. |
| 556 |
|
✗ |
if (jso == val) |
| 557 |
|
|
return -1; |
| 558 |
|
|
|
| 559 |
|
✗ |
if (!existing_entry) |
| 560 |
|
|
{ |
| 561 |
|
|
const void *const k = |
| 562 |
|
✗ |
(opts & JSON_C_OBJECT_ADD_CONSTANT_KEY) ? (const void *)key : strdup(key); |
| 563 |
|
✗ |
if (k == NULL) |
| 564 |
|
|
return -1; |
| 565 |
|
✗ |
return lh_table_insert_w_hash(JC_OBJECT(jso)->c_object, k, val, hash, opts); |
| 566 |
|
|
} |
| 567 |
|
|
existing_value = (json_object *)lh_entry_v(existing_entry); |
| 568 |
|
✗ |
if (existing_value) |
| 569 |
|
✗ |
json_object_put(existing_value); |
| 570 |
|
|
lh_entry_set_val(existing_entry, val); |
| 571 |
|
✗ |
return 0; |
| 572 |
|
|
} |
| 573 |
|
|
|
| 574 |
|
✗ |
int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val) |
| 575 |
|
|
{ |
| 576 |
|
✗ |
return json_object_object_add_ex(jso, key, val, 0); |
| 577 |
|
|
} |
| 578 |
|
|
|
| 579 |
|
✗ |
int json_object_object_length(const struct json_object *jso) |
| 580 |
|
|
{ |
| 581 |
|
|
assert(json_object_get_type(jso) == json_type_object); |
| 582 |
|
✗ |
return lh_table_length(JC_OBJECT_C(jso)->c_object); |
| 583 |
|
|
} |
| 584 |
|
|
|
| 585 |
|
✗ |
size_t json_c_object_sizeof(void) |
| 586 |
|
|
{ |
| 587 |
|
✗ |
return sizeof(struct json_object); |
| 588 |
|
|
} |
| 589 |
|
|
|
| 590 |
|
✗ |
struct json_object *json_object_object_get(const struct json_object *jso, const char *key) |
| 591 |
|
|
{ |
| 592 |
|
✗ |
struct json_object *result = NULL; |
| 593 |
|
✗ |
json_object_object_get_ex(jso, key, &result); |
| 594 |
|
✗ |
return result; |
| 595 |
|
|
} |
| 596 |
|
|
|
| 597 |
|
✗ |
json_bool json_object_object_get_ex(const struct json_object *jso, const char *key, |
| 598 |
|
|
struct json_object **value) |
| 599 |
|
|
{ |
| 600 |
|
✗ |
if (value != NULL) |
| 601 |
|
✗ |
*value = NULL; |
| 602 |
|
|
|
| 603 |
|
✗ |
if (NULL == jso) |
| 604 |
|
|
return 0; |
| 605 |
|
|
|
| 606 |
|
✗ |
switch (jso->o_type) |
| 607 |
|
|
{ |
| 608 |
|
|
case json_type_object: |
| 609 |
|
✗ |
return lh_table_lookup_ex(JC_OBJECT_C(jso)->c_object, (const void *)key, |
| 610 |
|
|
(void **)value); |
| 611 |
|
✗ |
default: |
| 612 |
|
✗ |
if (value != NULL) |
| 613 |
|
✗ |
*value = NULL; |
| 614 |
|
|
return 0; |
| 615 |
|
|
} |
| 616 |
|
|
} |
| 617 |
|
|
|
| 618 |
|
✗ |
void json_object_object_del(struct json_object *jso, const char *key) |
| 619 |
|
|
{ |
| 620 |
|
|
assert(json_object_get_type(jso) == json_type_object); |
| 621 |
|
✗ |
lh_table_delete(JC_OBJECT(jso)->c_object, key); |
| 622 |
|
|
} |
| 623 |
|
|
|
| 624 |
|
|
/* json_object_boolean */ |
| 625 |
|
|
|
| 626 |
|
✗ |
static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb, |
| 627 |
|
|
int level, int flags) |
| 628 |
|
|
{ |
| 629 |
|
✗ |
if (JC_BOOL(jso)->c_boolean) |
| 630 |
|
✗ |
return printbuf_strappend(pb, "true"); |
| 631 |
|
✗ |
return printbuf_strappend(pb, "false"); |
| 632 |
|
|
} |
| 633 |
|
|
|
| 634 |
|
✗ |
struct json_object *json_object_new_boolean(json_bool b) |
| 635 |
|
|
{ |
| 636 |
|
✗ |
struct json_object_boolean *jso = JSON_OBJECT_NEW(boolean); |
| 637 |
|
✗ |
if (!jso) |
| 638 |
|
|
return NULL; |
| 639 |
|
✗ |
jso->c_boolean = b; |
| 640 |
|
✗ |
return &jso->base; |
| 641 |
|
|
} |
| 642 |
|
|
|
| 643 |
|
✗ |
json_bool json_object_get_boolean(const struct json_object *jso) |
| 644 |
|
|
{ |
| 645 |
|
✗ |
if (!jso) |
| 646 |
|
|
return 0; |
| 647 |
|
✗ |
switch (jso->o_type) |
| 648 |
|
|
{ |
| 649 |
|
✗ |
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
| 650 |
|
|
case json_type_int: |
| 651 |
|
✗ |
switch (JC_INT_C(jso)->cint_type) |
| 652 |
|
|
{ |
| 653 |
|
✗ |
case json_object_int_type_int64: return (JC_INT_C(jso)->cint.c_int64 != 0); |
| 654 |
|
✗ |
case json_object_int_type_uint64: return (JC_INT_C(jso)->cint.c_uint64 != 0); |
| 655 |
|
✗ |
default: json_abort("invalid cint_type"); |
| 656 |
|
|
} |
| 657 |
|
✗ |
case json_type_double: return (JC_DOUBLE_C(jso)->c_double != 0); |
| 658 |
|
✗ |
case json_type_string: return (JC_STRING_C(jso)->len != 0); |
| 659 |
|
|
default: return 0; |
| 660 |
|
|
} |
| 661 |
|
|
} |
| 662 |
|
|
|
| 663 |
|
✗ |
int json_object_set_boolean(struct json_object *jso, json_bool new_value) |
| 664 |
|
|
{ |
| 665 |
|
✗ |
if (!jso || jso->o_type != json_type_boolean) |
| 666 |
|
|
return 0; |
| 667 |
|
✗ |
JC_BOOL(jso)->c_boolean = new_value; |
| 668 |
|
✗ |
return 1; |
| 669 |
|
|
} |
| 670 |
|
|
|
| 671 |
|
|
/* json_object_int */ |
| 672 |
|
|
|
| 673 |
|
✗ |
static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level, |
| 674 |
|
|
int flags) |
| 675 |
|
|
{ |
| 676 |
|
|
/* room for 19 digits, the sign char, and a null term */ |
| 677 |
|
|
char sbuf[21]; |
| 678 |
|
✗ |
if (JC_INT(jso)->cint_type == json_object_int_type_int64) |
| 679 |
|
✗ |
snprintf(sbuf, sizeof(sbuf), "%" PRId64, JC_INT(jso)->cint.c_int64); |
| 680 |
|
|
else |
| 681 |
|
✗ |
snprintf(sbuf, sizeof(sbuf), "%" PRIu64, JC_INT(jso)->cint.c_uint64); |
| 682 |
|
✗ |
return printbuf_memappend(pb, sbuf, strlen(sbuf)); |
| 683 |
|
|
} |
| 684 |
|
|
|
| 685 |
|
✗ |
struct json_object *json_object_new_int(int32_t i) |
| 686 |
|
|
{ |
| 687 |
|
✗ |
return json_object_new_int64(i); |
| 688 |
|
|
} |
| 689 |
|
|
|
| 690 |
|
✗ |
int32_t json_object_get_int(const struct json_object *jso) |
| 691 |
|
|
{ |
| 692 |
|
✗ |
int64_t cint64 = 0; |
| 693 |
|
|
double cdouble; |
| 694 |
|
|
enum json_type o_type; |
| 695 |
|
|
|
| 696 |
|
✗ |
if (!jso) |
| 697 |
|
|
return 0; |
| 698 |
|
|
|
| 699 |
|
✗ |
o_type = jso->o_type; |
| 700 |
|
✗ |
if (o_type == json_type_int) |
| 701 |
|
|
{ |
| 702 |
|
|
const struct json_object_int *jsoint = JC_INT_C(jso); |
| 703 |
|
✗ |
if (jsoint->cint_type == json_object_int_type_int64) |
| 704 |
|
|
{ |
| 705 |
|
✗ |
cint64 = jsoint->cint.c_int64; |
| 706 |
|
|
} |
| 707 |
|
|
else |
| 708 |
|
|
{ |
| 709 |
|
✗ |
if (jsoint->cint.c_uint64 >= INT64_MAX) |
| 710 |
|
✗ |
cint64 = INT64_MAX; |
| 711 |
|
|
else |
| 712 |
|
✗ |
cint64 = (int64_t)jsoint->cint.c_uint64; |
| 713 |
|
|
} |
| 714 |
|
|
} |
| 715 |
|
✗ |
else if (o_type == json_type_string) |
| 716 |
|
|
{ |
| 717 |
|
|
/* |
| 718 |
|
|
* Parse strings into 64-bit numbers, then use the |
| 719 |
|
|
* 64-to-32-bit number handling below. |
| 720 |
|
|
*/ |
| 721 |
|
✗ |
if (json_parse_int64(get_string_component(jso), &cint64) != 0) |
| 722 |
|
|
return 0; /* whoops, it didn't work. */ |
| 723 |
|
|
o_type = json_type_int; |
| 724 |
|
|
} |
| 725 |
|
|
|
| 726 |
|
✗ |
switch (o_type) |
| 727 |
|
|
{ |
| 728 |
|
✗ |
case json_type_int: |
| 729 |
|
|
/* Make sure we return the correct values for out of range numbers. */ |
| 730 |
|
✗ |
if (cint64 <= INT32_MIN) |
| 731 |
|
|
return INT32_MIN; |
| 732 |
|
✗ |
if (cint64 >= INT32_MAX) |
| 733 |
|
|
return INT32_MAX; |
| 734 |
|
✗ |
return (int32_t)cint64; |
| 735 |
|
|
case json_type_double: |
| 736 |
|
✗ |
cdouble = JC_DOUBLE_C(jso)->c_double; |
| 737 |
|
✗ |
if (cdouble <= INT32_MIN) |
| 738 |
|
|
return INT32_MIN; |
| 739 |
|
✗ |
if (cdouble >= INT32_MAX) |
| 740 |
|
|
return INT32_MAX; |
| 741 |
|
✗ |
return (int32_t)cdouble; |
| 742 |
|
✗ |
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
| 743 |
|
|
default: return 0; |
| 744 |
|
|
} |
| 745 |
|
|
} |
| 746 |
|
|
|
| 747 |
|
✗ |
int json_object_set_int(struct json_object *jso, int new_value) |
| 748 |
|
|
{ |
| 749 |
|
✗ |
return json_object_set_int64(jso, (int64_t)new_value); |
| 750 |
|
|
} |
| 751 |
|
|
|
| 752 |
|
✗ |
struct json_object *json_object_new_int64(int64_t i) |
| 753 |
|
|
{ |
| 754 |
|
✗ |
struct json_object_int *jso = JSON_OBJECT_NEW(int); |
| 755 |
|
✗ |
if (!jso) |
| 756 |
|
|
return NULL; |
| 757 |
|
✗ |
jso->cint.c_int64 = i; |
| 758 |
|
✗ |
jso->cint_type = json_object_int_type_int64; |
| 759 |
|
✗ |
return &jso->base; |
| 760 |
|
|
} |
| 761 |
|
|
|
| 762 |
|
✗ |
struct json_object *json_object_new_uint64(uint64_t i) |
| 763 |
|
|
{ |
| 764 |
|
✗ |
struct json_object_int *jso = JSON_OBJECT_NEW(int); |
| 765 |
|
✗ |
if (!jso) |
| 766 |
|
|
return NULL; |
| 767 |
|
✗ |
jso->cint.c_uint64 = i; |
| 768 |
|
✗ |
jso->cint_type = json_object_int_type_uint64; |
| 769 |
|
✗ |
return &jso->base; |
| 770 |
|
|
} |
| 771 |
|
|
|
| 772 |
|
✗ |
int64_t json_object_get_int64(const struct json_object *jso) |
| 773 |
|
|
{ |
| 774 |
|
|
int64_t cint; |
| 775 |
|
|
|
| 776 |
|
✗ |
if (!jso) |
| 777 |
|
|
return 0; |
| 778 |
|
✗ |
switch (jso->o_type) |
| 779 |
|
|
{ |
| 780 |
|
|
case json_type_int: |
| 781 |
|
|
{ |
| 782 |
|
|
const struct json_object_int *jsoint = JC_INT_C(jso); |
| 783 |
|
✗ |
switch (jsoint->cint_type) |
| 784 |
|
|
{ |
| 785 |
|
✗ |
case json_object_int_type_int64: return jsoint->cint.c_int64; |
| 786 |
|
✗ |
case json_object_int_type_uint64: |
| 787 |
|
✗ |
if (jsoint->cint.c_uint64 >= INT64_MAX) |
| 788 |
|
|
return INT64_MAX; |
| 789 |
|
✗ |
return (int64_t)jsoint->cint.c_uint64; |
| 790 |
|
✗ |
default: json_abort("invalid cint_type"); |
| 791 |
|
|
} |
| 792 |
|
|
} |
| 793 |
|
|
case json_type_double: |
| 794 |
|
|
// INT64_MAX can't be exactly represented as a double |
| 795 |
|
|
// so cast to tell the compiler it's ok to round up. |
| 796 |
|
✗ |
if (JC_DOUBLE_C(jso)->c_double >= (double)INT64_MAX) |
| 797 |
|
|
return INT64_MAX; |
| 798 |
|
✗ |
if (JC_DOUBLE_C(jso)->c_double <= INT64_MIN) |
| 799 |
|
|
return INT64_MIN; |
| 800 |
|
✗ |
return (int64_t)JC_DOUBLE_C(jso)->c_double; |
| 801 |
|
✗ |
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
| 802 |
|
|
case json_type_string: |
| 803 |
|
✗ |
if (json_parse_int64(get_string_component(jso), &cint) == 0) |
| 804 |
|
✗ |
return cint; |
| 805 |
|
|
/* FALLTHRU */ |
| 806 |
|
|
default: return 0; |
| 807 |
|
|
} |
| 808 |
|
|
} |
| 809 |
|
|
|
| 810 |
|
✗ |
uint64_t json_object_get_uint64(const struct json_object *jso) |
| 811 |
|
|
{ |
| 812 |
|
|
uint64_t cuint; |
| 813 |
|
|
|
| 814 |
|
✗ |
if (!jso) |
| 815 |
|
|
return 0; |
| 816 |
|
✗ |
switch (jso->o_type) |
| 817 |
|
|
{ |
| 818 |
|
|
case json_type_int: |
| 819 |
|
|
{ |
| 820 |
|
|
const struct json_object_int *jsoint = JC_INT_C(jso); |
| 821 |
|
✗ |
switch (jsoint->cint_type) |
| 822 |
|
|
{ |
| 823 |
|
✗ |
case json_object_int_type_int64: |
| 824 |
|
✗ |
if (jsoint->cint.c_int64 < 0) |
| 825 |
|
|
return 0; |
| 826 |
|
✗ |
return (uint64_t)jsoint->cint.c_int64; |
| 827 |
|
✗ |
case json_object_int_type_uint64: return jsoint->cint.c_uint64; |
| 828 |
|
✗ |
default: json_abort("invalid cint_type"); |
| 829 |
|
|
} |
| 830 |
|
|
} |
| 831 |
|
|
case json_type_double: |
| 832 |
|
|
// UINT64_MAX can't be exactly represented as a double |
| 833 |
|
|
// so cast to tell the compiler it's ok to round up. |
| 834 |
|
✗ |
if (JC_DOUBLE_C(jso)->c_double >= (double)UINT64_MAX) |
| 835 |
|
|
return UINT64_MAX; |
| 836 |
|
✗ |
if (JC_DOUBLE_C(jso)->c_double < 0) |
| 837 |
|
|
return 0; |
| 838 |
|
✗ |
return (uint64_t)JC_DOUBLE_C(jso)->c_double; |
| 839 |
|
✗ |
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
| 840 |
|
|
case json_type_string: |
| 841 |
|
✗ |
if (json_parse_uint64(get_string_component(jso), &cuint) == 0) |
| 842 |
|
✗ |
return cuint; |
| 843 |
|
|
/* FALLTHRU */ |
| 844 |
|
|
default: return 0; |
| 845 |
|
|
} |
| 846 |
|
|
} |
| 847 |
|
|
|
| 848 |
|
✗ |
int json_object_set_int64(struct json_object *jso, int64_t new_value) |
| 849 |
|
|
{ |
| 850 |
|
✗ |
if (!jso || jso->o_type != json_type_int) |
| 851 |
|
|
return 0; |
| 852 |
|
✗ |
JC_INT(jso)->cint.c_int64 = new_value; |
| 853 |
|
✗ |
JC_INT(jso)->cint_type = json_object_int_type_int64; |
| 854 |
|
✗ |
return 1; |
| 855 |
|
|
} |
| 856 |
|
|
|
| 857 |
|
✗ |
int json_object_set_uint64(struct json_object *jso, uint64_t new_value) |
| 858 |
|
|
{ |
| 859 |
|
✗ |
if (!jso || jso->o_type != json_type_int) |
| 860 |
|
|
return 0; |
| 861 |
|
✗ |
JC_INT(jso)->cint.c_uint64 = new_value; |
| 862 |
|
✗ |
JC_INT(jso)->cint_type = json_object_int_type_uint64; |
| 863 |
|
✗ |
return 1; |
| 864 |
|
|
} |
| 865 |
|
|
|
| 866 |
|
✗ |
int json_object_int_inc(struct json_object *jso, int64_t val) |
| 867 |
|
|
{ |
| 868 |
|
|
struct json_object_int *jsoint; |
| 869 |
|
✗ |
if (!jso || jso->o_type != json_type_int) |
| 870 |
|
|
return 0; |
| 871 |
|
|
jsoint = JC_INT(jso); |
| 872 |
|
✗ |
switch (jsoint->cint_type) |
| 873 |
|
|
{ |
| 874 |
|
✗ |
case json_object_int_type_int64: |
| 875 |
|
✗ |
if (val > 0 && jsoint->cint.c_int64 > INT64_MAX - val) |
| 876 |
|
|
{ |
| 877 |
|
✗ |
jsoint->cint.c_uint64 = (uint64_t)jsoint->cint.c_int64 + (uint64_t)val; |
| 878 |
|
✗ |
jsoint->cint_type = json_object_int_type_uint64; |
| 879 |
|
|
} |
| 880 |
|
✗ |
else if (val < 0 && jsoint->cint.c_int64 < INT64_MIN - val) |
| 881 |
|
|
{ |
| 882 |
|
✗ |
jsoint->cint.c_int64 = INT64_MIN; |
| 883 |
|
|
} |
| 884 |
|
|
else |
| 885 |
|
|
{ |
| 886 |
|
✗ |
jsoint->cint.c_int64 += val; |
| 887 |
|
|
} |
| 888 |
|
|
return 1; |
| 889 |
|
✗ |
case json_object_int_type_uint64: |
| 890 |
|
✗ |
if (val > 0 && jsoint->cint.c_uint64 > UINT64_MAX - (uint64_t)val) |
| 891 |
|
|
{ |
| 892 |
|
✗ |
jsoint->cint.c_uint64 = UINT64_MAX; |
| 893 |
|
|
} |
| 894 |
|
✗ |
else if (val < 0 && jsoint->cint.c_uint64 < (uint64_t)(-val)) |
| 895 |
|
|
{ |
| 896 |
|
✗ |
jsoint->cint.c_int64 = (int64_t)jsoint->cint.c_uint64 + val; |
| 897 |
|
✗ |
jsoint->cint_type = json_object_int_type_int64; |
| 898 |
|
|
} |
| 899 |
|
✗ |
else if (val < 0 && jsoint->cint.c_uint64 >= (uint64_t)(-val)) |
| 900 |
|
|
{ |
| 901 |
|
✗ |
jsoint->cint.c_uint64 -= (uint64_t)(-val); |
| 902 |
|
|
} |
| 903 |
|
|
else |
| 904 |
|
|
{ |
| 905 |
|
✗ |
jsoint->cint.c_uint64 += val; |
| 906 |
|
|
} |
| 907 |
|
|
return 1; |
| 908 |
|
✗ |
default: json_abort("invalid cint_type"); |
| 909 |
|
|
} |
| 910 |
|
|
} |
| 911 |
|
|
|
| 912 |
|
|
/* json_object_double */ |
| 913 |
|
|
|
| 914 |
|
|
#if defined(HAVE___THREAD) |
| 915 |
|
|
// i.e. __thread or __declspec(thread) |
| 916 |
|
|
static SPEC___THREAD char *tls_serialization_float_format = NULL; |
| 917 |
|
|
#endif |
| 918 |
|
|
static char *global_serialization_float_format = NULL; |
| 919 |
|
|
|
| 920 |
|
✗ |
int json_c_set_serialization_double_format(const char *double_format, int global_or_thread) |
| 921 |
|
|
{ |
| 922 |
|
✗ |
if (global_or_thread == JSON_C_OPTION_GLOBAL) |
| 923 |
|
|
{ |
| 924 |
|
|
#if defined(HAVE___THREAD) |
| 925 |
|
✗ |
if (tls_serialization_float_format) |
| 926 |
|
|
{ |
| 927 |
|
✗ |
free(tls_serialization_float_format); |
| 928 |
|
✗ |
tls_serialization_float_format = NULL; |
| 929 |
|
|
} |
| 930 |
|
|
#endif |
| 931 |
|
✗ |
if (global_serialization_float_format) |
| 932 |
|
✗ |
free(global_serialization_float_format); |
| 933 |
|
✗ |
if (double_format) |
| 934 |
|
|
{ |
| 935 |
|
✗ |
char *p = strdup(double_format); |
| 936 |
|
✗ |
if (p == NULL) |
| 937 |
|
|
{ |
| 938 |
|
✗ |
_json_c_set_last_err("json_c_set_serialization_double_format: " |
| 939 |
|
|
"out of memory\n"); |
| 940 |
|
✗ |
return -1; |
| 941 |
|
|
} |
| 942 |
|
✗ |
global_serialization_float_format = p; |
| 943 |
|
|
} |
| 944 |
|
|
else |
| 945 |
|
|
{ |
| 946 |
|
✗ |
global_serialization_float_format = NULL; |
| 947 |
|
|
} |
| 948 |
|
|
} |
| 949 |
|
✗ |
else if (global_or_thread == JSON_C_OPTION_THREAD) |
| 950 |
|
|
{ |
| 951 |
|
|
#if defined(HAVE___THREAD) |
| 952 |
|
✗ |
if (tls_serialization_float_format) |
| 953 |
|
|
{ |
| 954 |
|
✗ |
free(tls_serialization_float_format); |
| 955 |
|
✗ |
tls_serialization_float_format = NULL; |
| 956 |
|
|
} |
| 957 |
|
✗ |
if (double_format) |
| 958 |
|
|
{ |
| 959 |
|
✗ |
char *p = strdup(double_format); |
| 960 |
|
✗ |
if (p == NULL) |
| 961 |
|
|
{ |
| 962 |
|
✗ |
_json_c_set_last_err("json_c_set_serialization_double_format: " |
| 963 |
|
|
"out of memory\n"); |
| 964 |
|
✗ |
return -1; |
| 965 |
|
|
} |
| 966 |
|
✗ |
tls_serialization_float_format = p; |
| 967 |
|
|
} |
| 968 |
|
|
else |
| 969 |
|
|
{ |
| 970 |
|
✗ |
tls_serialization_float_format = NULL; |
| 971 |
|
|
} |
| 972 |
|
|
#else |
| 973 |
|
|
_json_c_set_last_err("json_c_set_serialization_double_format: not compiled " |
| 974 |
|
|
"with __thread support\n"); |
| 975 |
|
|
return -1; |
| 976 |
|
|
#endif |
| 977 |
|
|
} |
| 978 |
|
|
else |
| 979 |
|
|
{ |
| 980 |
|
✗ |
_json_c_set_last_err("json_c_set_serialization_double_format: invalid " |
| 981 |
|
|
"global_or_thread value: %d\n", global_or_thread); |
| 982 |
|
✗ |
return -1; |
| 983 |
|
|
} |
| 984 |
|
|
return 0; |
| 985 |
|
|
} |
| 986 |
|
|
|
| 987 |
|
✗ |
static int json_object_double_to_json_string_format(struct json_object *jso, struct printbuf *pb, |
| 988 |
|
|
int level, int flags, const char *format) |
| 989 |
|
|
{ |
| 990 |
|
|
struct json_object_double *jsodbl = JC_DOUBLE(jso); |
| 991 |
|
|
char buf[128], *p, *q; |
| 992 |
|
|
int size; |
| 993 |
|
|
/* Although JSON RFC does not support |
| 994 |
|
|
* NaN or Infinity as numeric values |
| 995 |
|
|
* ECMA 262 section 9.8.1 defines |
| 996 |
|
|
* how to handle these cases as strings |
| 997 |
|
|
*/ |
| 998 |
|
✗ |
if (isnan(jsodbl->c_double)) |
| 999 |
|
|
{ |
| 1000 |
|
|
size = snprintf(buf, sizeof(buf), "NaN"); |
| 1001 |
|
|
} |
| 1002 |
|
✗ |
else if (isinf(jsodbl->c_double)) |
| 1003 |
|
|
{ |
| 1004 |
|
✗ |
if (jsodbl->c_double > 0) |
| 1005 |
|
|
size = snprintf(buf, sizeof(buf), "Infinity"); |
| 1006 |
|
|
else |
| 1007 |
|
|
size = snprintf(buf, sizeof(buf), "-Infinity"); |
| 1008 |
|
|
} |
| 1009 |
|
|
else |
| 1010 |
|
|
{ |
| 1011 |
|
|
const char *std_format = "%.17g"; |
| 1012 |
|
|
int format_drops_decimals = 0; |
| 1013 |
|
|
int looks_numeric = 0; |
| 1014 |
|
|
|
| 1015 |
|
✗ |
if (!format) |
| 1016 |
|
|
{ |
| 1017 |
|
|
#if defined(HAVE___THREAD) |
| 1018 |
|
✗ |
if (tls_serialization_float_format) |
| 1019 |
|
|
format = tls_serialization_float_format; |
| 1020 |
|
|
else |
| 1021 |
|
|
#endif |
| 1022 |
|
✗ |
if (global_serialization_float_format) |
| 1023 |
|
|
format = global_serialization_float_format; |
| 1024 |
|
|
else |
| 1025 |
|
|
format = std_format; |
| 1026 |
|
|
} |
| 1027 |
|
|
size = snprintf(buf, sizeof(buf), format, jsodbl->c_double); |
| 1028 |
|
|
|
| 1029 |
|
✗ |
if (size < 0) |
| 1030 |
|
|
return -1; |
| 1031 |
|
|
|
| 1032 |
|
✗ |
p = strchr(buf, ','); |
| 1033 |
|
✗ |
if (p) |
| 1034 |
|
✗ |
*p = '.'; |
| 1035 |
|
|
else |
| 1036 |
|
✗ |
p = strchr(buf, '.'); |
| 1037 |
|
|
|
| 1038 |
|
✗ |
if (format == std_format || strstr(format, ".0f") == NULL) |
| 1039 |
|
|
format_drops_decimals = 1; |
| 1040 |
|
|
|
| 1041 |
|
|
looks_numeric = /* Looks like *some* kind of number */ |
| 1042 |
|
✗ |
is_plain_digit(buf[0]) || (size > 1 && buf[0] == '-' && is_plain_digit(buf[1])); |
| 1043 |
|
|
|
| 1044 |
|
✗ |
if (size < (int)sizeof(buf) - 2 && looks_numeric && !p && /* Has no decimal point */ |
| 1045 |
|
✗ |
strchr(buf, 'e') == NULL && /* Not scientific notation */ |
| 1046 |
|
|
format_drops_decimals) |
| 1047 |
|
|
{ |
| 1048 |
|
|
// Ensure it looks like a float, even if snprintf didn't, |
| 1049 |
|
|
// unless a custom format is set to omit the decimal. |
| 1050 |
|
|
strcat(buf, ".0"); |
| 1051 |
|
✗ |
size += 2; |
| 1052 |
|
|
} |
| 1053 |
|
✗ |
if (p && (flags & JSON_C_TO_STRING_NOZERO)) |
| 1054 |
|
|
{ |
| 1055 |
|
|
/* last useful digit, always keep 1 zero */ |
| 1056 |
|
✗ |
p++; |
| 1057 |
|
✗ |
for (q = p; *q; q++) |
| 1058 |
|
|
{ |
| 1059 |
|
✗ |
if (*q != '0') |
| 1060 |
|
|
p = q; |
| 1061 |
|
|
} |
| 1062 |
|
|
/* drop trailing zeroes */ |
| 1063 |
|
✗ |
if (*p != 0) |
| 1064 |
|
✗ |
*(++p) = 0; |
| 1065 |
|
✗ |
size = p - buf; |
| 1066 |
|
|
} |
| 1067 |
|
|
} |
| 1068 |
|
|
// although unlikely, snprintf can fail |
| 1069 |
|
✗ |
if (size < 0) |
| 1070 |
|
|
return -1; |
| 1071 |
|
|
|
| 1072 |
|
|
if (size >= (int)sizeof(buf)) |
| 1073 |
|
|
// The standard formats are guaranteed not to overrun the buffer, |
| 1074 |
|
|
// but if a custom one happens to do so, just silently truncate. |
| 1075 |
|
|
size = sizeof(buf) - 1; |
| 1076 |
|
✗ |
printbuf_memappend(pb, buf, size); |
| 1077 |
|
✗ |
return size; |
| 1078 |
|
|
} |
| 1079 |
|
|
|
| 1080 |
|
✗ |
static int json_object_double_to_json_string_default(struct json_object *jso, struct printbuf *pb, |
| 1081 |
|
|
int level, int flags) |
| 1082 |
|
|
{ |
| 1083 |
|
✗ |
return json_object_double_to_json_string_format(jso, pb, level, flags, NULL); |
| 1084 |
|
|
} |
| 1085 |
|
|
|
| 1086 |
|
✗ |
int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, int level, |
| 1087 |
|
|
int flags) |
| 1088 |
|
|
{ |
| 1089 |
|
✗ |
return json_object_double_to_json_string_format(jso, pb, level, flags, |
| 1090 |
|
✗ |
(const char *)jso->_userdata); |
| 1091 |
|
|
} |
| 1092 |
|
|
|
| 1093 |
|
✗ |
struct json_object *json_object_new_double(double d) |
| 1094 |
|
|
{ |
| 1095 |
|
✗ |
struct json_object_double *jso = JSON_OBJECT_NEW(double); |
| 1096 |
|
✗ |
if (!jso) |
| 1097 |
|
|
return NULL; |
| 1098 |
|
✗ |
jso->base._to_json_string = &json_object_double_to_json_string_default; |
| 1099 |
|
✗ |
jso->c_double = d; |
| 1100 |
|
✗ |
return &jso->base; |
| 1101 |
|
|
} |
| 1102 |
|
|
|
| 1103 |
|
✗ |
struct json_object *json_object_new_double_s(double d, const char *ds) |
| 1104 |
|
|
{ |
| 1105 |
|
|
char *new_ds; |
| 1106 |
|
✗ |
struct json_object *jso = json_object_new_double(d); |
| 1107 |
|
✗ |
if (!jso) |
| 1108 |
|
|
return NULL; |
| 1109 |
|
|
|
| 1110 |
|
✗ |
new_ds = strdup(ds); |
| 1111 |
|
✗ |
if (!new_ds) |
| 1112 |
|
|
{ |
| 1113 |
|
✗ |
json_object_generic_delete(jso); |
| 1114 |
|
✗ |
errno = ENOMEM; |
| 1115 |
|
✗ |
return NULL; |
| 1116 |
|
|
} |
| 1117 |
|
✗ |
json_object_set_serializer(jso, _json_object_userdata_to_json_string, new_ds, |
| 1118 |
|
|
json_object_free_userdata); |
| 1119 |
|
✗ |
return jso; |
| 1120 |
|
|
} |
| 1121 |
|
|
|
| 1122 |
|
|
/* |
| 1123 |
|
|
* A wrapper around json_object_userdata_to_json_string() used only |
| 1124 |
|
|
* by json_object_new_double_s() just so json_object_set_double() can |
| 1125 |
|
|
* detect when it needs to reset the serializer to the default. |
| 1126 |
|
|
*/ |
| 1127 |
|
✗ |
static int _json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, |
| 1128 |
|
|
int level, int flags) |
| 1129 |
|
|
{ |
| 1130 |
|
✗ |
return json_object_userdata_to_json_string(jso, pb, level, flags); |
| 1131 |
|
|
} |
| 1132 |
|
|
|
| 1133 |
|
✗ |
int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level, |
| 1134 |
|
|
int flags) |
| 1135 |
|
|
{ |
| 1136 |
|
✗ |
int userdata_len = strlen((const char *)jso->_userdata); |
| 1137 |
|
✗ |
printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len); |
| 1138 |
|
✗ |
return userdata_len; |
| 1139 |
|
|
} |
| 1140 |
|
|
|
| 1141 |
|
✗ |
void json_object_free_userdata(struct json_object *jso, void *userdata) |
| 1142 |
|
|
{ |
| 1143 |
|
✗ |
free(userdata); |
| 1144 |
|
|
} |
| 1145 |
|
|
|
| 1146 |
|
✗ |
double json_object_get_double(const struct json_object *jso) |
| 1147 |
|
|
{ |
| 1148 |
|
|
double cdouble; |
| 1149 |
|
✗ |
char *errPtr = NULL; |
| 1150 |
|
|
|
| 1151 |
|
✗ |
if (!jso) |
| 1152 |
|
|
return 0.0; |
| 1153 |
|
✗ |
switch (jso->o_type) |
| 1154 |
|
|
{ |
| 1155 |
|
✗ |
case json_type_double: return JC_DOUBLE_C(jso)->c_double; |
| 1156 |
|
|
case json_type_int: |
| 1157 |
|
✗ |
switch (JC_INT_C(jso)->cint_type) |
| 1158 |
|
|
{ |
| 1159 |
|
✗ |
case json_object_int_type_int64: return JC_INT_C(jso)->cint.c_int64; |
| 1160 |
|
✗ |
case json_object_int_type_uint64: return JC_INT_C(jso)->cint.c_uint64; |
| 1161 |
|
✗ |
default: json_abort("invalid cint_type"); |
| 1162 |
|
|
} |
| 1163 |
|
✗ |
case json_type_boolean: return JC_BOOL_C(jso)->c_boolean; |
| 1164 |
|
✗ |
case json_type_string: |
| 1165 |
|
✗ |
errno = 0; |
| 1166 |
|
✗ |
cdouble = strtod(get_string_component(jso), &errPtr); |
| 1167 |
|
|
|
| 1168 |
|
|
/* if conversion stopped at the first character, return 0.0 */ |
| 1169 |
|
✗ |
if (errPtr == get_string_component(jso)) |
| 1170 |
|
|
{ |
| 1171 |
|
✗ |
errno = EINVAL; |
| 1172 |
|
✗ |
return 0.0; |
| 1173 |
|
|
} |
| 1174 |
|
|
|
| 1175 |
|
|
/* |
| 1176 |
|
|
* Check that the conversion terminated on something sensible |
| 1177 |
|
|
* |
| 1178 |
|
|
* For example, { "pay" : 123AB } would parse as 123. |
| 1179 |
|
|
*/ |
| 1180 |
|
✗ |
if (*errPtr != '\0') |
| 1181 |
|
|
{ |
| 1182 |
|
✗ |
errno = EINVAL; |
| 1183 |
|
✗ |
return 0.0; |
| 1184 |
|
|
} |
| 1185 |
|
|
|
| 1186 |
|
|
/* |
| 1187 |
|
|
* If strtod encounters a string which would exceed the |
| 1188 |
|
|
* capacity of a double, it returns +/- HUGE_VAL and sets |
| 1189 |
|
|
* errno to ERANGE. But +/- HUGE_VAL is also a valid result |
| 1190 |
|
|
* from a conversion, so we need to check errno. |
| 1191 |
|
|
* |
| 1192 |
|
|
* Underflow also sets errno to ERANGE, but it returns 0 in |
| 1193 |
|
|
* that case, which is what we will return anyway. |
| 1194 |
|
|
* |
| 1195 |
|
|
* See CERT guideline ERR30-C |
| 1196 |
|
|
*/ |
| 1197 |
|
✗ |
if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) && (ERANGE == errno)) |
| 1198 |
|
|
cdouble = 0.0; |
| 1199 |
|
|
return cdouble; |
| 1200 |
|
✗ |
default: errno = EINVAL; return 0.0; |
| 1201 |
|
|
} |
| 1202 |
|
|
} |
| 1203 |
|
|
|
| 1204 |
|
✗ |
int json_object_set_double(struct json_object *jso, double new_value) |
| 1205 |
|
|
{ |
| 1206 |
|
✗ |
if (!jso || jso->o_type != json_type_double) |
| 1207 |
|
|
return 0; |
| 1208 |
|
✗ |
JC_DOUBLE(jso)->c_double = new_value; |
| 1209 |
|
✗ |
if (jso->_to_json_string == &_json_object_userdata_to_json_string) |
| 1210 |
|
✗ |
json_object_set_serializer(jso, NULL, NULL, NULL); |
| 1211 |
|
|
return 1; |
| 1212 |
|
|
} |
| 1213 |
|
|
|
| 1214 |
|
|
/* json_object_string */ |
| 1215 |
|
|
|
| 1216 |
|
✗ |
static int json_object_string_to_json_string(struct json_object *jso, struct printbuf *pb, |
| 1217 |
|
|
int level, int flags) |
| 1218 |
|
|
{ |
| 1219 |
|
✗ |
ssize_t len = JC_STRING(jso)->len; |
| 1220 |
|
✗ |
printbuf_strappend(pb, "\""); |
| 1221 |
|
✗ |
json_escape_str(pb, get_string_component(jso), len < 0 ? -(ssize_t)len : len, flags); |
| 1222 |
|
✗ |
printbuf_strappend(pb, "\""); |
| 1223 |
|
✗ |
return 0; |
| 1224 |
|
|
} |
| 1225 |
|
|
|
| 1226 |
|
✗ |
static void json_object_string_delete(struct json_object *jso) |
| 1227 |
|
|
{ |
| 1228 |
|
✗ |
if (JC_STRING(jso)->len < 0) |
| 1229 |
|
✗ |
free(JC_STRING(jso)->c_string.pdata); |
| 1230 |
|
✗ |
json_object_generic_delete(jso); |
| 1231 |
|
|
} |
| 1232 |
|
|
|
| 1233 |
|
✗ |
static struct json_object *_json_object_new_string(const char *s, const size_t len) |
| 1234 |
|
|
{ |
| 1235 |
|
|
size_t objsize; |
| 1236 |
|
|
struct json_object_string *jso; |
| 1237 |
|
|
|
| 1238 |
|
|
/* |
| 1239 |
|
|
* Structures Actual memory layout |
| 1240 |
|
|
* ------------------- -------------------- |
| 1241 |
|
|
* [json_object_string [json_object_string |
| 1242 |
|
|
* [json_object] [json_object] |
| 1243 |
|
|
* ...other fields... ...other fields... |
| 1244 |
|
|
* c_string] len |
| 1245 |
|
|
* bytes |
| 1246 |
|
|
* of |
| 1247 |
|
|
* string |
| 1248 |
|
|
* data |
| 1249 |
|
|
* \0] |
| 1250 |
|
|
*/ |
| 1251 |
|
✗ |
if (len > (SSIZE_T_MAX - (sizeof(*jso) - sizeof(jso->c_string)) - 1)) |
| 1252 |
|
|
return NULL; |
| 1253 |
|
✗ |
objsize = (sizeof(*jso) - sizeof(jso->c_string)) + len + 1; |
| 1254 |
|
✗ |
if (len < sizeof(void *)) |
| 1255 |
|
|
// We need a minimum size to support json_object_set_string() mutability |
| 1256 |
|
|
// so we can stuff a pointer into pdata :( |
| 1257 |
|
|
objsize += sizeof(void *) - len; |
| 1258 |
|
|
|
| 1259 |
|
✗ |
jso = (struct json_object_string *)json_object_new(json_type_string, objsize, |
| 1260 |
|
|
&json_object_string_to_json_string); |
| 1261 |
|
|
|
| 1262 |
|
✗ |
if (!jso) |
| 1263 |
|
|
return NULL; |
| 1264 |
|
✗ |
jso->len = len; |
| 1265 |
|
✗ |
memcpy(jso->c_string.idata, s, len); |
| 1266 |
|
|
// Cast below needed for Clang UB sanitizer |
| 1267 |
|
✗ |
((char *)jso->c_string.idata)[len] = '\0'; |
| 1268 |
|
✗ |
return &jso->base; |
| 1269 |
|
|
} |
| 1270 |
|
|
|
| 1271 |
|
✗ |
struct json_object *json_object_new_string(const char *s) |
| 1272 |
|
|
{ |
| 1273 |
|
✗ |
return _json_object_new_string(s, strlen(s)); |
| 1274 |
|
|
} |
| 1275 |
|
|
|
| 1276 |
|
✗ |
struct json_object *json_object_new_string_len(const char *s, const int len) |
| 1277 |
|
|
{ |
| 1278 |
|
✗ |
return _json_object_new_string(s, len); |
| 1279 |
|
|
} |
| 1280 |
|
|
|
| 1281 |
|
✗ |
const char *json_object_get_string(struct json_object *jso) |
| 1282 |
|
|
{ |
| 1283 |
|
✗ |
if (!jso) |
| 1284 |
|
|
return NULL; |
| 1285 |
|
✗ |
switch (jso->o_type) |
| 1286 |
|
|
{ |
| 1287 |
|
|
case json_type_string: return get_string_component(jso); |
| 1288 |
|
✗ |
default: return json_object_to_json_string(jso); |
| 1289 |
|
|
} |
| 1290 |
|
|
} |
| 1291 |
|
|
|
| 1292 |
|
|
static inline ssize_t _json_object_get_string_len(const struct json_object_string *jso) |
| 1293 |
|
|
{ |
| 1294 |
|
|
ssize_t len; |
| 1295 |
|
✗ |
len = jso->len; |
| 1296 |
|
✗ |
return (len < 0) ? -(ssize_t)len : len; |
| 1297 |
|
|
} |
| 1298 |
|
✗ |
int json_object_get_string_len(const struct json_object *jso) |
| 1299 |
|
|
{ |
| 1300 |
|
✗ |
if (!jso) |
| 1301 |
|
|
return 0; |
| 1302 |
|
✗ |
switch (jso->o_type) |
| 1303 |
|
|
{ |
| 1304 |
|
✗ |
case json_type_string: return _json_object_get_string_len(JC_STRING_C(jso)); |
| 1305 |
|
|
default: return 0; |
| 1306 |
|
|
} |
| 1307 |
|
|
} |
| 1308 |
|
|
|
| 1309 |
|
✗ |
static int _json_object_set_string_len(json_object *jso, const char *s, size_t len) |
| 1310 |
|
|
{ |
| 1311 |
|
|
char *dstbuf; |
| 1312 |
|
|
ssize_t curlen; |
| 1313 |
|
|
ssize_t newlen; |
| 1314 |
|
✗ |
if (jso == NULL || jso->o_type != json_type_string) |
| 1315 |
|
|
return 0; |
| 1316 |
|
|
|
| 1317 |
|
✗ |
if (len >= INT_MAX - 1) |
| 1318 |
|
|
// jso->len is a signed ssize_t, so it can't hold the |
| 1319 |
|
|
// full size_t range. json_object_get_string_len returns |
| 1320 |
|
|
// length as int, cap length at INT_MAX. |
| 1321 |
|
|
return 0; |
| 1322 |
|
|
|
| 1323 |
|
✗ |
curlen = JC_STRING(jso)->len; |
| 1324 |
|
✗ |
if (curlen < 0) { |
| 1325 |
|
✗ |
if (len == 0) { |
| 1326 |
|
✗ |
free(JC_STRING(jso)->c_string.pdata); |
| 1327 |
|
✗ |
JC_STRING(jso)->len = curlen = 0; |
| 1328 |
|
|
} else { |
| 1329 |
|
✗ |
curlen = -curlen; |
| 1330 |
|
|
} |
| 1331 |
|
|
} |
| 1332 |
|
|
|
| 1333 |
|
✗ |
newlen = len; |
| 1334 |
|
|
dstbuf = get_string_component_mutable(jso); |
| 1335 |
|
|
|
| 1336 |
|
✗ |
if ((ssize_t)len > curlen) |
| 1337 |
|
|
{ |
| 1338 |
|
|
// We have no way to return the new ptr from realloc(jso, newlen) |
| 1339 |
|
|
// and we have no way of knowing whether there's extra room available |
| 1340 |
|
|
// so we need to stuff a pointer in to pdata :( |
| 1341 |
|
✗ |
dstbuf = (char *)malloc(len + 1); |
| 1342 |
|
✗ |
if (dstbuf == NULL) |
| 1343 |
|
|
return 0; |
| 1344 |
|
✗ |
if (JC_STRING(jso)->len < 0) |
| 1345 |
|
✗ |
free(JC_STRING(jso)->c_string.pdata); |
| 1346 |
|
✗ |
JC_STRING(jso)->c_string.pdata = dstbuf; |
| 1347 |
|
✗ |
newlen = -(ssize_t)len; |
| 1348 |
|
|
} |
| 1349 |
|
✗ |
else if (JC_STRING(jso)->len < 0) |
| 1350 |
|
|
{ |
| 1351 |
|
|
// We've got enough room in the separate allocated buffer, |
| 1352 |
|
|
// so use it as-is and continue to indicate that pdata is used. |
| 1353 |
|
✗ |
newlen = -(ssize_t)len; |
| 1354 |
|
|
} |
| 1355 |
|
|
|
| 1356 |
|
|
memcpy(dstbuf, (const void *)s, len); |
| 1357 |
|
✗ |
dstbuf[len] = '\0'; |
| 1358 |
|
✗ |
JC_STRING(jso)->len = newlen; |
| 1359 |
|
✗ |
return 1; |
| 1360 |
|
|
} |
| 1361 |
|
|
|
| 1362 |
|
✗ |
int json_object_set_string(json_object *jso, const char *s) |
| 1363 |
|
|
{ |
| 1364 |
|
✗ |
return _json_object_set_string_len(jso, s, strlen(s)); |
| 1365 |
|
|
} |
| 1366 |
|
|
|
| 1367 |
|
✗ |
int json_object_set_string_len(json_object *jso, const char *s, int len) |
| 1368 |
|
|
{ |
| 1369 |
|
✗ |
return _json_object_set_string_len(jso, s, len); |
| 1370 |
|
|
} |
| 1371 |
|
|
|
| 1372 |
|
|
/* json_object_array */ |
| 1373 |
|
|
|
| 1374 |
|
✗ |
static int json_object_array_to_json_string(struct json_object *jso, struct printbuf *pb, int level, |
| 1375 |
|
|
int flags) |
| 1376 |
|
|
{ |
| 1377 |
|
|
int had_children = 0; |
| 1378 |
|
|
size_t ii; |
| 1379 |
|
|
|
| 1380 |
|
✗ |
printbuf_strappend(pb, "["); |
| 1381 |
|
✗ |
for (ii = 0; ii < json_object_array_length(jso); ii++) |
| 1382 |
|
|
{ |
| 1383 |
|
|
struct json_object *val; |
| 1384 |
|
✗ |
if (had_children) |
| 1385 |
|
|
{ |
| 1386 |
|
✗ |
printbuf_strappend(pb, ","); |
| 1387 |
|
|
} |
| 1388 |
|
✗ |
if (flags & JSON_C_TO_STRING_PRETTY) |
| 1389 |
|
✗ |
printbuf_strappend(pb, "\n"); |
| 1390 |
|
|
had_children = 1; |
| 1391 |
|
✗ |
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY)) |
| 1392 |
|
✗ |
printbuf_strappend(pb, " "); |
| 1393 |
|
✗ |
indent(pb, level + 1, flags); |
| 1394 |
|
✗ |
val = json_object_array_get_idx(jso, ii); |
| 1395 |
|
✗ |
if (val == NULL) |
| 1396 |
|
✗ |
printbuf_strappend(pb, "null"); |
| 1397 |
|
✗ |
else if (val->_to_json_string(val, pb, level + 1, flags) < 0) |
| 1398 |
|
|
return -1; |
| 1399 |
|
|
} |
| 1400 |
|
✗ |
if ((flags & JSON_C_TO_STRING_PRETTY) && had_children) |
| 1401 |
|
|
{ |
| 1402 |
|
✗ |
printbuf_strappend(pb, "\n"); |
| 1403 |
|
✗ |
indent(pb, level, flags); |
| 1404 |
|
|
} |
| 1405 |
|
|
|
| 1406 |
|
✗ |
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY)) |
| 1407 |
|
✗ |
return printbuf_strappend(pb, " ]"); |
| 1408 |
|
✗ |
return printbuf_strappend(pb, "]"); |
| 1409 |
|
|
} |
| 1410 |
|
|
|
| 1411 |
|
✗ |
static void json_object_array_entry_free(void *data) |
| 1412 |
|
|
{ |
| 1413 |
|
✗ |
json_object_put((struct json_object *)data); |
| 1414 |
|
|
} |
| 1415 |
|
|
|
| 1416 |
|
✗ |
static void json_object_array_delete(struct json_object *jso) |
| 1417 |
|
|
{ |
| 1418 |
|
✗ |
array_list_free(JC_ARRAY(jso)->c_array); |
| 1419 |
|
✗ |
json_object_generic_delete(jso); |
| 1420 |
|
|
} |
| 1421 |
|
|
|
| 1422 |
|
✗ |
struct json_object *json_object_new_array(void) |
| 1423 |
|
|
{ |
| 1424 |
|
✗ |
return json_object_new_array_ext(ARRAY_LIST_DEFAULT_SIZE); |
| 1425 |
|
|
} |
| 1426 |
|
✗ |
struct json_object *json_object_new_array_ext(int initial_size) |
| 1427 |
|
|
{ |
| 1428 |
|
✗ |
struct json_object_array *jso = JSON_OBJECT_NEW(array); |
| 1429 |
|
✗ |
if (!jso) |
| 1430 |
|
|
return NULL; |
| 1431 |
|
✗ |
jso->c_array = array_list_new2(&json_object_array_entry_free, initial_size); |
| 1432 |
|
✗ |
if (jso->c_array == NULL) |
| 1433 |
|
|
{ |
| 1434 |
|
✗ |
free(jso); |
| 1435 |
|
✗ |
return NULL; |
| 1436 |
|
|
} |
| 1437 |
|
✗ |
return &jso->base; |
| 1438 |
|
|
} |
| 1439 |
|
|
|
| 1440 |
|
✗ |
struct array_list *json_object_get_array(const struct json_object *jso) |
| 1441 |
|
|
{ |
| 1442 |
|
✗ |
if (!jso) |
| 1443 |
|
|
return NULL; |
| 1444 |
|
✗ |
switch (jso->o_type) |
| 1445 |
|
|
{ |
| 1446 |
|
✗ |
case json_type_array: return JC_ARRAY_C(jso)->c_array; |
| 1447 |
|
|
default: return NULL; |
| 1448 |
|
|
} |
| 1449 |
|
|
} |
| 1450 |
|
|
|
| 1451 |
|
✗ |
void json_object_array_sort(struct json_object *jso, int (*sort_fn)(const void *, const void *)) |
| 1452 |
|
|
{ |
| 1453 |
|
|
assert(json_object_get_type(jso) == json_type_array); |
| 1454 |
|
✗ |
array_list_sort(JC_ARRAY(jso)->c_array, sort_fn); |
| 1455 |
|
|
} |
| 1456 |
|
|
|
| 1457 |
|
✗ |
struct json_object *json_object_array_bsearch(const struct json_object *key, |
| 1458 |
|
|
const struct json_object *jso, |
| 1459 |
|
|
int (*sort_fn)(const void *, const void *)) |
| 1460 |
|
|
{ |
| 1461 |
|
|
struct json_object **result; |
| 1462 |
|
|
|
| 1463 |
|
|
assert(json_object_get_type(jso) == json_type_array); |
| 1464 |
|
✗ |
result = (struct json_object **)array_list_bsearch((const void **)(void *)&key, |
| 1465 |
|
✗ |
JC_ARRAY_C(jso)->c_array, sort_fn); |
| 1466 |
|
|
|
| 1467 |
|
✗ |
if (!result) |
| 1468 |
|
|
return NULL; |
| 1469 |
|
✗ |
return *result; |
| 1470 |
|
|
} |
| 1471 |
|
|
|
| 1472 |
|
✗ |
size_t json_object_array_length(const struct json_object *jso) |
| 1473 |
|
|
{ |
| 1474 |
|
|
assert(json_object_get_type(jso) == json_type_array); |
| 1475 |
|
✗ |
return array_list_length(JC_ARRAY_C(jso)->c_array); |
| 1476 |
|
|
} |
| 1477 |
|
|
|
| 1478 |
|
✗ |
int json_object_array_add(struct json_object *jso, struct json_object *val) |
| 1479 |
|
|
{ |
| 1480 |
|
|
assert(json_object_get_type(jso) == json_type_array); |
| 1481 |
|
✗ |
return array_list_add(JC_ARRAY(jso)->c_array, val); |
| 1482 |
|
|
} |
| 1483 |
|
|
|
| 1484 |
|
✗ |
int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val) |
| 1485 |
|
|
{ |
| 1486 |
|
|
assert(json_object_get_type(jso) == json_type_array); |
| 1487 |
|
✗ |
return array_list_put_idx(JC_ARRAY(jso)->c_array, idx, val); |
| 1488 |
|
|
} |
| 1489 |
|
|
|
| 1490 |
|
✗ |
int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count) |
| 1491 |
|
|
{ |
| 1492 |
|
|
assert(json_object_get_type(jso) == json_type_array); |
| 1493 |
|
✗ |
return array_list_del_idx(JC_ARRAY(jso)->c_array, idx, count); |
| 1494 |
|
|
} |
| 1495 |
|
|
|
| 1496 |
|
✗ |
struct json_object *json_object_array_get_idx(const struct json_object *jso, size_t idx) |
| 1497 |
|
|
{ |
| 1498 |
|
|
assert(json_object_get_type(jso) == json_type_array); |
| 1499 |
|
✗ |
return (struct json_object *)array_list_get_idx(JC_ARRAY_C(jso)->c_array, idx); |
| 1500 |
|
|
} |
| 1501 |
|
|
|
| 1502 |
|
✗ |
static int json_array_equal(struct json_object *jso1, struct json_object *jso2) |
| 1503 |
|
|
{ |
| 1504 |
|
|
size_t len, i; |
| 1505 |
|
|
|
| 1506 |
|
✗ |
len = json_object_array_length(jso1); |
| 1507 |
|
✗ |
if (len != json_object_array_length(jso2)) |
| 1508 |
|
|
return 0; |
| 1509 |
|
|
|
| 1510 |
|
✗ |
for (i = 0; i < len; i++) |
| 1511 |
|
|
{ |
| 1512 |
|
✗ |
if (!json_object_equal(json_object_array_get_idx(jso1, i), |
| 1513 |
|
|
json_object_array_get_idx(jso2, i))) |
| 1514 |
|
|
return 0; |
| 1515 |
|
|
} |
| 1516 |
|
|
return 1; |
| 1517 |
|
|
} |
| 1518 |
|
|
|
| 1519 |
|
✗ |
int json_object_array_shrink(struct json_object *jso, int empty_slots) |
| 1520 |
|
|
{ |
| 1521 |
|
✗ |
if (empty_slots < 0) |
| 1522 |
|
✗ |
json_abort("json_object_array_shrink called with negative empty_slots"); |
| 1523 |
|
✗ |
return array_list_shrink(JC_ARRAY(jso)->c_array, empty_slots); |
| 1524 |
|
|
} |
| 1525 |
|
|
|
| 1526 |
|
✗ |
struct json_object *json_object_new_null(void) |
| 1527 |
|
|
{ |
| 1528 |
|
✗ |
return NULL; |
| 1529 |
|
|
} |
| 1530 |
|
|
|
| 1531 |
|
✗ |
static int json_object_all_values_equal(struct json_object *jso1, struct json_object *jso2) |
| 1532 |
|
|
{ |
| 1533 |
|
|
struct json_object_iter iter; |
| 1534 |
|
|
struct json_object *sub; |
| 1535 |
|
|
|
| 1536 |
|
|
assert(json_object_get_type(jso1) == json_type_object); |
| 1537 |
|
|
assert(json_object_get_type(jso2) == json_type_object); |
| 1538 |
|
|
/* Iterate over jso1 keys and see if they exist and are equal in jso2 */ |
| 1539 |
|
✗ |
json_object_object_foreachC(jso1, iter) |
| 1540 |
|
|
{ |
| 1541 |
|
✗ |
if (!lh_table_lookup_ex(JC_OBJECT(jso2)->c_object, (void *)iter.key, |
| 1542 |
|
|
(void **)(void *)&sub)) |
| 1543 |
|
|
return 0; |
| 1544 |
|
✗ |
if (!json_object_equal(iter.val, sub)) |
| 1545 |
|
|
return 0; |
| 1546 |
|
|
} |
| 1547 |
|
|
|
| 1548 |
|
|
/* Iterate over jso2 keys to see if any exist that are not in jso1 */ |
| 1549 |
|
✗ |
json_object_object_foreachC(jso2, iter) |
| 1550 |
|
|
{ |
| 1551 |
|
✗ |
if (!lh_table_lookup_ex(JC_OBJECT(jso1)->c_object, (void *)iter.key, |
| 1552 |
|
|
(void **)(void *)&sub)) |
| 1553 |
|
|
return 0; |
| 1554 |
|
|
} |
| 1555 |
|
|
|
| 1556 |
|
|
return 1; |
| 1557 |
|
|
} |
| 1558 |
|
|
|
| 1559 |
|
✗ |
int json_object_equal(struct json_object *jso1, struct json_object *jso2) |
| 1560 |
|
|
{ |
| 1561 |
|
✗ |
if (jso1 == jso2) |
| 1562 |
|
|
return 1; |
| 1563 |
|
|
|
| 1564 |
|
✗ |
if (!jso1 || !jso2) |
| 1565 |
|
|
return 0; |
| 1566 |
|
|
|
| 1567 |
|
✗ |
if (jso1->o_type != jso2->o_type) |
| 1568 |
|
|
return 0; |
| 1569 |
|
|
|
| 1570 |
|
✗ |
switch (jso1->o_type) |
| 1571 |
|
|
{ |
| 1572 |
|
✗ |
case json_type_boolean: return (JC_BOOL(jso1)->c_boolean == JC_BOOL(jso2)->c_boolean); |
| 1573 |
|
|
|
| 1574 |
|
✗ |
case json_type_double: return (JC_DOUBLE(jso1)->c_double == JC_DOUBLE(jso2)->c_double); |
| 1575 |
|
|
|
| 1576 |
|
|
case json_type_int: |
| 1577 |
|
|
{ |
| 1578 |
|
|
struct json_object_int *int1 = JC_INT(jso1); |
| 1579 |
|
|
struct json_object_int *int2 = JC_INT(jso2); |
| 1580 |
|
✗ |
if (int1->cint_type == json_object_int_type_int64) |
| 1581 |
|
|
{ |
| 1582 |
|
✗ |
if (int2->cint_type == json_object_int_type_int64) |
| 1583 |
|
✗ |
return (int1->cint.c_int64 == int2->cint.c_int64); |
| 1584 |
|
✗ |
if (int1->cint.c_int64 < 0) |
| 1585 |
|
|
return 0; |
| 1586 |
|
✗ |
return ((uint64_t)int1->cint.c_int64 == int2->cint.c_uint64); |
| 1587 |
|
|
} |
| 1588 |
|
|
// else jso1 is a uint64 |
| 1589 |
|
✗ |
if (int2->cint_type == json_object_int_type_uint64) |
| 1590 |
|
✗ |
return (int1->cint.c_uint64 == int2->cint.c_uint64); |
| 1591 |
|
✗ |
if (int2->cint.c_int64 < 0) |
| 1592 |
|
|
return 0; |
| 1593 |
|
✗ |
return (int1->cint.c_uint64 == (uint64_t)int2->cint.c_int64); |
| 1594 |
|
|
} |
| 1595 |
|
|
|
| 1596 |
|
|
case json_type_string: |
| 1597 |
|
|
{ |
| 1598 |
|
|
return (_json_object_get_string_len(JC_STRING(jso1)) == |
| 1599 |
|
✗ |
_json_object_get_string_len(JC_STRING(jso2)) && |
| 1600 |
|
✗ |
memcmp(get_string_component(jso1), get_string_component(jso2), |
| 1601 |
|
|
_json_object_get_string_len(JC_STRING(jso1))) == 0); |
| 1602 |
|
|
} |
| 1603 |
|
|
|
| 1604 |
|
✗ |
case json_type_object: return json_object_all_values_equal(jso1, jso2); |
| 1605 |
|
|
|
| 1606 |
|
✗ |
case json_type_array: return json_array_equal(jso1, jso2); |
| 1607 |
|
|
|
| 1608 |
|
|
case json_type_null: return 1; |
| 1609 |
|
|
}; |
| 1610 |
|
|
|
| 1611 |
|
✗ |
return 0; |
| 1612 |
|
|
} |
| 1613 |
|
|
|
| 1614 |
|
✗ |
static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst) |
| 1615 |
|
|
{ |
| 1616 |
|
✗ |
if (!src->_userdata && !src->_user_delete) |
| 1617 |
|
|
return 0; |
| 1618 |
|
|
|
| 1619 |
|
✗ |
if (dst->_to_json_string == json_object_userdata_to_json_string || |
| 1620 |
|
|
dst->_to_json_string == _json_object_userdata_to_json_string) |
| 1621 |
|
|
{ |
| 1622 |
|
|
char *p; |
| 1623 |
|
|
assert(src->_userdata); |
| 1624 |
|
✗ |
p = strdup(src->_userdata); |
| 1625 |
|
✗ |
if (p == NULL) |
| 1626 |
|
|
{ |
| 1627 |
|
✗ |
_json_c_set_last_err("json_object_copy_serializer_data: out of memory\n"); |
| 1628 |
|
✗ |
return -1; |
| 1629 |
|
|
} |
| 1630 |
|
✗ |
dst->_userdata = p; |
| 1631 |
|
|
} |
| 1632 |
|
|
// else if ... other supported serializers ... |
| 1633 |
|
|
else |
| 1634 |
|
|
{ |
| 1635 |
|
✗ |
_json_c_set_last_err( |
| 1636 |
|
|
"json_object_copy_serializer_data: unable to copy unknown serializer data: " |
| 1637 |
|
|
"%p\n", (void *)dst->_to_json_string); |
| 1638 |
|
✗ |
return -1; |
| 1639 |
|
|
} |
| 1640 |
|
✗ |
dst->_user_delete = src->_user_delete; |
| 1641 |
|
✗ |
return 0; |
| 1642 |
|
|
} |
| 1643 |
|
|
|
| 1644 |
|
|
/** |
| 1645 |
|
|
* The default shallow copy implementation. Simply creates a new object of the same |
| 1646 |
|
|
* type but does *not* copy over _userdata nor retain any custom serializer. |
| 1647 |
|
|
* If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy |
| 1648 |
|
|
* implementation that is aware of how to copy them. |
| 1649 |
|
|
* |
| 1650 |
|
|
* This always returns -1 or 1. It will never return 2 since it does not copy the serializer. |
| 1651 |
|
|
*/ |
| 1652 |
|
✗ |
int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, |
| 1653 |
|
|
size_t index, json_object **dst) |
| 1654 |
|
|
{ |
| 1655 |
|
✗ |
switch (src->o_type) |
| 1656 |
|
|
{ |
| 1657 |
|
✗ |
case json_type_boolean: *dst = json_object_new_boolean(JC_BOOL(src)->c_boolean); break; |
| 1658 |
|
|
|
| 1659 |
|
✗ |
case json_type_double: *dst = json_object_new_double(JC_DOUBLE(src)->c_double); break; |
| 1660 |
|
|
|
| 1661 |
|
|
case json_type_int: |
| 1662 |
|
✗ |
switch (JC_INT(src)->cint_type) |
| 1663 |
|
|
{ |
| 1664 |
|
|
case json_object_int_type_int64: |
| 1665 |
|
✗ |
*dst = json_object_new_int64(JC_INT(src)->cint.c_int64); |
| 1666 |
|
✗ |
break; |
| 1667 |
|
|
case json_object_int_type_uint64: |
| 1668 |
|
✗ |
*dst = json_object_new_uint64(JC_INT(src)->cint.c_uint64); |
| 1669 |
|
✗ |
break; |
| 1670 |
|
✗ |
default: json_abort("invalid cint_type"); |
| 1671 |
|
|
} |
| 1672 |
|
|
break; |
| 1673 |
|
|
|
| 1674 |
|
|
case json_type_string: |
| 1675 |
|
✗ |
*dst = json_object_new_string_len(get_string_component(src), |
| 1676 |
|
|
_json_object_get_string_len(JC_STRING(src))); |
| 1677 |
|
✗ |
break; |
| 1678 |
|
|
|
| 1679 |
|
✗ |
case json_type_object: *dst = json_object_new_object(); break; |
| 1680 |
|
|
|
| 1681 |
|
✗ |
case json_type_array: *dst = json_object_new_array(); break; |
| 1682 |
|
|
|
| 1683 |
|
✗ |
default: errno = EINVAL; return -1; |
| 1684 |
|
|
} |
| 1685 |
|
|
|
| 1686 |
|
✗ |
if (!*dst) |
| 1687 |
|
|
{ |
| 1688 |
|
✗ |
errno = ENOMEM; |
| 1689 |
|
✗ |
return -1; |
| 1690 |
|
|
} |
| 1691 |
|
✗ |
(*dst)->_to_json_string = src->_to_json_string; |
| 1692 |
|
|
// _userdata and _user_delete are copied later |
| 1693 |
|
✗ |
return 1; |
| 1694 |
|
|
} |
| 1695 |
|
|
|
| 1696 |
|
|
/* |
| 1697 |
|
|
* The actual guts of json_object_deep_copy(), with a few additional args |
| 1698 |
|
|
* needed so we can keep track of where we are within the object tree. |
| 1699 |
|
|
* |
| 1700 |
|
|
* Note: caller is responsible for freeing *dst if this fails and returns -1. |
| 1701 |
|
|
*/ |
| 1702 |
|
✗ |
static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent, |
| 1703 |
|
|
const char *key_in_parent, size_t index_in_parent, |
| 1704 |
|
|
struct json_object **dst, |
| 1705 |
|
|
json_c_shallow_copy_fn *shallow_copy) |
| 1706 |
|
|
{ |
| 1707 |
|
|
struct json_object_iter iter; |
| 1708 |
|
|
size_t src_array_len, ii; |
| 1709 |
|
|
|
| 1710 |
|
|
int shallow_copy_rc = 0; |
| 1711 |
|
✗ |
shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst); |
| 1712 |
|
|
/* -1=error, 1=object created ok, 2=userdata set */ |
| 1713 |
|
✗ |
if (shallow_copy_rc < 1) |
| 1714 |
|
|
{ |
| 1715 |
|
✗ |
errno = EINVAL; |
| 1716 |
|
✗ |
return -1; |
| 1717 |
|
|
} |
| 1718 |
|
|
assert(*dst != NULL); |
| 1719 |
|
|
|
| 1720 |
|
✗ |
switch (src->o_type) |
| 1721 |
|
|
{ |
| 1722 |
|
✗ |
case json_type_object: |
| 1723 |
|
✗ |
json_object_object_foreachC(src, iter) |
| 1724 |
|
|
{ |
| 1725 |
|
✗ |
struct json_object *jso = NULL; |
| 1726 |
|
|
/* This handles the `json_type_null` case */ |
| 1727 |
|
✗ |
if (!iter.val) |
| 1728 |
|
|
jso = NULL; |
| 1729 |
|
✗ |
else if (json_object_deep_copy_recursive(iter.val, src, iter.key, UINT_MAX, |
| 1730 |
|
|
&jso, shallow_copy) < 0) |
| 1731 |
|
|
{ |
| 1732 |
|
✗ |
json_object_put(jso); |
| 1733 |
|
✗ |
return -1; |
| 1734 |
|
|
} |
| 1735 |
|
|
|
| 1736 |
|
✗ |
if (json_object_object_add(*dst, iter.key, jso) < 0) |
| 1737 |
|
|
{ |
| 1738 |
|
✗ |
json_object_put(jso); |
| 1739 |
|
✗ |
return -1; |
| 1740 |
|
|
} |
| 1741 |
|
|
} |
| 1742 |
|
|
break; |
| 1743 |
|
|
|
| 1744 |
|
✗ |
case json_type_array: |
| 1745 |
|
✗ |
src_array_len = json_object_array_length(src); |
| 1746 |
|
✗ |
for (ii = 0; ii < src_array_len; ii++) |
| 1747 |
|
|
{ |
| 1748 |
|
✗ |
struct json_object *jso = NULL; |
| 1749 |
|
✗ |
struct json_object *jso1 = json_object_array_get_idx(src, ii); |
| 1750 |
|
|
/* This handles the `json_type_null` case */ |
| 1751 |
|
✗ |
if (!jso1) |
| 1752 |
|
✗ |
jso = NULL; |
| 1753 |
|
✗ |
else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, |
| 1754 |
|
|
shallow_copy) < 0) |
| 1755 |
|
|
{ |
| 1756 |
|
✗ |
json_object_put(jso); |
| 1757 |
|
✗ |
return -1; |
| 1758 |
|
|
} |
| 1759 |
|
|
|
| 1760 |
|
✗ |
if (json_object_array_add(*dst, jso) < 0) |
| 1761 |
|
|
{ |
| 1762 |
|
✗ |
json_object_put(jso); |
| 1763 |
|
✗ |
return -1; |
| 1764 |
|
|
} |
| 1765 |
|
|
} |
| 1766 |
|
|
break; |
| 1767 |
|
|
|
| 1768 |
|
|
default: |
| 1769 |
|
|
break; |
| 1770 |
|
|
/* else, nothing to do, shallow_copy already did. */ |
| 1771 |
|
|
} |
| 1772 |
|
|
|
| 1773 |
|
✗ |
if (shallow_copy_rc != 2) |
| 1774 |
|
✗ |
return json_object_copy_serializer_data(src, *dst); |
| 1775 |
|
|
|
| 1776 |
|
|
return 0; |
| 1777 |
|
|
} |
| 1778 |
|
|
|
| 1779 |
|
✗ |
int json_object_deep_copy(struct json_object *src, struct json_object **dst, |
| 1780 |
|
|
json_c_shallow_copy_fn *shallow_copy) |
| 1781 |
|
|
{ |
| 1782 |
|
|
int rc; |
| 1783 |
|
|
|
| 1784 |
|
|
/* Check if arguments are sane ; *dst must not point to a non-NULL object */ |
| 1785 |
|
✗ |
if (!src || !dst || *dst) |
| 1786 |
|
|
{ |
| 1787 |
|
✗ |
errno = EINVAL; |
| 1788 |
|
✗ |
return -1; |
| 1789 |
|
|
} |
| 1790 |
|
|
|
| 1791 |
|
✗ |
if (shallow_copy == NULL) |
| 1792 |
|
|
shallow_copy = json_c_shallow_copy_default; |
| 1793 |
|
|
|
| 1794 |
|
✗ |
rc = json_object_deep_copy_recursive(src, NULL, NULL, UINT_MAX, dst, shallow_copy); |
| 1795 |
|
✗ |
if (rc < 0) |
| 1796 |
|
|
{ |
| 1797 |
|
✗ |
json_object_put(*dst); |
| 1798 |
|
✗ |
*dst = NULL; |
| 1799 |
|
|
} |
| 1800 |
|
|
|
| 1801 |
|
|
return rc; |
| 1802 |
|
|
} |
| 1803 |
|
|
|
| 1804 |
|
✗ |
static void json_abort(const char *message) |
| 1805 |
|
|
{ |
| 1806 |
|
✗ |
if (message != NULL) |
| 1807 |
|
✗ |
fprintf(stderr, "json-c aborts with error: %s\n", message); |
| 1808 |
|
✗ |
abort(); |
| 1809 |
|
|
} |
| 1810 |
|
|
|