You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object.c 43 kB

Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
9 years ago
9 years ago
9 years ago
5 years ago
9 years ago
9 years ago
Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
9 years ago
9 years ago
9 years ago
Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64 This is a recent regression since commit 6359b798479d379a3202e02c6a938d9b40c0d856 which added various assert(0) calls (often replacing return-s). With Ming-W64 compiler, json-c build was failing with various errors of the sort: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc': > /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type] > 841 | } > | ^ > In file included from /home/jehan/dev/src/json-c/json_object.c:17: > /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double': > /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=] > 76 | (_assert(#_Expression,__FILE__,__LINE__),0)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert' > 1070 | assert(0); > | ^~~~~~ > /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here > 1072 | case json_type_boolean: > | ^~~~ The problem is that Mingw-w64 does not consider assert() as a noreturn (even assert(0)), because it has to be compatible by Microsoft libraries. See the discussion here: https://sourceforge.net/p/mingw-w64/bugs/306/ Instead let's create a new json_abort() function which is basically just an abort() function with an optional message, for such cases where abortion was non-conditional (using assert() and using the assertion condition as a message here was clearly a misuse of the function). And mark json_abort() as 'noreturn', as well as 'cold' for optimization purpose (this is code we expect to never run, unless there is a bug, that is). Finally let's use this json_abort() instead of previous misused assert() calls.
5 years ago
9 years ago
9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /*
  2. * $Id: json_object.c,v 1.17 2006/07/25 03:24:50 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
  7. *
  8. * This library is free software; you can redistribute it and/or modify
  9. * it under the terms of the MIT license. See COPYING for details.
  10. *
  11. */
  12. #include "config.h"
  13. #include "strerror_override.h"
  14. #include <assert.h>
  15. #include <ctype.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include "debug.h"
  22. #include "printbuf.h"
  23. #include "linkhash.h"
  24. #include "arraylist.h"
  25. #include "json_inttypes.h"
  26. #include "json_object.h"
  27. #include "json_object_private.h"
  28. #include "json_util.h"
  29. #include "math_compat.h"
  30. #include "strdup_compat.h"
  31. #include "snprintf_compat.h"
  32. #if SIZEOF_LONG_LONG != SIZEOF_INT64_T
  33. #error "The long long type isn't 64-bits"
  34. #endif
  35. // Don't define this. It's not thread-safe.
  36. /* #define REFCOUNT_DEBUG 1 */
  37. const char *json_number_chars = "0123456789.+-eE";
  38. const char *json_hex_chars = "0123456789abcdefABCDEF";
  39. static void json_object_generic_delete(struct json_object* jso);
  40. static struct json_object* json_object_new(enum json_type o_type);
  41. static json_object_to_json_string_fn json_object_object_to_json_string;
  42. static json_object_to_json_string_fn json_object_boolean_to_json_string;
  43. static json_object_to_json_string_fn json_object_double_to_json_string_default;
  44. static json_object_to_json_string_fn json_object_int_to_json_string;
  45. static json_object_to_json_string_fn json_object_string_to_json_string;
  46. static json_object_to_json_string_fn json_object_array_to_json_string;
  47. static json_object_to_json_string_fn _json_object_userdata_to_json_string;
  48. /* ref count debugging */
  49. #ifdef REFCOUNT_DEBUG
  50. static struct lh_table *json_object_table;
  51. static void json_object_init(void) __attribute__ ((constructor));
  52. static void json_object_init(void) {
  53. MC_DEBUG("json_object_init: creating object table\n");
  54. json_object_table = lh_kptr_table_new(128, NULL);
  55. }
  56. static void json_object_fini(void) __attribute__ ((destructor));
  57. static void json_object_fini(void)
  58. {
  59. struct lh_entry *ent;
  60. if (MC_GET_DEBUG())
  61. {
  62. if (json_object_table->count)
  63. {
  64. MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
  65. json_object_table->count);
  66. lh_foreach(json_object_table, ent)
  67. {
  68. struct json_object* obj =
  69. (struct json_object*) lh_entry_v(ent);
  70. MC_DEBUG("\t%s:%p\n",
  71. json_type_to_name(obj->o_type), obj);
  72. }
  73. }
  74. }
  75. MC_DEBUG("json_object_fini: freeing object table\n");
  76. lh_table_free(json_object_table);
  77. }
  78. #endif /* REFCOUNT_DEBUG */
  79. /* helper for accessing the optimized string data component in json_object
  80. */
  81. static const char *
  82. get_string_component(const struct json_object *jso)
  83. {
  84. return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
  85. jso->o.c_string.str.data : jso->o.c_string.str.ptr;
  86. }
  87. /* string escaping */
  88. static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
  89. {
  90. int pos = 0, start_offset = 0;
  91. unsigned char c;
  92. while (len--)
  93. {
  94. c = str[pos];
  95. switch(c)
  96. {
  97. case '\b':
  98. case '\n':
  99. case '\r':
  100. case '\t':
  101. case '\f':
  102. case '"':
  103. case '\\':
  104. case '/':
  105. if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
  106. {
  107. pos++;
  108. break;
  109. }
  110. if(pos - start_offset > 0)
  111. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  112. if(c == '\b') printbuf_memappend(pb, "\\b", 2);
  113. else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
  114. else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
  115. else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
  116. else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
  117. else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
  118. else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
  119. else if(c == '/') printbuf_memappend(pb, "\\/", 2);
  120. start_offset = ++pos;
  121. break;
  122. default:
  123. if(c < ' ')
  124. {
  125. char sbuf[7];
  126. if(pos - start_offset > 0)
  127. printbuf_memappend(pb,
  128. str + start_offset,
  129. pos - start_offset);
  130. snprintf(sbuf, sizeof(sbuf),
  131. "\\u00%c%c",
  132. json_hex_chars[c >> 4],
  133. json_hex_chars[c & 0xf]);
  134. printbuf_memappend_fast(pb, sbuf, (int) sizeof(sbuf) - 1);
  135. start_offset = ++pos;
  136. } else
  137. pos++;
  138. }
  139. }
  140. if (pos - start_offset > 0)
  141. printbuf_memappend(pb, str + start_offset, pos - start_offset);
  142. return 0;
  143. }
  144. /* reference counting */
  145. struct json_object* json_object_get(struct json_object *jso)
  146. {
  147. if (!jso) return jso;
  148. // Don't overflow the refcounter.
  149. assert(jso->_ref_count < UINT32_MAX);
  150. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  151. __sync_add_and_fetch(&jso->_ref_count, 1);
  152. #else
  153. ++jso->_ref_count;
  154. #endif
  155. return jso;
  156. }
  157. int json_object_put(struct json_object *jso)
  158. {
  159. if(!jso) return 0;
  160. /* Avoid invalid free and crash explicitly instead of (silently)
  161. * segfaulting.
  162. */
  163. assert(jso->_ref_count > 0);
  164. #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
  165. /* Note: this only allow the refcount to remain correct
  166. * when multiple threads are adjusting it. It is still an error
  167. * for a thread to decrement the refcount if it doesn't "own" it,
  168. * as that can result in the thread that loses the race to 0
  169. * operating on an already-freed object.
  170. */
  171. if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) return 0;
  172. #else
  173. if (--jso->_ref_count > 0) return 0;
  174. #endif
  175. if (jso->_user_delete)
  176. jso->_user_delete(jso, jso->_userdata);
  177. jso->_delete(jso);
  178. return 1;
  179. }
  180. /* generic object construction and destruction parts */
  181. static void json_object_generic_delete(struct json_object* jso)
  182. {
  183. #ifdef REFCOUNT_DEBUG
  184. MC_DEBUG("json_object_delete_%s: %p\n",
  185. json_type_to_name(jso->o_type), jso);
  186. lh_table_delete(json_object_table, jso);
  187. #endif /* REFCOUNT_DEBUG */
  188. printbuf_free(jso->_pb);
  189. free(jso);
  190. }
  191. static struct json_object* json_object_new(enum json_type o_type)
  192. {
  193. struct json_object *jso;
  194. jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
  195. if (!jso)
  196. return NULL;
  197. jso->o_type = o_type;
  198. jso->_ref_count = 1;
  199. jso->_delete = &json_object_generic_delete;
  200. #ifdef REFCOUNT_DEBUG
  201. lh_table_insert(json_object_table, jso, jso);
  202. MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
  203. #endif /* REFCOUNT_DEBUG */
  204. return jso;
  205. }
  206. /* type checking functions */
  207. int json_object_is_type(const struct json_object *jso, enum json_type type)
  208. {
  209. if (!jso)
  210. return (type == json_type_null);
  211. return (jso->o_type == type);
  212. }
  213. enum json_type json_object_get_type(const struct json_object *jso)
  214. {
  215. if (!jso)
  216. return json_type_null;
  217. return jso->o_type;
  218. }
  219. void* json_object_get_userdata(json_object *jso) {
  220. return jso ? jso->_userdata : NULL;
  221. }
  222. void json_object_set_userdata(json_object *jso, void *userdata,
  223. json_object_delete_fn *user_delete)
  224. {
  225. // Can't return failure, so abort if we can't perform the operation.
  226. assert(jso != NULL);
  227. // First, clean up any previously existing user info
  228. if (jso->_user_delete)
  229. jso->_user_delete(jso, jso->_userdata);
  230. jso->_userdata = userdata;
  231. jso->_user_delete = user_delete;
  232. }
  233. /* set a custom conversion to string */
  234. void json_object_set_serializer(json_object *jso,
  235. json_object_to_json_string_fn *to_string_func,
  236. void *userdata,
  237. json_object_delete_fn *user_delete)
  238. {
  239. json_object_set_userdata(jso, userdata, user_delete);
  240. if (to_string_func == NULL)
  241. {
  242. // Reset to the standard serialization function
  243. switch(jso->o_type)
  244. {
  245. case json_type_null:
  246. jso->_to_json_string = NULL;
  247. break;
  248. case json_type_boolean:
  249. jso->_to_json_string = &json_object_boolean_to_json_string;
  250. break;
  251. case json_type_double:
  252. jso->_to_json_string = &json_object_double_to_json_string_default;
  253. break;
  254. case json_type_int:
  255. jso->_to_json_string = &json_object_int_to_json_string;
  256. break;
  257. case json_type_object:
  258. jso->_to_json_string = &json_object_object_to_json_string;
  259. break;
  260. case json_type_array:
  261. jso->_to_json_string = &json_object_array_to_json_string;
  262. break;
  263. case json_type_string:
  264. jso->_to_json_string = &json_object_string_to_json_string;
  265. break;
  266. }
  267. return;
  268. }
  269. jso->_to_json_string = to_string_func;
  270. }
  271. /* extended conversion to string */
  272. const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
  273. {
  274. const char *r = NULL;
  275. size_t s = 0;
  276. if (!jso)
  277. {
  278. s = 4;
  279. r = "null";
  280. }
  281. else if ((jso->_pb) || (jso->_pb = printbuf_new()))
  282. {
  283. printbuf_reset(jso->_pb);
  284. if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
  285. {
  286. s = (size_t)jso->_pb->bpos;
  287. r = jso->_pb->buf;
  288. }
  289. }
  290. if (length)
  291. *length = s;
  292. return r;
  293. }
  294. const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
  295. {
  296. return json_object_to_json_string_length(jso, flags, NULL);
  297. }
  298. /* backwards-compatible conversion to string */
  299. const char* json_object_to_json_string(struct json_object *jso)
  300. {
  301. return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
  302. }
  303. static void indent(struct printbuf *pb, int level, int flags)
  304. {
  305. if (flags & JSON_C_TO_STRING_PRETTY)
  306. {
  307. if (flags & JSON_C_TO_STRING_PRETTY_TAB)
  308. {
  309. printbuf_memset(pb, -1, '\t', level);
  310. }
  311. else
  312. {
  313. printbuf_memset(pb, -1, ' ', level * 2);
  314. }
  315. }
  316. }
  317. /* json_object_object */
  318. static int json_object_object_to_json_string(struct json_object* jso,
  319. struct printbuf *pb,
  320. int level,
  321. int flags)
  322. {
  323. int had_children = 0;
  324. struct json_object_iter iter;
  325. printbuf_strappend(pb, "{" /*}*/);
  326. if (flags & JSON_C_TO_STRING_PRETTY)
  327. printbuf_strappend(pb, "\n");
  328. json_object_object_foreachC(jso, iter)
  329. {
  330. if (had_children)
  331. {
  332. printbuf_strappend(pb, ",");
  333. if (flags & JSON_C_TO_STRING_PRETTY)
  334. printbuf_strappend(pb, "\n");
  335. }
  336. had_children = 1;
  337. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  338. printbuf_strappend(pb, " ");
  339. indent(pb, level+1, flags);
  340. printbuf_strappend(pb, "\"");
  341. json_escape_str(pb, iter.key, strlen(iter.key), flags);
  342. if (flags & JSON_C_TO_STRING_SPACED)
  343. printbuf_strappend(pb, "\": ");
  344. else
  345. printbuf_strappend(pb, "\":");
  346. if(iter.val == NULL)
  347. printbuf_strappend(pb, "null");
  348. else
  349. if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
  350. return -1;
  351. }
  352. if (flags & JSON_C_TO_STRING_PRETTY)
  353. {
  354. if (had_children)
  355. printbuf_strappend(pb, "\n");
  356. indent(pb,level,flags);
  357. }
  358. if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
  359. return printbuf_strappend(pb, /*{*/ " }");
  360. else
  361. return printbuf_strappend(pb, /*{*/ "}");
  362. }
  363. static void json_object_lh_entry_free(struct lh_entry *ent)
  364. {
  365. if (!ent->k_is_constant)
  366. free(lh_entry_k(ent));
  367. json_object_put((struct json_object*)lh_entry_v(ent));
  368. }
  369. static void json_object_object_delete(struct json_object* jso)
  370. {
  371. lh_table_free(jso->o.c_object);
  372. json_object_generic_delete(jso);
  373. }
  374. struct json_object* json_object_new_object(void)
  375. {
  376. struct json_object *jso = json_object_new(json_type_object);
  377. if (!jso)
  378. return NULL;
  379. jso->_delete = &json_object_object_delete;
  380. jso->_to_json_string = &json_object_object_to_json_string;
  381. jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
  382. &json_object_lh_entry_free);
  383. if (!jso->o.c_object)
  384. {
  385. json_object_generic_delete(jso);
  386. errno = ENOMEM;
  387. return NULL;
  388. }
  389. return jso;
  390. }
  391. struct lh_table* json_object_get_object(const struct json_object *jso)
  392. {
  393. if (!jso)
  394. return NULL;
  395. switch(jso->o_type)
  396. {
  397. case json_type_object:
  398. return jso->o.c_object;
  399. default:
  400. return NULL;
  401. }
  402. }
  403. int json_object_object_add_ex(struct json_object* jso,
  404. const char *const key,
  405. struct json_object *const val,
  406. const unsigned opts)
  407. {
  408. struct json_object *existing_value = NULL;
  409. struct lh_entry *existing_entry;
  410. unsigned long hash;
  411. assert(json_object_get_type(jso) == json_type_object);
  412. // We lookup the entry and replace the value, rather than just deleting
  413. // and re-adding it, so the existing key remains valid.
  414. hash = lh_get_hash(jso->o.c_object, (const void *)key);
  415. existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
  416. lh_table_lookup_entry_w_hash(jso->o.c_object,
  417. (const void *)key, hash);
  418. // The caller must avoid creating loops in the object tree, but do a
  419. // quick check anyway to make sure we're not creating a trivial loop.
  420. if (jso == val)
  421. return -1;
  422. if (!existing_entry)
  423. {
  424. const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
  425. (const void *)key : strdup(key);
  426. if (k == NULL)
  427. return -1;
  428. return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
  429. }
  430. existing_value = (json_object *) lh_entry_v(existing_entry);
  431. if (existing_value)
  432. json_object_put(existing_value);
  433. existing_entry->v = val;
  434. return 0;
  435. }
  436. int json_object_object_add(struct json_object* jso, const char *key,
  437. struct json_object *val)
  438. {
  439. return json_object_object_add_ex(jso, key, val, 0);
  440. }
  441. int json_object_object_length(const struct json_object *jso)
  442. {
  443. assert(json_object_get_type(jso) == json_type_object);
  444. return lh_table_length(jso->o.c_object);
  445. }
  446. size_t json_c_object_sizeof(void)
  447. {
  448. return sizeof(struct json_object);
  449. }
  450. struct json_object* json_object_object_get(const struct json_object* jso,
  451. const char *key)
  452. {
  453. struct json_object *result = NULL;
  454. json_object_object_get_ex(jso, key, &result);
  455. return result;
  456. }
  457. json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
  458. struct json_object **value)
  459. {
  460. if (value != NULL)
  461. *value = NULL;
  462. if (NULL == jso)
  463. return 0;
  464. switch(jso->o_type)
  465. {
  466. case json_type_object:
  467. return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
  468. (void**) value);
  469. default:
  470. if (value != NULL)
  471. *value = NULL;
  472. return 0;
  473. }
  474. }
  475. void json_object_object_del(struct json_object* jso, const char *key)
  476. {
  477. assert(json_object_get_type(jso) == json_type_object);
  478. lh_table_delete(jso->o.c_object, key);
  479. }
  480. /* json_object_boolean */
  481. static int json_object_boolean_to_json_string(struct json_object* jso,
  482. struct printbuf *pb,
  483. int level,
  484. int flags)
  485. {
  486. if (jso->o.c_boolean)
  487. return printbuf_strappend(pb, "true");
  488. return printbuf_strappend(pb, "false");
  489. }
  490. struct json_object* json_object_new_boolean(json_bool b)
  491. {
  492. struct json_object *jso = json_object_new(json_type_boolean);
  493. if (!jso)
  494. return NULL;
  495. jso->_to_json_string = &json_object_boolean_to_json_string;
  496. jso->o.c_boolean = b;
  497. return jso;
  498. }
  499. json_bool json_object_get_boolean(const struct json_object *jso)
  500. {
  501. if (!jso)
  502. return 0;
  503. switch(jso->o_type)
  504. {
  505. case json_type_boolean:
  506. return jso->o.c_boolean;
  507. case json_type_int:
  508. switch(jso->o.c_int.cint_type) {
  509. case json_object_int_type_int64:
  510. return (jso->o.c_int.cint.c_int64 != 0);
  511. case json_object_int_type_uint64:
  512. return (jso->o.c_int.cint.c_uint64 != 0);
  513. default:
  514. json_abort("invalid cint_type");
  515. }
  516. case json_type_double:
  517. return (jso->o.c_double != 0);
  518. case json_type_string:
  519. return (jso->o.c_string.len != 0);
  520. default:
  521. return 0;
  522. }
  523. }
  524. int json_object_set_boolean(struct json_object *jso,json_bool new_value){
  525. if (!jso || jso->o_type!=json_type_boolean)
  526. return 0;
  527. jso->o.c_boolean=new_value;
  528. return 1;
  529. }
  530. /* json_object_int */
  531. static int json_object_int_to_json_string(struct json_object* jso,
  532. struct printbuf *pb,
  533. int level,
  534. int flags)
  535. {
  536. /* room for 19 digits, the sign char, and a null term */
  537. char sbuf[21];
  538. if (jso->o.c_int.cint_type == json_object_int_type_int64)
  539. snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int.cint.c_int64);
  540. else
  541. snprintf(sbuf, sizeof(sbuf), "%" PRIu64, jso->o.c_int.cint.c_uint64);
  542. return printbuf_memappend (pb, sbuf, strlen(sbuf));
  543. }
  544. struct json_object* json_object_new_int(int32_t i)
  545. {
  546. struct json_object *jso = json_object_new(json_type_int);
  547. if (!jso)
  548. return NULL;
  549. jso->_to_json_string = &json_object_int_to_json_string;
  550. jso->o.c_int.cint.c_int64 = i;
  551. jso->o.c_int.cint_type = json_object_int_type_int64;
  552. return jso;
  553. }
  554. int32_t json_object_get_int(const struct json_object *jso)
  555. {
  556. int64_t cint64;
  557. enum json_type o_type;
  558. if(!jso) return 0;
  559. o_type = jso->o_type;
  560. if (jso->o.c_int.cint_type == json_object_int_type_int64) {
  561. cint64 = jso->o.c_int.cint.c_int64;
  562. } else {
  563. if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
  564. cint64 = INT64_MAX;
  565. else
  566. cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
  567. }
  568. if (o_type == json_type_string)
  569. {
  570. /*
  571. * Parse strings into 64-bit numbers, then use the
  572. * 64-to-32-bit number handling below.
  573. */
  574. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  575. return 0; /* whoops, it didn't work. */
  576. o_type = json_type_int;
  577. }
  578. switch(o_type) {
  579. case json_type_int:
  580. /* Make sure we return the correct values for out of range numbers. */
  581. if (cint64 <= INT32_MIN)
  582. return INT32_MIN;
  583. if (cint64 >= INT32_MAX)
  584. return INT32_MAX;
  585. return (int32_t) cint64;
  586. case json_type_double:
  587. if (jso->o.c_double <= INT32_MIN)
  588. return INT32_MIN;
  589. if (jso->o.c_double >= INT32_MAX)
  590. return INT32_MAX;
  591. return (int32_t)jso->o.c_double;
  592. case json_type_boolean:
  593. return jso->o.c_boolean;
  594. default:
  595. return 0;
  596. }
  597. }
  598. int json_object_set_int(struct json_object *jso,int new_value){
  599. return json_object_set_int64(jso,(int64_t)new_value);
  600. }
  601. struct json_object* json_object_new_int64(int64_t i)
  602. {
  603. struct json_object *jso = json_object_new(json_type_int);
  604. if (!jso)
  605. return NULL;
  606. jso->_to_json_string = &json_object_int_to_json_string;
  607. jso->o.c_int.cint.c_int64 = i;
  608. jso->o.c_int.cint_type = json_object_int_type_int64;
  609. return jso;
  610. }
  611. struct json_object* json_object_new_uint64(uint64_t i)
  612. {
  613. struct json_object *jso = json_object_new(json_type_int);
  614. if (!jso)
  615. return NULL;
  616. jso->_to_json_string = &json_object_int_to_json_string;
  617. jso->o.c_int.cint.c_uint64 = i;
  618. jso->o.c_int.cint_type = json_object_int_type_uint64;
  619. return jso;
  620. }
  621. int64_t json_object_get_int64(const struct json_object *jso)
  622. {
  623. int64_t cint;
  624. if (!jso)
  625. return 0;
  626. switch(jso->o_type)
  627. {
  628. case json_type_int:
  629. switch(jso->o.c_int.cint_type) {
  630. case json_object_int_type_int64:
  631. return jso->o.c_int.cint.c_int64;
  632. case json_object_int_type_uint64:
  633. if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
  634. return INT64_MAX;
  635. return (int64_t)jso->o.c_int.cint.c_uint64;
  636. default:
  637. json_abort("invalid cint_type");
  638. }
  639. case json_type_double:
  640. // INT64_MAX can't be exactly represented as a double
  641. // so cast to tell the compiler it's ok to round up.
  642. if (jso->o.c_double >= (double)INT64_MAX)
  643. return INT64_MAX;
  644. if (jso->o.c_double <= INT64_MIN)
  645. return INT64_MIN;
  646. return (int64_t)jso->o.c_double;
  647. case json_type_boolean:
  648. return jso->o.c_boolean;
  649. case json_type_string:
  650. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  651. return cint;
  652. /* FALLTHRU */
  653. default:
  654. return 0;
  655. }
  656. }
  657. uint64_t json_object_get_uint64(const struct json_object *jso)
  658. {
  659. uint64_t cuint;
  660. if (!jso)
  661. return 0;
  662. switch(jso->o_type)
  663. {
  664. case json_type_int:
  665. switch(jso->o.c_int.cint_type) {
  666. case json_object_int_type_int64:
  667. if (jso->o.c_int.cint.c_int64 < 0)
  668. return 0;
  669. return (uint64_t)jso->o.c_int.cint.c_int64;
  670. case json_object_int_type_uint64:
  671. return jso->o.c_int.cint.c_uint64;
  672. default:
  673. json_abort("invalid cint_type");
  674. }
  675. case json_type_double:
  676. // UINT64_MAX can't be exactly represented as a double
  677. // so cast to tell the compiler it's ok to round up.
  678. if (jso->o.c_double >= (double)UINT64_MAX)
  679. return UINT64_MAX;
  680. if (jso->o.c_double < 0)
  681. return 0;
  682. return (uint64_t)jso->o.c_double;
  683. case json_type_boolean:
  684. return jso->o.c_boolean;
  685. case json_type_string:
  686. if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
  687. return cuint;
  688. /* FALLTHRU */
  689. default:
  690. return 0;
  691. }
  692. }
  693. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  694. if (!jso || jso->o_type!=json_type_int)
  695. return 0;
  696. jso->o.c_int.cint.c_int64=new_value;
  697. jso->o.c_int.cint_type = json_object_int_type_int64;
  698. return 1;
  699. }
  700. int json_object_set_uint64(struct json_object *jso,uint64_t new_value){
  701. if (!jso || jso->o_type!=json_type_int)
  702. return 0;
  703. jso->o.c_int.cint.c_uint64=new_value;
  704. jso->o.c_int.cint_type = json_object_int_type_uint64;
  705. return 1;
  706. }
  707. int json_object_int_inc(struct json_object *jso, int64_t val) {
  708. if (!jso || jso->o_type != json_type_int)
  709. return 0;
  710. switch(jso->o.c_int.cint_type) {
  711. case json_object_int_type_int64:
  712. if (val > 0 && jso->o.c_int.cint.c_int64 > INT64_MAX - val) {
  713. jso->o.c_int.cint.c_uint64 = (uint64_t)jso->o.c_int.cint.c_int64 + (uint64_t)val;
  714. jso->o.c_int.cint_type = json_object_int_type_uint64;
  715. } else if (val < 0 && jso->o.c_int.cint.c_int64 < INT64_MIN - val) {
  716. jso->o.c_int.cint.c_int64 = INT64_MIN;
  717. } else {
  718. jso->o.c_int.cint.c_int64 += val;
  719. }
  720. return 1;
  721. case json_object_int_type_uint64:
  722. if (val > 0 && jso->o.c_int.cint.c_uint64 > UINT64_MAX - (uint64_t)val) {
  723. jso->o.c_int.cint.c_uint64 = UINT64_MAX;
  724. } else if (val < 0 && jso->o.c_int.cint.c_uint64 < (uint64_t)(-val)) {
  725. jso->o.c_int.cint.c_int64 = (int64_t)jso->o.c_int.cint.c_uint64 + val;
  726. jso->o.c_int.cint_type = json_object_int_type_int64;
  727. } else if (val < 0 && jso->o.c_int.cint.c_uint64 >= (uint64_t)(-val)) {
  728. jso->o.c_int.cint.c_uint64 -= (uint64_t)(-val);
  729. } else {
  730. jso->o.c_int.cint.c_uint64 += val;
  731. }
  732. return 1;
  733. default:
  734. json_abort("invalid cint_type");
  735. }
  736. }
  737. /* json_object_double */
  738. #if defined(HAVE___THREAD)
  739. // i.e. __thread or __declspec(thread)
  740. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  741. #endif
  742. static char *global_serialization_float_format = NULL;
  743. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  744. {
  745. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  746. {
  747. #if defined(HAVE___THREAD)
  748. if (tls_serialization_float_format)
  749. {
  750. free(tls_serialization_float_format);
  751. tls_serialization_float_format = NULL;
  752. }
  753. #endif
  754. if (global_serialization_float_format)
  755. free(global_serialization_float_format);
  756. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  757. }
  758. else if (global_or_thread == JSON_C_OPTION_THREAD)
  759. {
  760. #if defined(HAVE___THREAD)
  761. if (tls_serialization_float_format)
  762. {
  763. free(tls_serialization_float_format);
  764. tls_serialization_float_format = NULL;
  765. }
  766. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  767. #else
  768. _json_c_set_last_err("json_c_set_option: not compiled with __thread support\n");
  769. return -1;
  770. #endif
  771. }
  772. else
  773. {
  774. _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
  775. return -1;
  776. }
  777. return 0;
  778. }
  779. static int json_object_double_to_json_string_format(struct json_object* jso,
  780. struct printbuf *pb,
  781. int level,
  782. int flags,
  783. const char *format)
  784. {
  785. char buf[128], *p, *q;
  786. int size;
  787. /* Although JSON RFC does not support
  788. NaN or Infinity as numeric values
  789. ECMA 262 section 9.8.1 defines
  790. how to handle these cases as strings */
  791. if (isnan(jso->o.c_double))
  792. {
  793. size = snprintf(buf, sizeof(buf), "NaN");
  794. }
  795. else if (isinf(jso->o.c_double))
  796. {
  797. if(jso->o.c_double > 0)
  798. size = snprintf(buf, sizeof(buf), "Infinity");
  799. else
  800. size = snprintf(buf, sizeof(buf), "-Infinity");
  801. }
  802. else
  803. {
  804. const char *std_format = "%.17g";
  805. int format_drops_decimals = 0;
  806. int looks_numeric = 0;
  807. if (!format)
  808. {
  809. #if defined(HAVE___THREAD)
  810. if (tls_serialization_float_format)
  811. format = tls_serialization_float_format;
  812. else
  813. #endif
  814. if (global_serialization_float_format)
  815. format = global_serialization_float_format;
  816. else
  817. format = std_format;
  818. }
  819. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  820. if (size < 0)
  821. return -1;
  822. p = strchr(buf, ',');
  823. if (p)
  824. *p = '.';
  825. else
  826. p = strchr(buf, '.');
  827. if (format == std_format || strstr(format, ".0f") == NULL)
  828. format_drops_decimals = 1;
  829. looks_numeric = /* Looks like *some* kind of number */
  830. isdigit((unsigned char)buf[0]) ||
  831. (size > 1 && buf[0] == '-' && isdigit((unsigned char)buf[1]));
  832. if (size < (int)sizeof(buf) - 2 &&
  833. looks_numeric &&
  834. !p && /* Has no decimal point */
  835. strchr(buf, 'e') == NULL && /* Not scientific notation */
  836. format_drops_decimals)
  837. {
  838. // Ensure it looks like a float, even if snprintf didn't,
  839. // unless a custom format is set to omit the decimal.
  840. strcat(buf, ".0");
  841. size += 2;
  842. }
  843. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  844. {
  845. /* last useful digit, always keep 1 zero */
  846. p++;
  847. for (q=p ; *q ; q++) {
  848. if (*q!='0') p=q;
  849. }
  850. /* drop trailing zeroes */
  851. *(++p) = 0;
  852. size = p-buf;
  853. }
  854. }
  855. // although unlikely, snprintf can fail
  856. if (size < 0)
  857. return -1;
  858. if (size >= (int)sizeof(buf))
  859. // The standard formats are guaranteed not to overrun the buffer,
  860. // but if a custom one happens to do so, just silently truncate.
  861. size = sizeof(buf) - 1;
  862. printbuf_memappend(pb, buf, size);
  863. return size;
  864. }
  865. static int json_object_double_to_json_string_default(struct json_object* jso,
  866. struct printbuf *pb,
  867. int level,
  868. int flags)
  869. {
  870. return json_object_double_to_json_string_format(jso, pb, level, flags,
  871. NULL);
  872. }
  873. int json_object_double_to_json_string(struct json_object* jso,
  874. struct printbuf *pb,
  875. int level,
  876. int flags)
  877. {
  878. return json_object_double_to_json_string_format(jso, pb, level, flags,
  879. (const char *)jso->_userdata);
  880. }
  881. struct json_object* json_object_new_double(double d)
  882. {
  883. struct json_object *jso = json_object_new(json_type_double);
  884. if (!jso)
  885. return NULL;
  886. jso->_to_json_string = &json_object_double_to_json_string_default;
  887. jso->o.c_double = d;
  888. return jso;
  889. }
  890. struct json_object* json_object_new_double_s(double d, const char *ds)
  891. {
  892. char *new_ds;
  893. struct json_object *jso = json_object_new_double(d);
  894. if (!jso)
  895. return NULL;
  896. new_ds = strdup(ds);
  897. if (!new_ds)
  898. {
  899. json_object_generic_delete(jso);
  900. errno = ENOMEM;
  901. return NULL;
  902. }
  903. json_object_set_serializer(jso, _json_object_userdata_to_json_string,
  904. new_ds, json_object_free_userdata);
  905. return jso;
  906. }
  907. /*
  908. * A wrapper around json_object_userdata_to_json_string() used only
  909. * by json_object_new_double_s() just so json_object_set_double() can
  910. * detect when it needs to reset the serializer to the default.
  911. */
  912. static int _json_object_userdata_to_json_string(struct json_object *jso,
  913. struct printbuf *pb, int level, int flags)
  914. {
  915. return json_object_userdata_to_json_string(jso, pb, level, flags);
  916. }
  917. int json_object_userdata_to_json_string(struct json_object *jso,
  918. struct printbuf *pb, int level, int flags)
  919. {
  920. int userdata_len = strlen((const char *)jso->_userdata);
  921. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  922. return userdata_len;
  923. }
  924. void json_object_free_userdata(struct json_object *jso, void *userdata)
  925. {
  926. free(userdata);
  927. }
  928. double json_object_get_double(const struct json_object *jso)
  929. {
  930. double cdouble;
  931. char *errPtr = NULL;
  932. if(!jso) return 0.0;
  933. switch(jso->o_type) {
  934. case json_type_double:
  935. return jso->o.c_double;
  936. case json_type_int:
  937. switch(jso->o.c_int.cint_type) {
  938. case json_object_int_type_int64:
  939. return jso->o.c_int.cint.c_int64;
  940. case json_object_int_type_uint64:
  941. return jso->o.c_int.cint.c_uint64;
  942. default:
  943. json_abort("invalid cint_type");
  944. }
  945. case json_type_boolean:
  946. return jso->o.c_boolean;
  947. case json_type_string:
  948. errno = 0;
  949. cdouble = strtod(get_string_component(jso), &errPtr);
  950. /* if conversion stopped at the first character, return 0.0 */
  951. if (errPtr == get_string_component(jso))
  952. {
  953. errno = EINVAL;
  954. return 0.0;
  955. }
  956. /*
  957. * Check that the conversion terminated on something sensible
  958. *
  959. * For example, { "pay" : 123AB } would parse as 123.
  960. */
  961. if (*errPtr != '\0')
  962. {
  963. errno = EINVAL;
  964. return 0.0;
  965. }
  966. /*
  967. * If strtod encounters a string which would exceed the
  968. * capacity of a double, it returns +/- HUGE_VAL and sets
  969. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  970. * from a conversion, so we need to check errno.
  971. *
  972. * Underflow also sets errno to ERANGE, but it returns 0 in
  973. * that case, which is what we will return anyway.
  974. *
  975. * See CERT guideline ERR30-C
  976. */
  977. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  978. (ERANGE == errno))
  979. cdouble = 0.0;
  980. return cdouble;
  981. default:
  982. errno = EINVAL;
  983. return 0.0;
  984. }
  985. }
  986. int json_object_set_double(struct json_object *jso,double new_value){
  987. if (!jso || jso->o_type!=json_type_double)
  988. return 0;
  989. jso->o.c_double=new_value;
  990. if (jso->_to_json_string == &_json_object_userdata_to_json_string)
  991. json_object_set_serializer(jso, NULL, NULL, NULL);
  992. return 1;
  993. }
  994. /* json_object_string */
  995. static int json_object_string_to_json_string(struct json_object* jso,
  996. struct printbuf *pb,
  997. int level,
  998. int flags)
  999. {
  1000. printbuf_strappend(pb, "\"");
  1001. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  1002. printbuf_strappend(pb, "\"");
  1003. return 0;
  1004. }
  1005. static void json_object_string_delete(struct json_object* jso)
  1006. {
  1007. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  1008. free(jso->o.c_string.str.ptr);
  1009. json_object_generic_delete(jso);
  1010. }
  1011. struct json_object* json_object_new_string(const char *s)
  1012. {
  1013. struct json_object *jso = json_object_new(json_type_string);
  1014. if (!jso)
  1015. return NULL;
  1016. jso->_delete = &json_object_string_delete;
  1017. jso->_to_json_string = &json_object_string_to_json_string;
  1018. jso->o.c_string.len = strlen(s);
  1019. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  1020. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  1021. } else {
  1022. jso->o.c_string.str.ptr = strdup(s);
  1023. if (!jso->o.c_string.str.ptr)
  1024. {
  1025. json_object_generic_delete(jso);
  1026. errno = ENOMEM;
  1027. return NULL;
  1028. }
  1029. }
  1030. return jso;
  1031. }
  1032. struct json_object* json_object_new_string_len(const char *s, const int len)
  1033. {
  1034. char *dstbuf;
  1035. struct json_object *jso = json_object_new(json_type_string);
  1036. if (!jso)
  1037. return NULL;
  1038. jso->_delete = &json_object_string_delete;
  1039. jso->_to_json_string = &json_object_string_to_json_string;
  1040. if(len < LEN_DIRECT_STRING_DATA) {
  1041. dstbuf = jso->o.c_string.str.data;
  1042. } else {
  1043. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  1044. if (!jso->o.c_string.str.ptr)
  1045. {
  1046. json_object_generic_delete(jso);
  1047. errno = ENOMEM;
  1048. return NULL;
  1049. }
  1050. dstbuf = jso->o.c_string.str.ptr;
  1051. }
  1052. memcpy(dstbuf, (const void *)s, len);
  1053. dstbuf[len] = '\0';
  1054. jso->o.c_string.len = len;
  1055. return jso;
  1056. }
  1057. const char* json_object_get_string(struct json_object *jso)
  1058. {
  1059. if (!jso)
  1060. return NULL;
  1061. switch(jso->o_type)
  1062. {
  1063. case json_type_string:
  1064. return get_string_component(jso);
  1065. default:
  1066. return json_object_to_json_string(jso);
  1067. }
  1068. }
  1069. int json_object_get_string_len(const struct json_object *jso)
  1070. {
  1071. if (!jso)
  1072. return 0;
  1073. switch(jso->o_type)
  1074. {
  1075. case json_type_string:
  1076. return jso->o.c_string.len;
  1077. default:
  1078. return 0;
  1079. }
  1080. }
  1081. int json_object_set_string(json_object* jso, const char* s) {
  1082. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  1083. }
  1084. int json_object_set_string_len(json_object* jso, const char* s, int len){
  1085. char *dstbuf;
  1086. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  1087. if (len<LEN_DIRECT_STRING_DATA) {
  1088. dstbuf=jso->o.c_string.str.data;
  1089. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  1090. } else {
  1091. dstbuf=(char *)malloc(len+1);
  1092. if (dstbuf==NULL) return 0;
  1093. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  1094. jso->o.c_string.str.ptr=dstbuf;
  1095. }
  1096. jso->o.c_string.len=len;
  1097. memcpy(dstbuf, (const void *)s, len);
  1098. dstbuf[len] = '\0';
  1099. return 1;
  1100. }
  1101. /* json_object_array */
  1102. static int json_object_array_to_json_string(struct json_object* jso,
  1103. struct printbuf *pb,
  1104. int level,
  1105. int flags)
  1106. {
  1107. int had_children = 0;
  1108. size_t ii;
  1109. printbuf_strappend(pb, "[");
  1110. if (flags & JSON_C_TO_STRING_PRETTY)
  1111. printbuf_strappend(pb, "\n");
  1112. for(ii=0; ii < json_object_array_length(jso); ii++)
  1113. {
  1114. struct json_object *val;
  1115. if (had_children)
  1116. {
  1117. printbuf_strappend(pb, ",");
  1118. if (flags & JSON_C_TO_STRING_PRETTY)
  1119. printbuf_strappend(pb, "\n");
  1120. }
  1121. had_children = 1;
  1122. if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
  1123. printbuf_strappend(pb, " ");
  1124. indent(pb, level + 1, flags);
  1125. val = json_object_array_get_idx(jso, ii);
  1126. if(val == NULL)
  1127. printbuf_strappend(pb, "null");
  1128. else
  1129. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  1130. return -1;
  1131. }
  1132. if (flags & JSON_C_TO_STRING_PRETTY)
  1133. {
  1134. if (had_children)
  1135. printbuf_strappend(pb, "\n");
  1136. indent(pb,level,flags);
  1137. }
  1138. if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
  1139. return printbuf_strappend(pb, " ]");
  1140. return printbuf_strappend(pb, "]");
  1141. }
  1142. static void json_object_array_entry_free(void *data)
  1143. {
  1144. json_object_put((struct json_object*)data);
  1145. }
  1146. static void json_object_array_delete(struct json_object* jso)
  1147. {
  1148. array_list_free(jso->o.c_array);
  1149. json_object_generic_delete(jso);
  1150. }
  1151. struct json_object* json_object_new_array(void)
  1152. {
  1153. struct json_object *jso = json_object_new(json_type_array);
  1154. if (!jso)
  1155. return NULL;
  1156. jso->_delete = &json_object_array_delete;
  1157. jso->_to_json_string = &json_object_array_to_json_string;
  1158. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  1159. if(jso->o.c_array == NULL)
  1160. {
  1161. free(jso);
  1162. return NULL;
  1163. }
  1164. return jso;
  1165. }
  1166. struct array_list* json_object_get_array(const struct json_object *jso)
  1167. {
  1168. if (!jso)
  1169. return NULL;
  1170. switch(jso->o_type)
  1171. {
  1172. case json_type_array:
  1173. return jso->o.c_array;
  1174. default:
  1175. return NULL;
  1176. }
  1177. }
  1178. void json_object_array_sort(struct json_object *jso,
  1179. int(*sort_fn)(const void *, const void *))
  1180. {
  1181. assert(json_object_get_type(jso) == json_type_array);
  1182. array_list_sort(jso->o.c_array, sort_fn);
  1183. }
  1184. struct json_object* json_object_array_bsearch(
  1185. const struct json_object *key,
  1186. const struct json_object *jso,
  1187. int (*sort_fn)(const void *, const void *))
  1188. {
  1189. struct json_object **result;
  1190. assert(json_object_get_type(jso) == json_type_array);
  1191. result = (struct json_object **)array_list_bsearch(
  1192. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1193. if (!result)
  1194. return NULL;
  1195. return *result;
  1196. }
  1197. size_t json_object_array_length(const struct json_object *jso)
  1198. {
  1199. assert(json_object_get_type(jso) == json_type_array);
  1200. return array_list_length(jso->o.c_array);
  1201. }
  1202. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1203. {
  1204. assert(json_object_get_type(jso) == json_type_array);
  1205. return array_list_add(jso->o.c_array, val);
  1206. }
  1207. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1208. struct json_object *val)
  1209. {
  1210. assert(json_object_get_type(jso) == json_type_array);
  1211. return array_list_put_idx(jso->o.c_array, idx, val);
  1212. }
  1213. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1214. {
  1215. assert(json_object_get_type(jso) == json_type_array);
  1216. return array_list_del_idx(jso->o.c_array, idx, count);
  1217. }
  1218. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1219. size_t idx)
  1220. {
  1221. assert(json_object_get_type(jso) == json_type_array);
  1222. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1223. }
  1224. static int json_array_equal(struct json_object* jso1,
  1225. struct json_object* jso2)
  1226. {
  1227. size_t len, i;
  1228. len = json_object_array_length(jso1);
  1229. if (len != json_object_array_length(jso2))
  1230. return 0;
  1231. for (i = 0; i < len; i++) {
  1232. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1233. json_object_array_get_idx(jso2, i)))
  1234. return 0;
  1235. }
  1236. return 1;
  1237. }
  1238. struct json_object* json_object_new_null()
  1239. {
  1240. return NULL;
  1241. }
  1242. static int json_object_all_values_equal(struct json_object* jso1,
  1243. struct json_object* jso2)
  1244. {
  1245. struct json_object_iter iter;
  1246. struct json_object *sub;
  1247. assert(json_object_get_type(jso1) == json_type_object);
  1248. assert(json_object_get_type(jso2) == json_type_object);
  1249. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1250. json_object_object_foreachC(jso1, iter) {
  1251. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1252. (void**)(void *)&sub))
  1253. return 0;
  1254. if (!json_object_equal(iter.val, sub))
  1255. return 0;
  1256. }
  1257. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1258. json_object_object_foreachC(jso2, iter) {
  1259. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1260. (void**)(void *)&sub))
  1261. return 0;
  1262. }
  1263. return 1;
  1264. }
  1265. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1266. {
  1267. if (jso1 == jso2)
  1268. return 1;
  1269. if (!jso1 || !jso2)
  1270. return 0;
  1271. if (jso1->o_type != jso2->o_type)
  1272. return 0;
  1273. switch(jso1->o_type) {
  1274. case json_type_boolean:
  1275. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1276. case json_type_double:
  1277. return (jso1->o.c_double == jso2->o.c_double);
  1278. case json_type_int:
  1279. if (jso1->o.c_int.cint_type == json_object_int_type_int64)
  1280. {
  1281. if (jso2->o.c_int.cint_type == json_object_int_type_int64)
  1282. return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
  1283. if (jso1->o.c_int.cint.c_int64 < 0)
  1284. return 0;
  1285. return ((uint64_t)jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_uint64);
  1286. }
  1287. // else jso1 is a uint64
  1288. if (jso2->o.c_int.cint_type == json_object_int_type_uint64)
  1289. return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
  1290. if (jso2->o.c_int.cint.c_int64 < 0)
  1291. return 0;
  1292. return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
  1293. case json_type_string:
  1294. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1295. memcmp(get_string_component(jso1),
  1296. get_string_component(jso2),
  1297. jso1->o.c_string.len) == 0);
  1298. case json_type_object:
  1299. return json_object_all_values_equal(jso1, jso2);
  1300. case json_type_array:
  1301. return json_array_equal(jso1, jso2);
  1302. case json_type_null:
  1303. return 1;
  1304. };
  1305. return 0;
  1306. }
  1307. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1308. {
  1309. if (!src->_userdata && !src->_user_delete)
  1310. return 0;
  1311. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1312. dst->_to_json_string == _json_object_userdata_to_json_string)
  1313. {
  1314. dst->_userdata = strdup(src->_userdata);
  1315. }
  1316. // else if ... other supported serializers ...
  1317. else
  1318. {
  1319. _json_c_set_last_err("json_object_deep_copy: unable to copy unknown serializer data: %p\n", dst->_to_json_string);
  1320. return -1;
  1321. }
  1322. dst->_user_delete = src->_user_delete;
  1323. return 0;
  1324. }
  1325. /**
  1326. * The default shallow copy implementation. Simply creates a new object of the same
  1327. * type but does *not* copy over _userdata nor retain any custom serializer.
  1328. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1329. * implementation that is aware of how to copy them.
  1330. *
  1331. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1332. */
  1333. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
  1334. {
  1335. switch (src->o_type) {
  1336. case json_type_boolean:
  1337. *dst = json_object_new_boolean(src->o.c_boolean);
  1338. break;
  1339. case json_type_double:
  1340. *dst = json_object_new_double(src->o.c_double);
  1341. break;
  1342. case json_type_int:
  1343. switch(src->o.c_int.cint_type) {
  1344. case json_object_int_type_int64:
  1345. *dst = json_object_new_int64(src->o.c_int.cint.c_int64);
  1346. break;
  1347. case json_object_int_type_uint64:
  1348. *dst = json_object_new_uint64(src->o.c_int.cint.c_uint64);
  1349. break;
  1350. default:
  1351. assert(!"invalid cint_type");
  1352. }
  1353. break;
  1354. case json_type_string:
  1355. *dst = json_object_new_string(get_string_component(src));
  1356. break;
  1357. case json_type_object:
  1358. *dst = json_object_new_object();
  1359. break;
  1360. case json_type_array:
  1361. *dst = json_object_new_array();
  1362. break;
  1363. default:
  1364. errno = EINVAL;
  1365. return -1;
  1366. }
  1367. if (!*dst) {
  1368. errno = ENOMEM;
  1369. return -1;
  1370. }
  1371. (*dst)->_to_json_string = src->_to_json_string;
  1372. // _userdata and _user_delete are copied later
  1373. return 1;
  1374. }
  1375. /*
  1376. * The actual guts of json_object_deep_copy(), with a few additional args
  1377. * needed so we can keep track of where we are within the object tree.
  1378. *
  1379. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1380. */
  1381. static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent, const char *key_in_parent, size_t index_in_parent, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
  1382. {
  1383. struct json_object_iter iter;
  1384. size_t src_array_len, ii;
  1385. int shallow_copy_rc = 0;
  1386. shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
  1387. /* -1=error, 1=object created ok, 2=userdata set */
  1388. if (shallow_copy_rc < 1)
  1389. {
  1390. errno = EINVAL;
  1391. return -1;
  1392. }
  1393. assert(*dst != NULL);
  1394. switch (src->o_type) {
  1395. case json_type_object:
  1396. json_object_object_foreachC(src, iter) {
  1397. struct json_object *jso = NULL;
  1398. /* This handles the `json_type_null` case */
  1399. if (!iter.val)
  1400. jso = NULL;
  1401. else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso, shallow_copy) < 0)
  1402. {
  1403. json_object_put(jso);
  1404. return -1;
  1405. }
  1406. if (json_object_object_add(*dst, iter.key, jso) < 0)
  1407. {
  1408. json_object_put(jso);
  1409. return -1;
  1410. }
  1411. }
  1412. break;
  1413. case json_type_array:
  1414. src_array_len = json_object_array_length(src);
  1415. for (ii = 0; ii < src_array_len; ii++) {
  1416. struct json_object *jso = NULL;
  1417. struct json_object *jso1 = json_object_array_get_idx(src, ii);
  1418. /* This handles the `json_type_null` case */
  1419. if (!jso1)
  1420. jso = NULL;
  1421. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, shallow_copy) < 0)
  1422. {
  1423. json_object_put(jso);
  1424. return -1;
  1425. }
  1426. if (json_object_array_add(*dst, jso) < 0)
  1427. {
  1428. json_object_put(jso);
  1429. return -1;
  1430. }
  1431. }
  1432. break;
  1433. default:
  1434. break;
  1435. /* else, nothing to do, shallow_copy already did. */
  1436. }
  1437. if (shallow_copy_rc != 2)
  1438. return json_object_copy_serializer_data(src, *dst);
  1439. return 0;
  1440. }
  1441. int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
  1442. {
  1443. int rc;
  1444. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1445. if (!src || !dst || *dst) {
  1446. errno = EINVAL;
  1447. return -1;
  1448. }
  1449. if (shallow_copy == NULL)
  1450. shallow_copy = json_c_shallow_copy_default;
  1451. rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
  1452. if (rc < 0) {
  1453. json_object_put(*dst);
  1454. *dst = NULL;
  1455. }
  1456. return rc;
  1457. }