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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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(1, sizeof(struct json_object));
  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. */
  792. if (isnan(jso->o.c_double))
  793. {
  794. size = snprintf(buf, sizeof(buf), "NaN");
  795. }
  796. else if (isinf(jso->o.c_double))
  797. {
  798. if(jso->o.c_double > 0)
  799. size = snprintf(buf, sizeof(buf), "Infinity");
  800. else
  801. size = snprintf(buf, sizeof(buf), "-Infinity");
  802. }
  803. else
  804. {
  805. const char *std_format = "%.17g";
  806. int format_drops_decimals = 0;
  807. int looks_numeric = 0;
  808. if (!format)
  809. {
  810. #if defined(HAVE___THREAD)
  811. if (tls_serialization_float_format)
  812. format = tls_serialization_float_format;
  813. else
  814. #endif
  815. if (global_serialization_float_format)
  816. format = global_serialization_float_format;
  817. else
  818. format = std_format;
  819. }
  820. size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
  821. if (size < 0)
  822. return -1;
  823. p = strchr(buf, ',');
  824. if (p)
  825. *p = '.';
  826. else
  827. p = strchr(buf, '.');
  828. if (format == std_format || strstr(format, ".0f") == NULL)
  829. format_drops_decimals = 1;
  830. looks_numeric = /* Looks like *some* kind of number */
  831. isdigit((unsigned char)buf[0]) ||
  832. (size > 1 && buf[0] == '-' && isdigit((unsigned char)buf[1]));
  833. if (size < (int)sizeof(buf) - 2 &&
  834. looks_numeric &&
  835. !p && /* Has no decimal point */
  836. strchr(buf, 'e') == NULL && /* Not scientific notation */
  837. format_drops_decimals)
  838. {
  839. // Ensure it looks like a float, even if snprintf didn't,
  840. // unless a custom format is set to omit the decimal.
  841. strcat(buf, ".0");
  842. size += 2;
  843. }
  844. if (p && (flags & JSON_C_TO_STRING_NOZERO))
  845. {
  846. /* last useful digit, always keep 1 zero */
  847. p++;
  848. for (q=p ; *q ; q++) {
  849. if (*q!='0') p=q;
  850. }
  851. /* drop trailing zeroes */
  852. *(++p) = 0;
  853. size = p-buf;
  854. }
  855. }
  856. // although unlikely, snprintf can fail
  857. if (size < 0)
  858. return -1;
  859. if (size >= (int)sizeof(buf))
  860. // The standard formats are guaranteed not to overrun the buffer,
  861. // but if a custom one happens to do so, just silently truncate.
  862. size = sizeof(buf) - 1;
  863. printbuf_memappend(pb, buf, size);
  864. return size;
  865. }
  866. static int json_object_double_to_json_string_default(struct json_object* jso,
  867. struct printbuf *pb,
  868. int level,
  869. int flags)
  870. {
  871. return json_object_double_to_json_string_format(jso, pb, level, flags,
  872. NULL);
  873. }
  874. int json_object_double_to_json_string(struct json_object* jso,
  875. struct printbuf *pb,
  876. int level,
  877. int flags)
  878. {
  879. return json_object_double_to_json_string_format(jso, pb, level, flags,
  880. (const char *)jso->_userdata);
  881. }
  882. struct json_object* json_object_new_double(double d)
  883. {
  884. struct json_object *jso = json_object_new(json_type_double);
  885. if (!jso)
  886. return NULL;
  887. jso->_to_json_string = &json_object_double_to_json_string_default;
  888. jso->o.c_double = d;
  889. return jso;
  890. }
  891. struct json_object* json_object_new_double_s(double d, const char *ds)
  892. {
  893. char *new_ds;
  894. struct json_object *jso = json_object_new_double(d);
  895. if (!jso)
  896. return NULL;
  897. new_ds = strdup(ds);
  898. if (!new_ds)
  899. {
  900. json_object_generic_delete(jso);
  901. errno = ENOMEM;
  902. return NULL;
  903. }
  904. json_object_set_serializer(jso, _json_object_userdata_to_json_string,
  905. new_ds, json_object_free_userdata);
  906. return jso;
  907. }
  908. /*
  909. * A wrapper around json_object_userdata_to_json_string() used only
  910. * by json_object_new_double_s() just so json_object_set_double() can
  911. * detect when it needs to reset the serializer to the default.
  912. */
  913. static int _json_object_userdata_to_json_string(struct json_object *jso,
  914. struct printbuf *pb, int level, int flags)
  915. {
  916. return json_object_userdata_to_json_string(jso, pb, level, flags);
  917. }
  918. int json_object_userdata_to_json_string(struct json_object *jso,
  919. struct printbuf *pb, int level, int flags)
  920. {
  921. int userdata_len = strlen((const char *)jso->_userdata);
  922. printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
  923. return userdata_len;
  924. }
  925. void json_object_free_userdata(struct json_object *jso, void *userdata)
  926. {
  927. free(userdata);
  928. }
  929. double json_object_get_double(const struct json_object *jso)
  930. {
  931. double cdouble;
  932. char *errPtr = NULL;
  933. if(!jso) return 0.0;
  934. switch(jso->o_type) {
  935. case json_type_double:
  936. return jso->o.c_double;
  937. case json_type_int:
  938. switch(jso->o.c_int.cint_type) {
  939. case json_object_int_type_int64:
  940. return jso->o.c_int.cint.c_int64;
  941. case json_object_int_type_uint64:
  942. return jso->o.c_int.cint.c_uint64;
  943. default:
  944. json_abort("invalid cint_type");
  945. }
  946. case json_type_boolean:
  947. return jso->o.c_boolean;
  948. case json_type_string:
  949. errno = 0;
  950. cdouble = strtod(get_string_component(jso), &errPtr);
  951. /* if conversion stopped at the first character, return 0.0 */
  952. if (errPtr == get_string_component(jso))
  953. {
  954. errno = EINVAL;
  955. return 0.0;
  956. }
  957. /*
  958. * Check that the conversion terminated on something sensible
  959. *
  960. * For example, { "pay" : 123AB } would parse as 123.
  961. */
  962. if (*errPtr != '\0')
  963. {
  964. errno = EINVAL;
  965. return 0.0;
  966. }
  967. /*
  968. * If strtod encounters a string which would exceed the
  969. * capacity of a double, it returns +/- HUGE_VAL and sets
  970. * errno to ERANGE. But +/- HUGE_VAL is also a valid result
  971. * from a conversion, so we need to check errno.
  972. *
  973. * Underflow also sets errno to ERANGE, but it returns 0 in
  974. * that case, which is what we will return anyway.
  975. *
  976. * See CERT guideline ERR30-C
  977. */
  978. if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
  979. (ERANGE == errno))
  980. cdouble = 0.0;
  981. return cdouble;
  982. default:
  983. errno = EINVAL;
  984. return 0.0;
  985. }
  986. }
  987. int json_object_set_double(struct json_object *jso,double new_value){
  988. if (!jso || jso->o_type!=json_type_double)
  989. return 0;
  990. jso->o.c_double=new_value;
  991. if (jso->_to_json_string == &_json_object_userdata_to_json_string)
  992. json_object_set_serializer(jso, NULL, NULL, NULL);
  993. return 1;
  994. }
  995. /* json_object_string */
  996. static int json_object_string_to_json_string(struct json_object* jso,
  997. struct printbuf *pb,
  998. int level,
  999. int flags)
  1000. {
  1001. printbuf_strappend(pb, "\"");
  1002. json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
  1003. printbuf_strappend(pb, "\"");
  1004. return 0;
  1005. }
  1006. static void json_object_string_delete(struct json_object* jso)
  1007. {
  1008. if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
  1009. free(jso->o.c_string.str.ptr);
  1010. json_object_generic_delete(jso);
  1011. }
  1012. struct json_object* json_object_new_string(const char *s)
  1013. {
  1014. struct json_object *jso = json_object_new(json_type_string);
  1015. if (!jso)
  1016. return NULL;
  1017. jso->_delete = &json_object_string_delete;
  1018. jso->_to_json_string = &json_object_string_to_json_string;
  1019. jso->o.c_string.len = strlen(s);
  1020. if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
  1021. memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
  1022. } else {
  1023. jso->o.c_string.str.ptr = strdup(s);
  1024. if (!jso->o.c_string.str.ptr)
  1025. {
  1026. json_object_generic_delete(jso);
  1027. errno = ENOMEM;
  1028. return NULL;
  1029. }
  1030. }
  1031. return jso;
  1032. }
  1033. struct json_object* json_object_new_string_len(const char *s, const int len)
  1034. {
  1035. char *dstbuf;
  1036. struct json_object *jso = json_object_new(json_type_string);
  1037. if (!jso)
  1038. return NULL;
  1039. jso->_delete = &json_object_string_delete;
  1040. jso->_to_json_string = &json_object_string_to_json_string;
  1041. if(len < LEN_DIRECT_STRING_DATA) {
  1042. dstbuf = jso->o.c_string.str.data;
  1043. } else {
  1044. jso->o.c_string.str.ptr = (char*)malloc(len + 1);
  1045. if (!jso->o.c_string.str.ptr)
  1046. {
  1047. json_object_generic_delete(jso);
  1048. errno = ENOMEM;
  1049. return NULL;
  1050. }
  1051. dstbuf = jso->o.c_string.str.ptr;
  1052. }
  1053. memcpy(dstbuf, (const void *)s, len);
  1054. dstbuf[len] = '\0';
  1055. jso->o.c_string.len = len;
  1056. return jso;
  1057. }
  1058. const char* json_object_get_string(struct json_object *jso)
  1059. {
  1060. if (!jso)
  1061. return NULL;
  1062. switch(jso->o_type)
  1063. {
  1064. case json_type_string:
  1065. return get_string_component(jso);
  1066. default:
  1067. return json_object_to_json_string(jso);
  1068. }
  1069. }
  1070. int json_object_get_string_len(const struct json_object *jso)
  1071. {
  1072. if (!jso)
  1073. return 0;
  1074. switch(jso->o_type)
  1075. {
  1076. case json_type_string:
  1077. return jso->o.c_string.len;
  1078. default:
  1079. return 0;
  1080. }
  1081. }
  1082. int json_object_set_string(json_object* jso, const char* s) {
  1083. return json_object_set_string_len(jso, s, (int)(strlen(s)));
  1084. }
  1085. int json_object_set_string_len(json_object* jso, const char* s, int len){
  1086. char *dstbuf;
  1087. if (jso==NULL || jso->o_type!=json_type_string) return 0;
  1088. if (len<LEN_DIRECT_STRING_DATA) {
  1089. dstbuf=jso->o.c_string.str.data;
  1090. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  1091. } else {
  1092. dstbuf=(char *)malloc(len+1);
  1093. if (dstbuf==NULL) return 0;
  1094. if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
  1095. jso->o.c_string.str.ptr=dstbuf;
  1096. }
  1097. jso->o.c_string.len=len;
  1098. memcpy(dstbuf, (const void *)s, len);
  1099. dstbuf[len] = '\0';
  1100. return 1;
  1101. }
  1102. /* json_object_array */
  1103. static int json_object_array_to_json_string(struct json_object* jso,
  1104. struct printbuf *pb,
  1105. int level,
  1106. int flags)
  1107. {
  1108. int had_children = 0;
  1109. size_t ii;
  1110. printbuf_strappend(pb, "[");
  1111. if (flags & JSON_C_TO_STRING_PRETTY)
  1112. printbuf_strappend(pb, "\n");
  1113. for(ii=0; ii < json_object_array_length(jso); ii++)
  1114. {
  1115. struct json_object *val;
  1116. if (had_children)
  1117. {
  1118. printbuf_strappend(pb, ",");
  1119. if (flags & JSON_C_TO_STRING_PRETTY)
  1120. printbuf_strappend(pb, "\n");
  1121. }
  1122. had_children = 1;
  1123. if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
  1124. printbuf_strappend(pb, " ");
  1125. indent(pb, level + 1, flags);
  1126. val = json_object_array_get_idx(jso, ii);
  1127. if(val == NULL)
  1128. printbuf_strappend(pb, "null");
  1129. else
  1130. if (val->_to_json_string(val, pb, level+1, flags) < 0)
  1131. return -1;
  1132. }
  1133. if (flags & JSON_C_TO_STRING_PRETTY)
  1134. {
  1135. if (had_children)
  1136. printbuf_strappend(pb, "\n");
  1137. indent(pb,level,flags);
  1138. }
  1139. if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
  1140. return printbuf_strappend(pb, " ]");
  1141. return printbuf_strappend(pb, "]");
  1142. }
  1143. static void json_object_array_entry_free(void *data)
  1144. {
  1145. json_object_put((struct json_object*)data);
  1146. }
  1147. static void json_object_array_delete(struct json_object* jso)
  1148. {
  1149. array_list_free(jso->o.c_array);
  1150. json_object_generic_delete(jso);
  1151. }
  1152. struct json_object* json_object_new_array(void)
  1153. {
  1154. struct json_object *jso = json_object_new(json_type_array);
  1155. if (!jso)
  1156. return NULL;
  1157. jso->_delete = &json_object_array_delete;
  1158. jso->_to_json_string = &json_object_array_to_json_string;
  1159. jso->o.c_array = array_list_new(&json_object_array_entry_free);
  1160. if(jso->o.c_array == NULL)
  1161. {
  1162. free(jso);
  1163. return NULL;
  1164. }
  1165. return jso;
  1166. }
  1167. struct array_list* json_object_get_array(const struct json_object *jso)
  1168. {
  1169. if (!jso)
  1170. return NULL;
  1171. switch(jso->o_type)
  1172. {
  1173. case json_type_array:
  1174. return jso->o.c_array;
  1175. default:
  1176. return NULL;
  1177. }
  1178. }
  1179. void json_object_array_sort(struct json_object *jso,
  1180. int(*sort_fn)(const void *, const void *))
  1181. {
  1182. assert(json_object_get_type(jso) == json_type_array);
  1183. array_list_sort(jso->o.c_array, sort_fn);
  1184. }
  1185. struct json_object* json_object_array_bsearch(
  1186. const struct json_object *key,
  1187. const struct json_object *jso,
  1188. int (*sort_fn)(const void *, const void *))
  1189. {
  1190. struct json_object **result;
  1191. assert(json_object_get_type(jso) == json_type_array);
  1192. result = (struct json_object **)array_list_bsearch(
  1193. (const void **)(void *)&key, jso->o.c_array, sort_fn);
  1194. if (!result)
  1195. return NULL;
  1196. return *result;
  1197. }
  1198. size_t json_object_array_length(const struct json_object *jso)
  1199. {
  1200. assert(json_object_get_type(jso) == json_type_array);
  1201. return array_list_length(jso->o.c_array);
  1202. }
  1203. int json_object_array_add(struct json_object *jso,struct json_object *val)
  1204. {
  1205. assert(json_object_get_type(jso) == json_type_array);
  1206. return array_list_add(jso->o.c_array, val);
  1207. }
  1208. int json_object_array_put_idx(struct json_object *jso, size_t idx,
  1209. struct json_object *val)
  1210. {
  1211. assert(json_object_get_type(jso) == json_type_array);
  1212. return array_list_put_idx(jso->o.c_array, idx, val);
  1213. }
  1214. int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
  1215. {
  1216. assert(json_object_get_type(jso) == json_type_array);
  1217. return array_list_del_idx(jso->o.c_array, idx, count);
  1218. }
  1219. struct json_object* json_object_array_get_idx(const struct json_object *jso,
  1220. size_t idx)
  1221. {
  1222. assert(json_object_get_type(jso) == json_type_array);
  1223. return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
  1224. }
  1225. static int json_array_equal(struct json_object* jso1,
  1226. struct json_object* jso2)
  1227. {
  1228. size_t len, i;
  1229. len = json_object_array_length(jso1);
  1230. if (len != json_object_array_length(jso2))
  1231. return 0;
  1232. for (i = 0; i < len; i++) {
  1233. if (!json_object_equal(json_object_array_get_idx(jso1, i),
  1234. json_object_array_get_idx(jso2, i)))
  1235. return 0;
  1236. }
  1237. return 1;
  1238. }
  1239. struct json_object* json_object_new_null()
  1240. {
  1241. return NULL;
  1242. }
  1243. static int json_object_all_values_equal(struct json_object* jso1,
  1244. struct json_object* jso2)
  1245. {
  1246. struct json_object_iter iter;
  1247. struct json_object *sub;
  1248. assert(json_object_get_type(jso1) == json_type_object);
  1249. assert(json_object_get_type(jso2) == json_type_object);
  1250. /* Iterate over jso1 keys and see if they exist and are equal in jso2 */
  1251. json_object_object_foreachC(jso1, iter) {
  1252. if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
  1253. (void**)(void *)&sub))
  1254. return 0;
  1255. if (!json_object_equal(iter.val, sub))
  1256. return 0;
  1257. }
  1258. /* Iterate over jso2 keys to see if any exist that are not in jso1 */
  1259. json_object_object_foreachC(jso2, iter) {
  1260. if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
  1261. (void**)(void *)&sub))
  1262. return 0;
  1263. }
  1264. return 1;
  1265. }
  1266. int json_object_equal(struct json_object* jso1, struct json_object* jso2)
  1267. {
  1268. if (jso1 == jso2)
  1269. return 1;
  1270. if (!jso1 || !jso2)
  1271. return 0;
  1272. if (jso1->o_type != jso2->o_type)
  1273. return 0;
  1274. switch(jso1->o_type) {
  1275. case json_type_boolean:
  1276. return (jso1->o.c_boolean == jso2->o.c_boolean);
  1277. case json_type_double:
  1278. return (jso1->o.c_double == jso2->o.c_double);
  1279. case json_type_int:
  1280. if (jso1->o.c_int.cint_type == json_object_int_type_int64)
  1281. {
  1282. if (jso2->o.c_int.cint_type == json_object_int_type_int64)
  1283. return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
  1284. if (jso1->o.c_int.cint.c_int64 < 0)
  1285. return 0;
  1286. return ((uint64_t)jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_uint64);
  1287. }
  1288. // else jso1 is a uint64
  1289. if (jso2->o.c_int.cint_type == json_object_int_type_uint64)
  1290. return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
  1291. if (jso2->o.c_int.cint.c_int64 < 0)
  1292. return 0;
  1293. return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
  1294. case json_type_string:
  1295. return (jso1->o.c_string.len == jso2->o.c_string.len &&
  1296. memcmp(get_string_component(jso1),
  1297. get_string_component(jso2),
  1298. jso1->o.c_string.len) == 0);
  1299. case json_type_object:
  1300. return json_object_all_values_equal(jso1, jso2);
  1301. case json_type_array:
  1302. return json_array_equal(jso1, jso2);
  1303. case json_type_null:
  1304. return 1;
  1305. };
  1306. return 0;
  1307. }
  1308. static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
  1309. {
  1310. if (!src->_userdata && !src->_user_delete)
  1311. return 0;
  1312. if (dst->_to_json_string == json_object_userdata_to_json_string ||
  1313. dst->_to_json_string == _json_object_userdata_to_json_string)
  1314. {
  1315. dst->_userdata = strdup(src->_userdata);
  1316. }
  1317. // else if ... other supported serializers ...
  1318. else
  1319. {
  1320. _json_c_set_last_err("json_object_deep_copy: unable to copy unknown serializer data: %p\n", dst->_to_json_string);
  1321. return -1;
  1322. }
  1323. dst->_user_delete = src->_user_delete;
  1324. return 0;
  1325. }
  1326. /**
  1327. * The default shallow copy implementation. Simply creates a new object of the same
  1328. * type but does *not* copy over _userdata nor retain any custom serializer.
  1329. * If custom serializers are in use, json_object_deep_copy() must be passed a shallow copy
  1330. * implementation that is aware of how to copy them.
  1331. *
  1332. * This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
  1333. */
  1334. int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
  1335. {
  1336. switch (src->o_type) {
  1337. case json_type_boolean:
  1338. *dst = json_object_new_boolean(src->o.c_boolean);
  1339. break;
  1340. case json_type_double:
  1341. *dst = json_object_new_double(src->o.c_double);
  1342. break;
  1343. case json_type_int:
  1344. switch(src->o.c_int.cint_type) {
  1345. case json_object_int_type_int64:
  1346. *dst = json_object_new_int64(src->o.c_int.cint.c_int64);
  1347. break;
  1348. case json_object_int_type_uint64:
  1349. *dst = json_object_new_uint64(src->o.c_int.cint.c_uint64);
  1350. break;
  1351. default:
  1352. assert(!"invalid cint_type");
  1353. }
  1354. break;
  1355. case json_type_string:
  1356. *dst = json_object_new_string(get_string_component(src));
  1357. break;
  1358. case json_type_object:
  1359. *dst = json_object_new_object();
  1360. break;
  1361. case json_type_array:
  1362. *dst = json_object_new_array();
  1363. break;
  1364. default:
  1365. errno = EINVAL;
  1366. return -1;
  1367. }
  1368. if (!*dst) {
  1369. errno = ENOMEM;
  1370. return -1;
  1371. }
  1372. (*dst)->_to_json_string = src->_to_json_string;
  1373. // _userdata and _user_delete are copied later
  1374. return 1;
  1375. }
  1376. /*
  1377. * The actual guts of json_object_deep_copy(), with a few additional args
  1378. * needed so we can keep track of where we are within the object tree.
  1379. *
  1380. * Note: caller is responsible for freeing *dst if this fails and returns -1.
  1381. */
  1382. 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)
  1383. {
  1384. struct json_object_iter iter;
  1385. size_t src_array_len, ii;
  1386. int shallow_copy_rc = 0;
  1387. shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
  1388. /* -1=error, 1=object created ok, 2=userdata set */
  1389. if (shallow_copy_rc < 1)
  1390. {
  1391. errno = EINVAL;
  1392. return -1;
  1393. }
  1394. assert(*dst != NULL);
  1395. switch (src->o_type) {
  1396. case json_type_object:
  1397. json_object_object_foreachC(src, iter) {
  1398. struct json_object *jso = NULL;
  1399. /* This handles the `json_type_null` case */
  1400. if (!iter.val)
  1401. jso = NULL;
  1402. else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso, shallow_copy) < 0)
  1403. {
  1404. json_object_put(jso);
  1405. return -1;
  1406. }
  1407. if (json_object_object_add(*dst, iter.key, jso) < 0)
  1408. {
  1409. json_object_put(jso);
  1410. return -1;
  1411. }
  1412. }
  1413. break;
  1414. case json_type_array:
  1415. src_array_len = json_object_array_length(src);
  1416. for (ii = 0; ii < src_array_len; ii++) {
  1417. struct json_object *jso = NULL;
  1418. struct json_object *jso1 = json_object_array_get_idx(src, ii);
  1419. /* This handles the `json_type_null` case */
  1420. if (!jso1)
  1421. jso = NULL;
  1422. else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, shallow_copy) < 0)
  1423. {
  1424. json_object_put(jso);
  1425. return -1;
  1426. }
  1427. if (json_object_array_add(*dst, jso) < 0)
  1428. {
  1429. json_object_put(jso);
  1430. return -1;
  1431. }
  1432. }
  1433. break;
  1434. default:
  1435. break;
  1436. /* else, nothing to do, shallow_copy already did. */
  1437. }
  1438. if (shallow_copy_rc != 2)
  1439. return json_object_copy_serializer_data(src, *dst);
  1440. return 0;
  1441. }
  1442. int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
  1443. {
  1444. int rc;
  1445. /* Check if arguments are sane ; *dst must not point to a non-NULL object */
  1446. if (!src || !dst || *dst) {
  1447. errno = EINVAL;
  1448. return -1;
  1449. }
  1450. if (shallow_copy == NULL)
  1451. shallow_copy = json_c_shallow_copy_default;
  1452. rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
  1453. if (rc < 0) {
  1454. json_object_put(*dst);
  1455. *dst = NULL;
  1456. }
  1457. return rc;
  1458. }