libjson.cpp 23 KB

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