1 /* 2 This is the implementation of the C interface to libjson 3 This file may be included in any C++ application, but it will 4 be completely ignored if JSON_LIBRARY isn't defined. The 5 only reason JSON_LIBRARY should be defined is when compiling libjson 6 as a library 7 */ 8 #include "../libjson.h" 9 #ifdef JSON_LIBRARY 10 11 #include "JSONNode.h" 12 #include "JSONWorker.h" 13 #include "JSONValidator.h" 14 #include "JSONStream.h" 15 #include "JSONGlobals.h" 16 #include <stdexcept> //some methods throw exceptions 17 #ifdef JSON_MEMORY_MANAGE 18 #define MANAGER_INSERT(x) json_global(NODE_HANDLER).insert(x) 19 #define MANAGER_STREAM_INSERT(x) json_global(STREAM_HANDLER).insert(x) 20 #else 21 #define MANAGER_INSERT(x) x 22 #define MANAGER_STREAM_INSERT(x) x 23 #endif 24 25 static const json_char * EMPTY_CSTRING(JSON_TEXT("")); 26 27 #ifdef JSON_MEMORY_POOL 28 #include "JSONMemoryPool.h" 29 extern memory_pool<NODEPOOL> json_node_mempool; 30 #endif 31 toCString(const json_string & str)32 inline json_char * toCString(const json_string & str) json_nothrow { 33 const size_t len = (str.length() + 1) * sizeof(json_char); 34 #ifdef JSON_MEMORY_MANAGE 35 return (json_char *)json_global(STRING_HANDLER).insert(std::memcpy(json_malloc<json_char>(len), str.c_str(), len)); 36 #else 37 return (json_char *)std::memcpy(json_malloc<json_char>(len), str.c_str(), len); 38 #endif 39 } 40 alreadyCString(json_char * str)41 inline json_char * alreadyCString(json_char * str) json_nothrow { 42 #ifdef JSON_MEMORY_MANAGE 43 return (json_char *)json_global(STRING_HANDLER).insert(str); 44 #else 45 return str; 46 #endif 47 } 48 49 /* 50 stuff that's in namespace libjson 51 */ json_free(void * str)52 void json_free(void * str){ 53 JSON_ASSERT_SAFE(str, JSON_TEXT("freeing null ptr"), return;); 54 #ifdef JSON_MEMORY_MANAGE 55 json_global(STRING_HANDLER).remove(str); 56 #endif 57 libjson_free<void>(str); 58 } 59 json_delete(JSONNODE * node)60 void json_delete(JSONNODE * node){ 61 JSON_ASSERT_SAFE(node, JSON_TEXT("deleting null ptr"), return;); 62 #ifdef JSON_MEMORY_MANAGE 63 json_global(NODE_HANDLER).remove(node); 64 #endif 65 JSONNode::deleteJSONNode((JSONNode *)node); 66 } 67 68 #ifdef JSON_MEMORY_MANAGE json_free_all(void)69 void json_free_all(void){ 70 json_global(STRING_HANDLER).clear(); 71 } 72 json_delete_all(void)73 void json_delete_all(void){ 74 json_global(NODE_HANDLER).clear(); 75 } 76 #endif 77 78 #ifdef JSON_READ_PRIORITY json_parse(json_const json_char * json)79 JSONNODE * json_parse(json_const json_char * json){ 80 JSON_ASSERT_SAFE(json, JSON_TEXT("null ptr to json_parse"), return 0;); 81 json_try { 82 //use this constructor to simply copy reference instead of copying the temp 83 return MANAGER_INSERT(JSONNode::newJSONNode_Shallow(JSONWorker::parse(TOCONST_CSTR(json)))); 84 } json_catch (std::invalid_argument, (void)0; ) 85 #ifndef JSON_NO_EXCEPTIONS 86 return 0; 87 #endif 88 } 89 json_parse_unformatted(json_const json_char * json)90 JSONNODE * json_parse_unformatted(json_const json_char * json){ 91 JSON_ASSERT_SAFE(json, JSON_TEXT("null ptr to json_parse"), return 0;); 92 json_try { 93 //use this constructor to simply copy reference instead of copying the temp 94 return MANAGER_INSERT(JSONNode::newJSONNode_Shallow(JSONWorker::parse_unformatted(TOCONST_CSTR(json)))); 95 } json_catch(std::invalid_argument, (void)0; ) 96 #ifndef JSON_NO_EXCEPTIONS 97 return 0; 98 #endif 99 } 100 #endif 101 json_strip_white_space(json_const json_char * json)102 json_char * json_strip_white_space(json_const json_char * json){ 103 JSON_ASSERT_SAFE(json, JSON_TEXT("null ptr to json_strip_white_space"), return 0;); 104 return alreadyCString(JSONWorker::RemoveWhiteSpaceAndCommentsC(TOCONST_CSTR(json), false)); 105 } 106 107 #ifdef JSON_VALIDATE 108 #ifdef JSON_DEPRECATED_FUNCTIONS json_validate(json_const json_char * json)109 JSONNODE * json_validate(json_const json_char * json){ 110 JSON_ASSERT_SAFE(json, JSON_TEXT("null ptr to json_validate"), return 0;); 111 if (json_is_valid(json)){ 112 return json_parse(json); 113 } 114 return 0; 115 } 116 #endif json_is_valid(json_const json_char * json)117 json_bool_t json_is_valid(json_const json_char * json){ 118 JSON_ASSERT_SAFE(json, JSON_TEXT("null ptr to json_is_valid"), return (json_bool_t)false;); 119 #ifdef JSON_SECURITY_MAX_STRING_LENGTH 120 if (json_unlikely(json_strlen(json) > JSON_SECURITY_MAX_STRING_LENGTH)){ 121 JSON_FAIL(JSON_TEXT("Exceeding JSON_SECURITY_MAX_STRING_LENGTH")); 122 return false; 123 } 124 #endif 125 json_auto<json_char> s; 126 s.set(JSONWorker::RemoveWhiteSpaceAndCommentsC(json, false)); 127 return (json_bool_t)JSONValidator::isValidRoot(s.ptr); 128 } 129 json_is_valid_unformatted(json_const json_char * json)130 json_bool_t json_is_valid_unformatted(json_const json_char * json){ 131 JSON_ASSERT_SAFE(json, JSON_TEXT("null ptr to json_is_valid_unformatted"), return (json_bool_t)true;); 132 #ifdef JSON_SECURITY_MAX_STRING_LENGTH 133 if (json_unlikely(json_strlen(json) > JSON_SECURITY_MAX_STRING_LENGTH)){ 134 JSON_FAIL(JSON_TEXT("Exceeding JSON_SECURITY_MAX_STRING_LENGTH")); 135 return false; 136 } 137 #endif 138 return (json_bool_t)JSONValidator::isValidRoot(json); 139 } 140 #endif 141 142 #if defined JSON_DEBUG && !defined JSON_STDERROR 143 //When libjson errors, a callback allows the user to know what went wrong json_register_debug_callback(json_error_callback_t callback)144 void json_register_debug_callback(json_error_callback_t callback){ 145 JSONDebug::register_callback(callback); 146 } 147 #endif 148 149 #ifdef JSON_MUTEX_CALLBACKS 150 #ifdef JSON_MUTEX_MANAGE json_register_mutex_callbacks(json_mutex_callback_t lock,json_mutex_callback_t unlock,json_mutex_callback_t destroy,void * manager_lock)151 void json_register_mutex_callbacks(json_mutex_callback_t lock, json_mutex_callback_t unlock, json_mutex_callback_t destroy, void * manager_lock){ 152 JSONNode::register_mutex_callbacks(lock, unlock, manager_lock); 153 JSONNode::register_mutex_destructor(destroy); 154 } 155 156 #else json_register_mutex_callbacks(json_mutex_callback_t lock,json_mutex_callback_t unlock,void * manager_lock)157 void json_register_mutex_callbacks(json_mutex_callback_t lock, json_mutex_callback_t unlock, void * manager_lock){ 158 JSONNode::register_mutex_callbacks(lock, unlock, manager_lock); 159 } 160 #endif 161 json_set_global_mutex(void * mutex)162 void json_set_global_mutex(void * mutex){ 163 JSONNode::set_global_mutex(mutex); 164 } 165 json_set_mutex(JSONNODE * node,void * mutex)166 void json_set_mutex(JSONNODE * node, void * mutex){ 167 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_set_mutex"), return;); 168 ((JSONNode*)node) -> set_mutex(mutex); 169 } 170 json_lock(JSONNODE * node,int threadid)171 void json_lock(JSONNODE * node, int threadid){ 172 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_lock"), return;); 173 ((JSONNode*)node) -> lock(threadid); 174 } 175 json_unlock(JSONNODE * node,int threadid)176 void json_unlock(JSONNODE * node, int threadid){ 177 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_unlock"), return;); 178 ((JSONNode*)node) -> unlock(threadid); 179 } 180 #endif 181 182 #ifdef JSON_MEMORY_CALLBACKS json_register_memory_callbacks(json_malloc_t mal,json_realloc_t real,json_free_t fre)183 void json_register_memory_callbacks(json_malloc_t mal, json_realloc_t real, json_free_t fre){ 184 JSONMemory::registerMemoryCallbacks(mal, real, fre); 185 } 186 #endif 187 188 #ifdef JSON_STREAM json_stream_push(JSONSTREAM * stream,json_const json_char * addendum)189 void json_stream_push(JSONSTREAM * stream, json_const json_char * addendum){ 190 (*((JSONStream*)stream)) << addendum; 191 } 192 json_delete_stream(JSONSTREAM * stream)193 void json_delete_stream(JSONSTREAM * stream){ 194 JSON_ASSERT_SAFE(stream, JSON_TEXT("deleting null ptr"), return;); 195 #ifdef JSON_MEMORY_MANAGE 196 json_global(STREAM_HANDLER).remove(stream); 197 #endif 198 JSONStream::deleteJSONStream((JSONStream *)stream); 199 } 200 json_new_stream(json_stream_callback_t callback,json_stream_e_callback_t e_callback,void * identifier)201 JSONSTREAM * json_new_stream(json_stream_callback_t callback, json_stream_e_callback_t e_callback, void * identifier){ 202 return MANAGER_STREAM_INSERT(JSONStream::newJSONStream(callback, e_callback, identifier)); 203 } 204 json_stream_reset(JSONSTREAM * stream)205 void json_stream_reset(JSONSTREAM * stream){ 206 JSON_ASSERT_SAFE(stream, JSON_TEXT("resetting null ptr"), return;); 207 ((JSONStream*)stream) -> reset(); 208 } 209 #endif 210 211 212 /* 213 stuff that's in class JSONNode 214 */ 215 //ctors json_new_a(json_const json_char * name,json_const json_char * value)216 JSONNODE * json_new_a(json_const json_char * name, json_const json_char * value){ 217 if (!name) name = EMPTY_CSTRING; 218 JSON_ASSERT_SAFE(value, JSON_TEXT("null value to json_new_a"), value = EMPTY_CSTRING;); 219 #ifdef JSON_MEMORY_POOL 220 return MANAGER_INSERT(new((JSONNode*)json_node_mempool.allocate()) JSONNode(TOCONST_CSTR(name), json_string(TOCONST_CSTR(value)))); 221 #elif defined(JSON_MEMORY_CALLBACKS) 222 return MANAGER_INSERT(new(json_malloc<JSONNode>(1)) JSONNode(TOCONST_CSTR(name), json_string(TOCONST_CSTR(value)))); 223 #else 224 return MANAGER_INSERT(new JSONNode(TOCONST_CSTR(name), json_string(TOCONST_CSTR(value)))); 225 #endif 226 } 227 json_new_i(json_const json_char * name,json_int_t value)228 JSONNODE * json_new_i(json_const json_char * name, json_int_t value){ 229 if (!name) name = EMPTY_CSTRING; 230 #ifdef JSON_MEMORY_POOL 231 return MANAGER_INSERT(new((JSONNode*)json_node_mempool.allocate()) JSONNode(TOCONST_CSTR(name), value)); 232 #elif defined(JSON_MEMORY_CALLBACKS) 233 return MANAGER_INSERT(new(json_malloc<JSONNode>(1)) JSONNode(TOCONST_CSTR(name), value)); 234 #else 235 return MANAGER_INSERT(new JSONNode(TOCONST_CSTR(name), value)); 236 #endif 237 } 238 json_new_f(json_const json_char * name,json_number value)239 JSONNODE * json_new_f(json_const json_char * name, json_number value){ 240 if (!name) name = EMPTY_CSTRING; 241 #ifdef JSON_MEMORY_POOL 242 return MANAGER_INSERT(new((JSONNode*)json_node_mempool.allocate()) JSONNode(TOCONST_CSTR(name), value)); 243 #elif defined(JSON_MEMORY_CALLBACKS) 244 return MANAGER_INSERT(new(json_malloc<JSONNode>(1)) JSONNode(TOCONST_CSTR(name), value)); 245 #else 246 return MANAGER_INSERT(new JSONNode(TOCONST_CSTR(name), value)); 247 #endif 248 } 249 json_new_b(json_const json_char * name,json_bool_t value)250 JSONNODE * json_new_b(json_const json_char * name, json_bool_t value){ 251 if (!name) name = EMPTY_CSTRING; 252 #ifdef JSON_MEMORY_POOL 253 return MANAGER_INSERT(new((JSONNode*)json_node_mempool.allocate()) JSONNode(TOCONST_CSTR(name), static_cast<bool>(value))); 254 #elif defined(JSON_MEMORY_CALLBACKS) 255 return MANAGER_INSERT(new(json_malloc<JSONNode>(1)) JSONNode(TOCONST_CSTR(name), static_cast<bool>(value))); 256 #else 257 return MANAGER_INSERT(new JSONNode(TOCONST_CSTR(name), static_cast<bool>(value))); 258 #endif 259 } 260 json_new(char type)261 JSONNODE * json_new(char type){ 262 #ifdef JSON_MEMORY_POOL 263 return MANAGER_INSERT(new((JSONNode*)json_node_mempool.allocate()) JSONNode(type)); 264 #elif defined(JSON_MEMORY_CALLBACKS) 265 return MANAGER_INSERT(new(json_malloc<JSONNode>(1)) JSONNode(type)); 266 #else 267 return MANAGER_INSERT(new JSONNode(type)); 268 #endif 269 } 270 json_copy(json_const JSONNODE * orig)271 JSONNODE * json_copy(json_const JSONNODE * orig){ 272 JSON_ASSERT_SAFE(orig, JSON_TEXT("null orig to json_copy"), return 0;); 273 #ifdef JSON_MEMORY_POOL 274 return MANAGER_INSERT(new((JSONNode*)json_node_mempool.allocate()) JSONNode(*((JSONNode*)orig))); 275 #elif defined(JSON_MEMORY_CALLBACKS) 276 return MANAGER_INSERT(new(json_malloc<JSONNode>(1)) JSONNode(*((JSONNode*)orig))); 277 #else 278 return MANAGER_INSERT(new JSONNode(*((JSONNode*)orig))); 279 #endif 280 } 281 json_duplicate(json_const JSONNODE * orig)282 JSONNODE * json_duplicate(json_const JSONNODE * orig){ 283 JSON_ASSERT_SAFE(orig, JSON_TEXT("null orig to json_duplicate"), return 0;); 284 return MANAGER_INSERT(JSONNode::newJSONNode_Shallow(((JSONNode*)orig) -> duplicate())); 285 } 286 287 //assignment json_set_a(JSONNODE * node,json_const json_char * value)288 void json_set_a(JSONNODE * node, json_const json_char * value){ 289 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_set_a"), return;); 290 JSON_ASSERT_SAFE(value, JSON_TEXT("null value to json_set_a"), value = EMPTY_CSTRING;); 291 *((JSONNode*)node) = json_string(TOCONST_CSTR(value)); 292 } 293 json_set_i(JSONNODE * node,json_int_t value)294 void json_set_i(JSONNODE * node, json_int_t value){ 295 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_set_i"), return;); 296 *((JSONNode*)node) = value; 297 } 298 json_set_f(JSONNODE * node,json_number value)299 void json_set_f(JSONNODE * node, json_number value){ 300 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_set_f"), return;); 301 *((JSONNode*)node) = value; 302 } 303 json_set_b(JSONNODE * node,json_bool_t value)304 void json_set_b(JSONNODE * node, json_bool_t value){ 305 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_set_b"), return;); 306 *((JSONNode*)node) = static_cast<bool>(value); 307 } 308 json_set_n(JSONNODE * node,json_const JSONNODE * orig)309 void json_set_n(JSONNODE * node, json_const JSONNODE * orig){ 310 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_set_n"), return;); 311 JSON_ASSERT_SAFE(orig, JSON_TEXT("null node to json_set_n"), return;); 312 *((JSONNode*)node) = *((JSONNode*)orig); 313 } 314 315 316 //inspectors json_type(json_const JSONNODE * node)317 char json_type(json_const JSONNODE * node){ 318 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_type"), return JSON_NULL;); 319 return ((JSONNode*)node) -> type(); 320 } 321 json_size(json_const JSONNODE * node)322 json_index_t json_size(json_const JSONNODE * node){ 323 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_size"), return 0;); 324 return ((JSONNode*)node) -> size(); 325 } 326 json_empty(json_const JSONNODE * node)327 json_bool_t json_empty(json_const JSONNODE * node){ 328 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_empty"), return true;); 329 return (json_bool_t)(((JSONNode*)node) -> empty()); 330 } 331 json_name(json_const JSONNODE * node)332 json_char * json_name(json_const JSONNODE * node){ 333 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_name"), return toCString(EMPTY_CSTRING);); 334 return toCString(((JSONNode*)node) -> name()); 335 } 336 337 #ifdef JSON_COMMENTS json_get_comment(json_const JSONNODE * node)338 json_char * json_get_comment(json_const JSONNODE * node){ 339 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_get_comment"), return toCString(EMPTY_CSTRING);); 340 return toCString(((JSONNode*)node) -> get_comment()); 341 } 342 #endif 343 json_as_string(json_const JSONNODE * node)344 json_char * json_as_string(json_const JSONNODE * node){ 345 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_as_string"), return toCString(EMPTY_CSTRING);); 346 return toCString(((JSONNode*)node) -> as_string()); 347 //return toCString(static_cast<json_string>(*((JSONNode*)node))); 348 } 349 json_as_int(json_const JSONNODE * node)350 json_int_t json_as_int(json_const JSONNODE * node){ 351 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_as_int"), return 0;); 352 return ((JSONNode*)node) -> as_int(); 353 //return static_cast<json_int_t>(*((JSONNode*)node)); 354 } 355 json_as_float(json_const JSONNODE * node)356 json_number json_as_float(json_const JSONNODE * node){ 357 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_as_float"), return 0.0f;); 358 return ((JSONNode*)node) -> as_float(); 359 //return static_cast<json_number>(*((JSONNode*)node)); 360 } 361 json_as_bool(json_const JSONNODE * node)362 json_bool_t json_as_bool(json_const JSONNODE * node){ 363 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_as_bool"), return false;); 364 return ((JSONNode*)node) -> as_bool(); 365 //return (json_bool_t)static_cast<bool>(*((JSONNode*)node)); 366 } 367 368 #ifdef JSON_CASTABLE json_as_node(json_const JSONNODE * node)369 JSONNODE * json_as_node(json_const JSONNODE * node){ 370 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_as_node"), return 0;); 371 return MANAGER_INSERT(JSONNode::newJSONNode_Shallow(((JSONNode*)node) -> as_node())); 372 } 373 json_as_array(json_const JSONNODE * node)374 JSONNODE * json_as_array(json_const JSONNODE * node){ 375 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_as_array"), return 0;); 376 return MANAGER_INSERT(JSONNode::newJSONNode_Shallow(((JSONNode*)node) -> as_array())); 377 } 378 #endif 379 380 #if defined(JSON_BINARY) || defined(JSON_EXPOSE_BASE64) 381 static void * returnDecode64(const std::string & result, unsigned long * size) json_nothrow json_cold; returnDecode64(const std::string & result,unsigned long * size)382 static void * returnDecode64(const std::string & result, unsigned long * size) json_nothrow { 383 const size_t len = result.length(); 384 if (json_likely(size)) *size = (json_index_t)len; 385 #ifdef JSON_SAFE 386 if (json_unlikely(result.empty())) return 0; 387 #endif 388 #ifdef JSON_MEMORY_MANAGE 389 return json_global(STRING_HANDLER).insert(std::memcpy(json_malloc<char>(len), result.data(), len)); 390 #else 391 return std::memcpy(json_malloc<char>(len), result.data(), len); 392 #endif 393 } 394 #endif 395 396 #ifdef JSON_BINARY json_as_binary(json_const JSONNODE * node,unsigned long * size)397 void * json_as_binary(json_const JSONNODE * node, unsigned long * size){ 398 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_as_binary"), if (size){*size = 0;} return 0;); 399 return returnDecode64(((JSONNode*)node) -> as_binary(), size); 400 401 } 402 #endif 403 404 #ifdef JSON_EXPOSE_BASE64 405 #include "JSON_Base64.h" json_encode64(json_const void * binary,json_index_t bytes)406 json_char * json_encode64(json_const void * binary, json_index_t bytes){ 407 const json_string result(JSONBase64::json_encode64((const unsigned char *)binary, (size_t)bytes)); 408 #ifdef JSON_MEMORY_MANAGE 409 return (json_char*)json_global(STRING_HANDLER).insert((json_char*)std::memcpy(json_malloc<json_char>(result.length() + 1), result.c_str(), (result.length() + 1) * sizeof(json_char))); 410 #else 411 return (json_char*)std::memcpy(json_malloc<json_char>(result.length() + 1), result.c_str(), (result.length() + 1) * sizeof(json_char)); 412 #endif 413 } 414 json_decode64(const json_char * text,unsigned long * size)415 void * json_decode64(const json_char * text, unsigned long * size){ 416 return returnDecode64(JSONBase64::json_decode64(text), size); 417 } 418 #endif 419 420 #ifdef JSON_WRITE_PRIORITY json_write(json_const JSONNODE * node)421 json_char * json_write(json_const JSONNODE * node){ 422 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_write"), return toCString(EMPTY_CSTRING);); 423 return toCString(((JSONNode*)node) -> write()); 424 } 425 json_write_formatted(json_const JSONNODE * node)426 json_char * json_write_formatted(json_const JSONNODE * node){ 427 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_write_formatted"), return toCString(EMPTY_CSTRING);); 428 return toCString(((JSONNode*)node) -> write_formatted()); 429 } 430 #endif 431 432 //modifiers json_set_name(JSONNODE * node,json_const json_char * name)433 void json_set_name(JSONNODE * node, json_const json_char * name){ 434 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_set_name"), return;); 435 JSON_ASSERT_SAFE(name, JSON_TEXT("null name to json_set_name"), name = EMPTY_CSTRING;); 436 ((JSONNode*)node) -> set_name(TOCONST_CSTR(name)); 437 } 438 439 #ifdef JSON_COMMENTS json_set_comment(JSONNODE * node,json_const json_char * comment)440 void json_set_comment(JSONNODE * node, json_const json_char * comment){ 441 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_set_comment"), return;); 442 JSON_ASSERT_SAFE(comment, JSON_TEXT("null name to json_set_comment"), comment = EMPTY_CSTRING;); 443 ((JSONNode*)node) -> set_comment(TOCONST_CSTR(comment)); 444 } 445 #endif 446 json_clear(JSONNODE * node)447 void json_clear(JSONNODE * node){ 448 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_clear"), return;); 449 ((JSONNode*)node) -> clear(); 450 } 451 json_nullify(JSONNODE * node)452 void json_nullify(JSONNODE * node){ 453 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_nullify"), return;); 454 ((JSONNode*)node) -> nullify(); 455 } 456 json_swap(JSONNODE * node,JSONNODE * node2)457 void json_swap(JSONNODE * node, JSONNODE * node2){ 458 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_swap"), return;); 459 JSON_ASSERT_SAFE(node2, JSON_TEXT("null node to json_swap"), return;); 460 ((JSONNode*)node) -> swap(*(JSONNode*)node2); 461 } 462 json_merge(JSONNODE * node,JSONNODE * node2)463 void json_merge(JSONNODE * node, JSONNODE * node2){ 464 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_merge"), return;); 465 JSON_ASSERT_SAFE(node2, JSON_TEXT("null node to json_merge"), return;); 466 ((JSONNode*)node) -> merge(*(JSONNode*)node2); 467 } 468 469 #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY) json_preparse(JSONNODE * node)470 void json_preparse(JSONNODE * node){ 471 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_preparse"), return;); 472 ((JSONNode*)node) -> preparse(); 473 } 474 #endif 475 476 #ifdef JSON_BINARY json_set_binary(JSONNODE * node,json_const void * data,unsigned long length)477 void json_set_binary(JSONNODE * node, json_const void * data, unsigned long length){ 478 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_swap"), return;); 479 JSON_ASSERT_SAFE(data, JSON_TEXT("null data to json_set_binary"), *((JSONNode*)node) = EMPTY_CSTRING; return;); 480 ((JSONNode*)node) -> set_binary((unsigned char *)data, (size_t)length); 481 } 482 #endif 483 484 #ifdef JSON_CASTABLE json_cast(JSONNODE * node,char type)485 void json_cast(JSONNODE * node, char type){ 486 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_cast"), return;); 487 ((JSONNode*)node) -> cast(type); 488 } 489 #endif 490 491 //children access json_reserve(JSONNODE * node,json_index_t siz)492 void json_reserve(JSONNODE * node, json_index_t siz){ 493 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_reserve"), return;); 494 ((JSONNode*)node) -> reserve(siz); 495 } 496 json_at(JSONNODE * node,unsigned int pos)497 JSONNODE * json_at(JSONNODE * node, unsigned int pos){ 498 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_at"), return 0;); 499 json_try { 500 return &((JSONNode*)node) -> at(pos); 501 } json_catch (std::out_of_range, (void)0; ) 502 #ifndef JSON_NO_EXCEPTIONS 503 return 0; 504 #endif 505 } 506 json_get(JSONNODE * node,json_const json_char * name)507 JSONNODE * json_get(JSONNODE * node, json_const json_char * name){ 508 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_get"), return 0;); 509 JSON_ASSERT_SAFE(name, JSON_TEXT("null node to json_get. Did you mean to use json_at?"), return 0;); 510 json_try { 511 return &((JSONNode*)node) -> at(TOCONST_CSTR(name)); 512 } json_catch (std::out_of_range, (void)0; ) 513 #ifndef JSON_NO_EXCEPTIONS 514 return 0; 515 #endif 516 } 517 518 519 #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS json_get_nocase(JSONNODE * node,json_const json_char * name)520 JSONNODE * json_get_nocase(JSONNODE * node, json_const json_char * name){ 521 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_at_nocase"), return 0;); 522 JSON_ASSERT_SAFE(name, JSON_TEXT("null name to json_at_nocase"), return 0;); 523 json_try { 524 return &((JSONNode*)node) -> at_nocase(TOCONST_CSTR(name)); 525 } json_catch (std::out_of_range, (void)0; ) 526 #ifndef JSON_NO_EXCEPTIONS 527 return 0; 528 #endif 529 } 530 json_pop_back_nocase(JSONNODE * node,json_const json_char * name)531 JSONNODE * json_pop_back_nocase(JSONNODE * node, json_const json_char * name){ 532 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_pop_back_nocase"), return 0;); 533 JSON_ASSERT_SAFE(name, JSON_TEXT("null name to json_pop_back_nocase"), return 0;); 534 return MANAGER_INSERT(((JSONNode*)node) -> pop_back_nocase(TOCONST_CSTR(name))); 535 } 536 #endif 537 json_push_back(JSONNODE * node,JSONNODE * node2)538 void json_push_back(JSONNODE * node, JSONNODE * node2){ 539 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_push_back"), return;); 540 JSON_ASSERT_SAFE(node2, JSON_TEXT("null node2 to json_push_back"), return;); 541 #ifdef JSON_MEMORY_MANAGE 542 json_global(NODE_HANDLER).remove(node2); 543 #endif 544 ((JSONNode*)node) -> push_back((JSONNode*)node2); 545 } 546 json_pop_back_at(JSONNODE * node,unsigned int pos)547 JSONNODE * json_pop_back_at(JSONNODE * node, unsigned int pos){ 548 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_pop_back_i"), return 0;); 549 return MANAGER_INSERT(((JSONNode*)node) -> pop_back(pos)); 550 } 551 json_pop_back(JSONNODE * node,json_const json_char * name)552 JSONNODE * json_pop_back(JSONNODE * node, json_const json_char * name){ 553 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_pop_back"), return 0;); 554 JSON_ASSERT_SAFE(name, JSON_TEXT("null name to json_pop_back. Did you mean to use json_pop_back_at?"), return 0;); 555 return MANAGER_INSERT(((JSONNode*)node) -> pop_back(TOCONST_CSTR(name))); 556 } 557 558 #ifdef JSON_ITERATORS json_find(JSONNODE * node,json_const json_char * name)559 JSONNODE_ITERATOR json_find(JSONNODE * node, json_const json_char * name){ 560 return (JSONNODE_ITERATOR)(((JSONNode*)node) -> find(TOCONST_CSTR(name))); 561 } 562 563 #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS json_find_nocase(JSONNODE * node,json_const json_char * name)564 JSONNODE_ITERATOR json_find_nocase(JSONNODE * node, json_const json_char * name){ 565 return (JSONNODE_ITERATOR)(((JSONNode*)node) -> find_nocase(TOCONST_CSTR(name))); 566 } 567 #endif 568 json_erase(JSONNODE * node,JSONNODE_ITERATOR it)569 JSONNODE_ITERATOR json_erase(JSONNODE * node, JSONNODE_ITERATOR it){ 570 return (JSONNODE_ITERATOR)(((JSONNode*)node) -> erase((JSONNode**)it)); 571 } 572 json_erase_multi(JSONNODE * node,JSONNODE_ITERATOR start,JSONNODE_ITERATOR end)573 JSONNODE_ITERATOR json_erase_multi(JSONNODE * node, JSONNODE_ITERATOR start, JSONNODE_ITERATOR end){ 574 return (JSONNODE_ITERATOR)(((JSONNode*)node) -> erase((JSONNode**)start, (JSONNode**)end)); 575 } 576 json_insert(JSONNODE * node,JSONNODE_ITERATOR it,JSONNODE * node2)577 JSONNODE_ITERATOR json_insert(JSONNODE * node, JSONNODE_ITERATOR it, JSONNODE * node2){ 578 #ifdef JSON_MEMORY_MANAGE 579 json_global(NODE_HANDLER).remove(node2); 580 #endif 581 return (JSONNODE_ITERATOR)(((JSONNode*)node) -> insert((JSONNode**)it, (JSONNode*)node2)); 582 } 583 json_insert_multi(JSONNODE * node,JSONNODE_ITERATOR it,JSONNODE_ITERATOR start,JSONNODE_ITERATOR end)584 JSONNODE_ITERATOR json_insert_multi(JSONNODE * node, JSONNODE_ITERATOR it, JSONNODE_ITERATOR start, JSONNODE_ITERATOR end){ 585 return (JSONNODE_ITERATOR)(((JSONNode*)node) -> insert((JSONNode**)it, (JSONNode**)start, (JSONNode**)end)); 586 } 587 588 //iterator functions json_begin(JSONNODE * node)589 JSONNODE_ITERATOR json_begin(JSONNODE * node){ 590 return (JSONNODE_ITERATOR)(((JSONNode*)node) -> begin()); 591 } 592 json_end(JSONNODE * node)593 JSONNODE_ITERATOR json_end(JSONNODE * node){ 594 return (JSONNODE_ITERATOR)(((JSONNode*)node) -> end()); 595 } 596 #endif 597 598 //comparison json_equal(JSONNODE * node,JSONNODE * node2)599 json_bool_t json_equal(JSONNODE * node, JSONNODE * node2){ 600 JSON_ASSERT_SAFE(node, JSON_TEXT("null node to json_equal"), return false;); 601 JSON_ASSERT_SAFE(node2, JSON_TEXT("null node2 to json_equal"), return false;); 602 return (json_bool_t)(*((JSONNode*)node) == *((JSONNode*)node2)); 603 } 604 605 #endif //JSON_LIBRARY 606