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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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. cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
  566. }
  567. if (o_type == json_type_string)
  568. {
  569. /*
  570. * Parse strings into 64-bit numbers, then use the
  571. * 64-to-32-bit number handling below.
  572. */
  573. if (json_parse_int64(get_string_component(jso), &cint64) != 0)
  574. return 0; /* whoops, it didn't work. */
  575. o_type = json_type_int;
  576. }
  577. switch(o_type) {
  578. case json_type_int:
  579. /* Make sure we return the correct values for out of range numbers. */
  580. if (cint64 <= INT32_MIN)
  581. return INT32_MIN;
  582. if (cint64 >= INT32_MAX)
  583. return INT32_MAX;
  584. return (int32_t) cint64;
  585. case json_type_double:
  586. if (jso->o.c_double <= INT32_MIN)
  587. return INT32_MIN;
  588. if (jso->o.c_double >= INT32_MAX)
  589. return INT32_MAX;
  590. return (int32_t)jso->o.c_double;
  591. case json_type_boolean:
  592. return jso->o.c_boolean;
  593. default:
  594. return 0;
  595. }
  596. }
  597. int json_object_set_int(struct json_object *jso,int new_value){
  598. return json_object_set_int64(jso,(int64_t)new_value);
  599. }
  600. struct json_object* json_object_new_int64(int64_t i)
  601. {
  602. struct json_object *jso = json_object_new(json_type_int);
  603. if (!jso)
  604. return NULL;
  605. jso->_to_json_string = &json_object_int_to_json_string;
  606. jso->o.c_int.cint.c_int64 = i;
  607. jso->o.c_int.cint_type = json_object_int_type_int64;
  608. return jso;
  609. }
  610. struct json_object* json_object_new_uint64(uint64_t i)
  611. {
  612. struct json_object *jso = json_object_new(json_type_int);
  613. if (!jso)
  614. return NULL;
  615. jso->_to_json_string = &json_object_int_to_json_string;
  616. jso->o.c_int.cint.c_uint64 = i;
  617. jso->o.c_int.cint_type = json_object_int_type_uint64;
  618. return jso;
  619. }
  620. int64_t json_object_get_int64(const struct json_object *jso)
  621. {
  622. int64_t cint;
  623. if (!jso)
  624. return 0;
  625. switch(jso->o_type)
  626. {
  627. case json_type_int:
  628. switch(jso->o.c_int.cint_type) {
  629. case json_object_int_type_int64:
  630. return jso->o.c_int.cint.c_int64;
  631. case json_object_int_type_uint64:
  632. if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
  633. return INT64_MAX;
  634. return (int64_t)jso->o.c_int.cint.c_uint64;
  635. default:
  636. json_abort("invalid cint_type");
  637. }
  638. case json_type_double:
  639. // INT64_MAX can't be exactly represented as a double
  640. // so cast to tell the compiler it's ok to round up.
  641. if (jso->o.c_double >= (double)INT64_MAX)
  642. return INT64_MAX;
  643. if (jso->o.c_double <= INT64_MIN)
  644. return INT64_MIN;
  645. return (int64_t)jso->o.c_double;
  646. case json_type_boolean:
  647. return jso->o.c_boolean;
  648. case json_type_string:
  649. if (json_parse_int64(get_string_component(jso), &cint) == 0)
  650. return cint;
  651. /* FALLTHRU */
  652. default:
  653. return 0;
  654. }
  655. }
  656. uint64_t json_object_get_uint64(const struct json_object *jso)
  657. {
  658. uint64_t cuint;
  659. if (!jso)
  660. return 0;
  661. switch(jso->o_type)
  662. {
  663. case json_type_int:
  664. switch(jso->o.c_int.cint_type) {
  665. case json_object_int_type_int64:
  666. if (jso->o.c_int.cint.c_int64 < 0)
  667. return 0;
  668. return (uint64_t)jso->o.c_int.cint.c_int64;
  669. case json_object_int_type_uint64:
  670. return jso->o.c_int.cint.c_uint64;
  671. default:
  672. json_abort("invalid cint_type");
  673. }
  674. case json_type_double:
  675. // UINT64_MAX can't be exactly represented as a double
  676. // so cast to tell the compiler it's ok to round up.
  677. if (jso->o.c_double >= (double)UINT64_MAX)
  678. return UINT64_MAX;
  679. if (jso->o.c_double < 0)
  680. return 0;
  681. return (uint64_t)jso->o.c_double;
  682. case json_type_boolean:
  683. return jso->o.c_boolean;
  684. case json_type_string:
  685. if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
  686. return cuint;
  687. /* FALLTHRU */
  688. default:
  689. return 0;
  690. }
  691. }
  692. int json_object_set_int64(struct json_object *jso,int64_t new_value){
  693. if (!jso || jso->o_type!=json_type_int)
  694. return 0;
  695. jso->o.c_int.cint.c_int64=new_value;
  696. jso->o.c_int.cint_type = json_object_int_type_int64;
  697. return 1;
  698. }
  699. int json_object_set_uint64(struct json_object *jso,uint64_t new_value){
  700. if (!jso || jso->o_type!=json_type_int)
  701. return 0;
  702. jso->o.c_int.cint.c_uint64=new_value;
  703. jso->o.c_int.cint_type = json_object_int_type_uint64;
  704. return 1;
  705. }
  706. int json_object_int_inc(struct json_object *jso, int64_t val) {
  707. if (!jso || jso->o_type != json_type_int)
  708. return 0;
  709. switch(jso->o.c_int.cint_type) {
  710. case json_object_int_type_int64:
  711. if (val > 0 && jso->o.c_int.cint.c_int64 > INT64_MAX - val) {
  712. jso->o.c_int.cint.c_uint64 = (uint64_t)jso->o.c_int.cint.c_int64 + (uint64_t)val;
  713. jso->o.c_int.cint_type = json_object_int_type_uint64;
  714. } else if (val < 0 && jso->o.c_int.cint.c_int64 < INT64_MIN - val) {
  715. jso->o.c_int.cint.c_int64 = INT64_MIN;
  716. } else {
  717. jso->o.c_int.cint.c_int64 += val;
  718. }
  719. return 1;
  720. case json_object_int_type_uint64:
  721. if (val > 0 && jso->o.c_int.cint.c_uint64 > UINT64_MAX - (uint64_t)val) {
  722. jso->o.c_int.cint.c_uint64 = UINT64_MAX;
  723. } else if (val < 0 && jso->o.c_int.cint.c_uint64 < (uint64_t)(-val)) {
  724. jso->o.c_int.cint.c_int64 = (int64_t)jso->o.c_int.cint.c_uint64 + val;
  725. jso->o.c_int.cint_type = json_object_int_type_int64;
  726. } else if (val < 0 && jso->o.c_int.cint.c_uint64 >= (uint64_t)(-val)) {
  727. jso->o.c_int.cint.c_uint64 -= (uint64_t)(-val);
  728. } else {
  729. jso->o.c_int.cint.c_uint64 += val;
  730. }
  731. return 1;
  732. default:
  733. json_abort("invalid cint_type");
  734. }
  735. }
  736. /* json_object_double */
  737. #if defined(HAVE___THREAD)
  738. // i.e. __thread or __declspec(thread)
  739. static SPEC___THREAD char *tls_serialization_float_format = NULL;
  740. #endif
  741. static char *global_serialization_float_format = NULL;
  742. int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
  743. {
  744. if (global_or_thread == JSON_C_OPTION_GLOBAL)
  745. {
  746. #if defined(HAVE___THREAD)
  747. if (tls_serialization_float_format)
  748. {
  749. free(tls_serialization_float_format);
  750. tls_serialization_float_format = NULL;
  751. }
  752. #endif
  753. if (global_serialization_float_format)
  754. free(global_serialization_float_format);
  755. global_serialization_float_format = double_format ? strdup(double_format) : NULL;
  756. }
  757. else if (global_or_thread == JSON_C_OPTION_THREAD)
  758. {
  759. #if defined(HAVE___THREAD)
  760. if (tls_serialization_float_format)
  761. {
  762. free(tls_serialization_float_format);
  763. tls_serialization_float_format = NULL;
  764. }
  765. tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
  766. #else
  767. _json_c_set_last_err("json_c_set_option: not compiled with __thread support\n");
  768. return -1;
  769. #endif
  770. }
  771. else
  772. {
  773. _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
  774. return -1;
  775. }
  776. return 0;
  777. }
  778. static int json_object_double_to_json_string_format(struct json_object* jso,
  779. struct printbuf *pb,
  780. int level,
  781. int flags,
  782. const char *format)
  783. {
  784. char buf[128], *p, *q;
  785. int size;
  786. /* Although JSON RFC does not support
  787. NaN or Infinity as numeric values
  788. ECMA 262 section 9.8.1 defines
  789. how to handle these cases as strings */
  790. if (isnan(jso->o.c_double))
  791. {
  792. size = snprintf(buf, sizeof(buf), "NaN");
  793. }
  794. else if (isinf(jso->o.c_double))
  795. {
  796. if(jso->o.c_double > 0)
  797. size = snprintf(buf, sizeof(buf), "Infinity");
  798. else
  799. size = snprintf(buf, sizeof(buf), "-Infinity");
  800. }
  801. else
  802. {
  803. const char *std_format = "%.17g";
  804. int format_drops_decimals = 0;
  805. int looks_numeric = 0;
  806. if (!format)
  807. {
  808. #if defined(HAVE___THREAD)
  809. if (tls_serialization_float_format)
  810. format = tls_serialization_float_format;
  811. else
  812. #endif
  813. if (global_serialization_float_format)
  814. format = global_serialization_float_format;
  815. else
  816. format = std_format;
  817. }
  818. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  819. if (size < 0)
  820. return -1;
  821. p = strchr(buf, ',');
  822. if (p)
  823. *p = '.';
  824. else
  825. p = strchr(buf, '.');
  826. if (format == std_format || strstr(format, ".0f") == NULL)
  827. format_drops_decimals = 1;
  828. looks_numeric = /* Looks like *some* kind of number */
  829. isdigit((unsigned char)buf[0]) ||
  830. (size > 1 && buf[0] == '-' && isdigit((unsigned char)buf[1]));
  831. if (size < (int)sizeof(buf) - 2 &&
  832. looks_numeric &&
  833. !p && /* Has no decimal point */
  834. strchr(buf, 'e') == NULL && /* Not scientific notation */
  835. format_drops_decimals)
  836. {
  837. // Ensure it looks like a float, even if snprintf didn't,
  838. // unless a custom format is set to omit the decimal.
  839. strcat(buf, ".0");
  840. size += 2;
  841. }
  842. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  843. {
  844. /* last useful digit, always keep 1 zero */
  845. p++;
  846. for (q=p ; *q ; q++) {
  847. if (*q!='0') p=q;
  848. }
  849. /* drop trailing zeroes */
  850. *(++p) = 0;
  851. size = p-buf;
  852. }
  853. }
  854. // although unlikely, snprintf can fail
  855. if (size < 0)
  856. return -1;
  857. if (size >= (int)sizeof(buf))
  858. // The standard formats are guaranteed not to overrun the buffer,
  859. // but if a custom one happens to do so, just silently truncate.
  860. size = sizeof(buf) - 1;
  861. printbuf_memappend(pb, buf, size);
  862. return size;
  863. }
  864. static int json_object_double_to_json_string_default(struct json_object* jso,
  865. struct printbuf *pb,
  866. int level,
  867. int flags)
  868. {
  869. return json_object_double_to_json_string_format(jso, pb, level, flags,
  870. NULL);
  871. }
  872. int json_object_double_to_json_string(struct json_object* jso,
  873. struct printbuf *pb,
  874. int level,
  875. int flags)
  876. {
  877. return json_object_double_to_json_string_format(jso, pb, level, flags,
  878. (const char *)jso->_userdata);
  879. }
  880. struct json_object* json_object_new_double(double d)
  881. {
  882. struct json_object *jso = json_object_new(json_type_double);
  883. if (!jso)
  884. return NULL;
  885. jso->_to_json_string = &json_object_double_to_json_string_default;
  886. jso->o.c_double = d;
  887. return jso;
  888. }
  889. struct json_object* json_object_new_double_s(double d, const char *ds)
  890. {
  891. char *new_ds;
  892. struct json_object *jso = json_object_new_double(d);
  893. if (!jso)
  894. return NULL;
  895. new_ds = strdup(ds);
  896. if (!new_ds)
  897. {
  898. json_object_generic_delete(jso);
  899. errno = ENOMEM;
  900. return NULL;
  901. }
  902. json_object_set_serializer(jso, _json_object_userdata_to_json_string,
  903. new_ds, json_object_free_userdata);
  904. return jso;
  905. }
  906. /*
  907. * A wrapper around json_object_userdata_to_json_string() used only
  908. * by json_object_new_double_s() just so json_object_set_double() can
  909. * detect when it needs to reset the serializer to the default.
  910. */
  911. static int _json_object_userdata_to_json_string(struct json_object *jso,
  912. struct printbuf *pb, int level, int flags)
  913. {
  914. return json_object_userdata_to_json_string(jso, pb, level, flags);
  915. }
  916. int json_object_userdata_to_json_string(struct json_object *jso,
  917. struct printbuf *pb, int level, int flags)
  918. {
  919. int userdata_len = strlen((const char *)jso->_userdata);
  920. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  921. return userdata_len;
  922. }
  923. void json_object_free_userdata(struct json_object *jso, void *userdata)
  924. {
  925. free(userdata);
  926. }
  927. double json_object_get_double(const struct json_object *jso)
  928. {
  929. double cdouble;
  930. char *errPtr = NULL;
  931. if(!jso) return 0.0;
  932. switch(jso->o_type) {
  933. case json_type_double:
  934. return jso->o.c_double;
  935. case json_type_int:
  936. switch(jso->o.c_int.cint_type) {
  937. case json_object_int_type_int64:
  938. return jso->o.c_int.cint.c_int64;
  939. case json_object_int_type_uint64:
  940. return jso->o.c_int.cint.c_uint64;
  941. default:
  942. json_abort("invalid cint_type");
  943. }
  944. case json_type_boolean:
  945. return jso->o.c_boolean;
  946. case json_type_string:
  947. errno = 0;
  948. cdouble = strtod(get_string_component(jso), &errPtr);
  949. /* if conversion stopped at the first character, return 0.0 */
  950. if (errPtr == get_string_component(jso))
  951. {
  952. errno = EINVAL;
  953. return 0.0;
  954. }
  955. /*
  956. * Check that the conversion terminated on something sensible
  957. *
  958. * For example, { "pay" : 123AB } would parse as 123.
  959. */
  960. if (*errPtr != '\0')
  961. {
  962. errno = EINVAL;
  963. return 0.0;
  964. }
  965. /*
  966. * If strtod encounters a string which would exceed the
  967. * capacity of a double, it returns +/- HUGE_VAL and sets
  968. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  969. * from a conversion, so we need to check errno.
  970. *
  971. * Underflow also sets errno to ERANGE, but it returns 0 in
  972. * that case, which is what we will return anyway.
  973. *
  974. * See CERT guideline ERR30-C
  975. */
  976. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  977. (ERANGE == errno))
  978. cdouble = 0.0;
  979. return cdouble;
  980. default:
  981. errno = EINVAL;
  982. return 0.0;
  983. }
  984. }
  985. int json_object_set_double(struct json_object *jso,double new_value){
  986. if (!jso || jso->o_type!=json_type_double)
  987. return 0;
  988. jso->o.c_double=new_value;
  989. if (jso->_to_json_string == &_json_object_userdata_to_json_string)
  990. json_object_set_serializer(jso, NULL, NULL, NULL);
  991. return 1;
  992. }
  993. /* json_object_string */
  994. static int json_object_string_to_json_string(struct json_object* jso,
  995. struct printbuf *pb,
  996. int level,
  997. int flags)
  998. {
  999. printbuf_strappend(pb, "\"");
  1000. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  1001. printbuf_strappend(pb, "\"");
  1002. return 0;
  1003. }
  1004. static void json_object_string_delete(struct json_object* jso)
  1005. {
  1006. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  1007. free(jso->o.c_string.str.ptr);
  1008. json_object_generic_delete(jso);
  1009. }
  1010. struct json_object* json_object_new_string(const char *s)
  1011. {
  1012. struct json_object *jso = json_object_new(json_type_string);
  1013. if (!jso)
  1014. return NULL;
  1015. jso->_delete = &json_object_string_delete;
  1016. jso->_to_json_string = &json_object_string_to_json_string;
  1017. jso->o.c_string.len = strlen(s);
  1018. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  1019. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  1020. } else {
  1021. jso->o.c_string.str.ptr = strdup(s);
  1022. if (!jso->o.c_string.str.ptr)
  1023. {
  1024. json_object_generic_delete(jso);
  1025. errno = ENOMEM;
  1026. return NULL;
  1027. }
  1028. }
  1029. return jso;
  1030. }
  1031. struct json_object* json_object_new_string_len(const char *s, const int len)
  1032. {
  1033. char *dstbuf;
  1034. struct json_object *jso = json_object_new(json_type_string);
  1035. if (!jso)
  1036. return NULL;
  1037. jso->_delete = &json_object_string_delete;
  1038. jso->_to_json_string = &json_object_string_to_json_string;
  1039. if(len < LEN_DIRECT_STRING_DATA) {
  1040. dstbuf = jso->o.c_string.str.data;
  1041. } else {
  1042. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  1043. if (!jso->o.c_string.str.ptr)
  1044. {
  1045. json_object_generic_delete(jso);
  1046. errno = ENOMEM;
  1047. return NULL;
  1048. }
  1049. dstbuf = jso->o.c_string.str.ptr;
  1050. }
  1051. memcpy(dstbuf, (const void *)s, len);
  1052. dstbuf[len] = '\0';
  1053. jso->o.c_string.len = len;
  1054. return jso;
  1055. }
  1056. const char* json_object_get_string(struct json_object *jso)
  1057. {
  1058. if (!jso)
  1059. return NULL;
  1060. switch(jso->o_type)
  1061. {
  1062. case json_type_string:
  1063. return get_string_component(jso);
  1064. default:
  1065. return json_object_to_json_string(jso);
  1066. }
  1067. }
  1068. int json_object_get_string_len(const struct json_object *jso)
  1069. {
  1070. if (!jso)
  1071. return 0;
  1072. switch(jso->o_type)
  1073. {
  1074. case json_type_string:
  1075. return jso->o.c_string.len;
  1076. default:
  1077. return 0;
  1078. }
  1079. }
  1080. int json_object_set_string(json_object* jso, const char* s) {
  1081. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  1082. }
  1083. int json_object_set_string_len(json_object* jso, const char* s, int len){
  1084. char *dstbuf;
  1085. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  1086. if (len<LEN_DIRECT_STRING_DATA) {
  1087. dstbuf=jso->o.c_string.str.data;
  1088. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  1089. } else {
  1090. dstbuf=(char *)malloc(len+1);
  1091. if (dstbuf==NULL) return 0;
  1092. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  1093. jso->o.c_string.str.ptr=dstbuf;
  1094. }
  1095. jso->o.c_string.len=len;
  1096. memcpy(dstbuf, (const void *)s, len);
  1097. dstbuf[len] = '\0';
  1098. return 1;
  1099. }
  1100. /* json_object_array */
  1101. static int json_object_array_to_json_string(struct json_object* jso,
  1102. struct printbuf *pb,
  1103. int level,
  1104. int flags)
  1105. {
  1106. int had_children = 0;
  1107. size_t ii;
  1108. printbuf_strappend(pb, "[");
  1109. if (flags & JSON_C_TO_STRING_PRETTY)
  1110. printbuf_strappend(pb, "\n");
  1111. for(ii=0; ii < json_object_array_length(jso); ii++)
  1112. {
  1113. struct json_object *val;
  1114. if (had_children)
  1115. {
  1116. printbuf_strappend(pb, ",");
  1117. if (flags & JSON_C_TO_STRING_PRETTY)
  1118. printbuf_strappend(pb, "\n");
  1119. }
  1120. had_children = 1;
  1121. if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
  1122. printbuf_strappend(pb, " ");
  1123. indent(pb, level + 1, flags);
  1124. val = json_object_array_get_idx(jso, ii);
  1125. if(val == NULL)
  1126. printbuf_strappend(pb, "null");
  1127. else
  1128. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  1129. return -1;
  1130. }
  1131. if (flags & JSON_C_TO_STRING_PRETTY)
  1132. {
  1133. if (had_children)
  1134. printbuf_strappend(pb, "\n");
  1135. indent(pb,level,flags);
  1136. }
  1137. if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
  1138. return printbuf_strappend(pb, " ]");
  1139. return printbuf_strappend(pb, "]");
  1140. }
  1141. static void json_object_array_entry_free(void *data)
  1142. {
  1143. json_object_put((struct json_object*)data);
  1144. }
  1145. static void json_object_array_delete(struct json_object* jso)
  1146. {
  1147. array_list_free(jso->o.c_array);
  1148. json_object_generic_delete(jso);
  1149. }
  1150. struct json_object* json_object_new_array(void)
  1151. {
  1152. struct json_object *jso = json_object_new(json_type_array);
  1153. if (!jso)
  1154. return NULL;
  1155. jso->_delete = &json_object_array_delete;
  1156. jso->_to_json_string = &json_object_array_to_json_string;
  1157. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  1158. if(jso->o.c_array == NULL)
  1159. {
  1160. free(jso);
  1161. return NULL;
  1162. }
  1163. return jso;
  1164. }
  1165. struct array_list* json_object_get_array(const struct json_object *jso)
  1166. {
  1167. if (!jso)
  1168. return NULL;
  1169. switch(jso->o_type)
  1170. {
  1171. case json_type_array:
  1172. return jso->o.c_array;
  1173. default:
  1174. return NULL;
  1175. }
  1176. }
  1177. void json_object_array_sort(struct json_object *jso,
  1178. int(*sort_fn)(const void *, const void *))
  1179. {
  1180. assert(json_object_get_type(jso) == json_type_array);
  1181. array_list_sort(jso->o.c_array, sort_fn);
  1182. }
  1183. struct json_object* json_object_array_bsearch(
  1184. const struct json_object *key,
  1185. const struct json_object *jso,
  1186. int (*sort_fn)(const void *, const void *))
  1187. {
  1188. struct json_object **result;
  1189. assert(json_object_get_type(jso) == json_type_array);
  1190. result = (struct json_object **)array_list_bsearch(
  1191. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1192. if (!result)
  1193. return NULL;
  1194. return *result;
  1195. }
  1196. size_t json_object_array_length(const struct json_object *jso)
  1197. {
  1198. assert(json_object_get_type(jso) == json_type_array);
  1199. return array_list_length(jso->o.c_array);
  1200. }
  1201. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1202. {
  1203. assert(json_object_get_type(jso) == json_type_array);
  1204. return array_list_add(jso->o.c_array, val);
  1205. }
  1206. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1207. struct json_object *val)
  1208. {
  1209. assert(json_object_get_type(jso) == json_type_array);
  1210. return array_list_put_idx(jso->o.c_array, idx, val);
  1211. }
  1212. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1213. {
  1214. assert(json_object_get_type(jso) == json_type_array);
  1215. return array_list_del_idx(jso->o.c_array, idx, count);
  1216. }
  1217. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1218. size_t idx)
  1219. {
  1220. assert(json_object_get_type(jso) == json_type_array);
  1221. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1222. }
  1223. static int json_array_equal(struct json_object* jso1,
  1224. struct json_object* jso2)
  1225. {
  1226. size_t len, i;
  1227. len = json_object_array_length(jso1);
  1228. if (len != json_object_array_length(jso2))
  1229. return 0;
  1230. for (i = 0; i < len; i++) {
  1231. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1232. json_object_array_get_idx(jso2, i)))
  1233. return 0;
  1234. }
  1235. return 1;
  1236. }
  1237. struct json_object* json_object_new_null()
  1238. {
  1239. return NULL;
  1240. }
  1241. static int json_object_all_values_equal(struct json_object* jso1,
  1242. struct json_object* jso2)
  1243. {
  1244. struct json_object_iter iter;
  1245. struct json_object *sub;
  1246. assert(json_object_get_type(jso1) == json_type_object);
  1247. assert(json_object_get_type(jso2) == json_type_object);
  1248. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1249. json_object_object_foreachC(jso1, iter) {
  1250. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1251. (void**)(void *)&sub))
  1252. return 0;
  1253. if (!json_object_equal(iter.val, sub))
  1254. return 0;
  1255. }
  1256. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1257. json_object_object_foreachC(jso2, iter) {
  1258. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1259. (void**)(void *)&sub))
  1260. return 0;
  1261. }
  1262. return 1;
  1263. }
  1264. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1265. {
  1266. if (jso1 == jso2)
  1267. return 1;
  1268. if (!jso1 || !jso2)
  1269. return 0;
  1270. if (jso1->o_type != jso2->o_type)
  1271. return 0;
  1272. switch(jso1->o_type) {
  1273. case json_type_boolean:
  1274. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1275. case json_type_double:
  1276. return (jso1->o.c_double == jso2->o.c_double);
  1277. case json_type_int:
  1278. if (jso1->o.c_int.cint_type == json_object_int_type_int64)
  1279. {
  1280. if (jso2->o.c_int.cint_type == json_object_int_type_int64)
  1281. return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
  1282. if (jso1->o.c_int.cint.c_int64 < 0)
  1283. return 0;
  1284. return ((uint64_t)jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_uint64);
  1285. }
  1286. // else jso1 is a uint64
  1287. if (jso2->o.c_int.cint_type == json_object_int_type_uint64)
  1288. return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
  1289. if (jso2->o.c_int.cint.c_int64 < 0)
  1290. return 0;
  1291. return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
  1292. case json_type_string:
  1293. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1294. memcmp(get_string_component(jso1),
  1295. get_string_component(jso2),
  1296. jso1->o.c_string.len) == 0);
  1297. case json_type_object:
  1298. return json_object_all_values_equal(jso1, jso2);
  1299. case json_type_array:
  1300. return json_array_equal(jso1, jso2);
  1301. case json_type_null:
  1302. return 1;
  1303. };
  1304. return 0;
  1305. }
  1306. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1307. {
  1308. if (!src->_userdata && !src->_user_delete)
  1309. return 0;
  1310. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1311. dst->_to_json_string == _json_object_userdata_to_json_string)
  1312. {
  1313. dst->_userdata = strdup(src->_userdata);
  1314. }
  1315. // else if ... other supported serializers ...
  1316. else
  1317. {
  1318. _json_c_set_last_err("json_object_deep_copy: unable to copy unknown serializer data: %p\n", dst->_to_json_string);
  1319. return -1;
  1320. }
  1321. dst->_user_delete = src->_user_delete;
  1322. return 0;
  1323. }
  1324. /**
  1325. * The default shallow copy implementation. Simply creates a new object of the same
  1326. * type but does *not* copy over _userdata nor retain any custom serializer.
  1327. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1328. * implementation that is aware of how to copy them.
  1329. *
  1330. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1331. */
  1332. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
  1333. {
  1334. switch (src->o_type) {
  1335. case json_type_boolean:
  1336. *dst = json_object_new_boolean(src->o.c_boolean);
  1337. break;
  1338. case json_type_double:
  1339. *dst = json_object_new_double(src->o.c_double);
  1340. break;
  1341. case json_type_int:
  1342. switch(src->o.c_int.cint_type) {
  1343. case json_object_int_type_int64:
  1344. *dst = json_object_new_int64(src->o.c_int.cint.c_int64);
  1345. break;
  1346. case json_object_int_type_uint64:
  1347. *dst = json_object_new_uint64(src->o.c_int.cint.c_uint64);
  1348. break;
  1349. default:
  1350. assert(!"invalid cint_type");
  1351. }
  1352. break;
  1353. case json_type_string:
  1354. *dst = json_object_new_string(get_string_component(src));
  1355. break;
  1356. case json_type_object:
  1357. *dst = json_object_new_object();
  1358. break;
  1359. case json_type_array:
  1360. *dst = json_object_new_array();
  1361. break;
  1362. default:
  1363. errno = EINVAL;
  1364. return -1;
  1365. }
  1366. if (!*dst) {
  1367. errno = ENOMEM;
  1368. return -1;
  1369. }
  1370. (*dst)->_to_json_string = src->_to_json_string;
  1371. // _userdata and _user_delete are copied later
  1372. return 1;
  1373. }
  1374. /*
  1375. * The actual guts of json_object_deep_copy(), with a few additional args
  1376. * needed so we can keep track of where we are within the object tree.
  1377. *
  1378. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1379. */
  1380. 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)
  1381. {
  1382. struct json_object_iter iter;
  1383. size_t src_array_len, ii;
  1384. int shallow_copy_rc = 0;
  1385. shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
  1386. /* -1=error, 1=object created ok, 2=userdata set */
  1387. if (shallow_copy_rc < 1)
  1388. {
  1389. errno = EINVAL;
  1390. return -1;
  1391. }
  1392. assert(*dst != NULL);
  1393. switch (src->o_type) {
  1394. case json_type_object:
  1395. json_object_object_foreachC(src, iter) {
  1396. struct json_object *jso = NULL;
  1397. /* This handles the `json_type_null` case */
  1398. if (!iter.val)
  1399. jso = NULL;
  1400. else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso, shallow_copy) < 0)
  1401. {
  1402. json_object_put(jso);
  1403. return -1;
  1404. }
  1405. if (json_object_object_add(*dst, iter.key, jso) < 0)
  1406. {
  1407. json_object_put(jso);
  1408. return -1;
  1409. }
  1410. }
  1411. break;
  1412. case json_type_array:
  1413. src_array_len = json_object_array_length(src);
  1414. for (ii = 0; ii < src_array_len; ii++) {
  1415. struct json_object *jso = NULL;
  1416. struct json_object *jso1 = json_object_array_get_idx(src, ii);
  1417. /* This handles the `json_type_null` case */
  1418. if (!jso1)
  1419. jso = NULL;
  1420. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, shallow_copy) < 0)
  1421. {
  1422. json_object_put(jso);
  1423. return -1;
  1424. }
  1425. if (json_object_array_add(*dst, jso) < 0)
  1426. {
  1427. json_object_put(jso);
  1428. return -1;
  1429. }
  1430. }
  1431. break;
  1432. default:
  1433. break;
  1434. /* else, nothing to do, shallow_copy already did. */
  1435. }
  1436. if (shallow_copy_rc != 2)
  1437. return json_object_copy_serializer_data(src, *dst);
  1438. return 0;
  1439. }
  1440. int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
  1441. {
  1442. int rc;
  1443. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1444. if (!src || !dst || *dst) {
  1445. errno = EINVAL;
  1446. return -1;
  1447. }
  1448. if (shallow_copy == NULL)
  1449. shallow_copy = json_c_shallow_copy_default;
  1450. rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
  1451. if (rc < 0) {
  1452. json_object_put(*dst);
  1453. *dst = NULL;
  1454. }
  1455. return rc;
  1456. }