json.cpp 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822
  1. /*Software License
  2. Copyright(C) 2024[liuyingjie]
  3. License Terms
  4. Usage Rights
  5. Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code.
  6. If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license.
  7. Future Versions Updates
  8. From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version.
  9. Users must comply with the terms of the new license issued in future releases.
  10. Liability and Disclaimer
  11. This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
  12. Contact Information
  13. If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com.
  14. */
  15. #include "util/json.h"
  16. #include <assert.h>
  17. #include <fstream>
  18. #include "util/mem.h"
  19. #include "base/exception.h"
  20. #ifndef PP
  21. #if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
  22. #define _CRT_SECURE_NO_DEPRECATE
  23. #endif
  24. #ifdef __GNUC__
  25. #pragma GCC visibility push(default)
  26. #endif
  27. #if defined(_MSC_VER)
  28. #pragma warning (push)
  29. /* disable warning about single line comments in system headers */
  30. #pragma warning (disable : 4001)
  31. #endif
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <math.h>
  35. #include <stdlib.h>
  36. #include <limits.h>
  37. #include <ctype.h>
  38. #include <float.h>
  39. #ifdef ENABLE_LOCALES
  40. #include <locale.h>
  41. #endif
  42. #if defined(_MSC_VER)
  43. #pragma warning (pop)
  44. #endif
  45. #ifdef __GNUC__
  46. #pragma GCC visibility pop
  47. #endif
  48. /* define our own boolean type */
  49. #ifdef true
  50. #undef true
  51. #endif
  52. #define true ((cJSON_bool)1)
  53. #ifdef false
  54. #undef false
  55. #endif
  56. #define false ((cJSON_bool)0)
  57. /* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */
  58. #ifndef isinf
  59. #define isinf(d) (isnan((d - d)) && !isnan(d))
  60. #endif
  61. #ifndef isnan
  62. #define isnan(d) (d != d)
  63. #endif
  64. #ifndef NAN
  65. #ifdef _WIN32
  66. #define NAN sqrt(-1.0)
  67. #else
  68. #define NAN 0.0/0.0
  69. #endif
  70. #endif
  71. typedef struct {
  72. const unsigned char* json;
  73. size_t position;
  74. } error__;
  75. static error__ global_error = { NULL, 0 };
  76. const char* cJSON_GetErrorPtr(void)
  77. {
  78. return (const char*)(global_error.json + global_error.position);
  79. }
  80. char* cJSON_GetStringValue(const cJSON* const item)
  81. {
  82. if (!cJSON_IsString(item))
  83. {
  84. return NULL;
  85. }
  86. return item->valuestring;
  87. }
  88. double cJSON_GetNumberValue(const cJSON* const item)
  89. {
  90. if (!cJSON_IsNumber(item))
  91. {
  92. return (double)NAN;
  93. }
  94. return item->valuedouble;
  95. }
  96. /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
  97. #if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 15)
  98. #error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
  99. #endif
  100. /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
  101. static int case_insensitive_strcmp(const unsigned char* string1, const unsigned char* string2)
  102. {
  103. if ((string1 == NULL) || (string2 == NULL))
  104. {
  105. return 1;
  106. }
  107. if (string1 == string2)
  108. {
  109. return 0;
  110. }
  111. for (; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
  112. {
  113. if (*string1 == '\0')
  114. {
  115. return 0;
  116. }
  117. }
  118. return tolower(*string1) - tolower(*string2);
  119. }
  120. typedef struct internal_hooks
  121. {
  122. void* (*allocate)(size_t size);
  123. void (*deallocate)(void* pointer);
  124. void* (*reallocate)(void* pointer, size_t size);
  125. } internal_hooks;
  126. #if defined(_MSC_VER)
  127. /* work around MSVC error C2322: '...' address of dllimport '...' is not static */
  128. static void* internal_malloc(size_t size)
  129. {
  130. return ylib::mem::malloc(size);
  131. }
  132. static void internal_free(void* pointer)
  133. {
  134. ylib::mem::free(pointer);
  135. }
  136. static void* internal_realloc(void* pointer, size_t size)
  137. {
  138. return ylib::mem::realloc(pointer, size);
  139. }
  140. #else
  141. #define internal_malloc mem::malloc
  142. #define internal_free mem::free
  143. #define internal_realloc mem::realloc
  144. #endif
  145. /* strlen of character literals resolved at compile time */
  146. #define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
  147. static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc };
  148. static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks* const hooks)
  149. {
  150. size_t length = 0;
  151. unsigned char* copy = NULL;
  152. if (string == NULL)
  153. {
  154. return NULL;
  155. }
  156. length = strlen((const char*)string) + sizeof("");
  157. copy = (unsigned char*)hooks->allocate(length);
  158. if (copy == NULL)
  159. {
  160. return NULL;
  161. }
  162. memcpy(copy, string, length);
  163. return copy;
  164. }
  165. void cJSON_InitHooks(cJSON_Hooks* hooks)
  166. {
  167. if (hooks == NULL)
  168. {
  169. /* Reset hooks */
  170. global_hooks.allocate = ylib::mem::malloc;
  171. global_hooks.deallocate = ylib::mem::free;
  172. global_hooks.reallocate = ylib::mem::realloc;
  173. return;
  174. }
  175. global_hooks.allocate = ylib::mem::malloc;
  176. if (hooks->malloc_fn != NULL)
  177. {
  178. global_hooks.allocate = hooks->malloc_fn;
  179. }
  180. global_hooks.deallocate = ylib::mem::free;
  181. if (hooks->free_fn != NULL)
  182. {
  183. global_hooks.deallocate = hooks->free_fn;
  184. }
  185. /* use realloc only if both free and malloc are used */
  186. global_hooks.reallocate = NULL;
  187. if ((global_hooks.allocate == ylib::mem::malloc) && (global_hooks.deallocate == ylib::mem::free))
  188. {
  189. global_hooks.reallocate = ylib::mem::realloc;
  190. }
  191. }
  192. /* Internal constructor. */
  193. static cJSON* cJSON_New_Item(const internal_hooks* const hooks)
  194. {
  195. cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
  196. if (node)
  197. {
  198. memset(node, '\0', sizeof(cJSON));
  199. }
  200. return node;
  201. }
  202. /* Delete a cJSON structure. */
  203. void cJSON_Delete(cJSON* item)
  204. {
  205. cJSON* next = NULL;
  206. while (item != NULL)
  207. {
  208. next = item->next;
  209. if (!(item->type & cJSON_IsReference) && (item->child != NULL))
  210. {
  211. cJSON_Delete(item->child);
  212. }
  213. if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
  214. {
  215. global_hooks.deallocate(item->valuestring);
  216. }
  217. if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
  218. {
  219. global_hooks.deallocate(item->string);
  220. }
  221. global_hooks.deallocate(item);
  222. item = next;
  223. }
  224. }
  225. /* get the decimal point character of the current locale */
  226. static unsigned char get_decimal_point(void)
  227. {
  228. #ifdef ENABLE_LOCALES
  229. struct lconv* lconv = localeconv();
  230. return (unsigned char)lconv->decimal_point[0];
  231. #else
  232. return '.';
  233. #endif
  234. }
  235. typedef struct
  236. {
  237. const unsigned char* content;
  238. size_t length;
  239. size_t offset;
  240. size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
  241. internal_hooks hooks;
  242. } parse_buffer;
  243. /* check if the given size is left to read in a given parse buffer (starting with 1) */
  244. #define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
  245. /* check if the buffer can be accessed at the given index (starting with 0) */
  246. #define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
  247. #define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
  248. /* get a pointer to the buffer at the position */
  249. #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
  250. /* Parse the input text to generate a number, and populate the result into item. */
  251. static cJSON_bool parse_number(cJSON* const item, parse_buffer* const input_buffer)
  252. {
  253. double number = 0;
  254. unsigned char* after_end = NULL;
  255. unsigned char number_c_string[64];
  256. unsigned char decimal_point = get_decimal_point();
  257. size_t i = 0;
  258. if ((input_buffer == NULL) || (input_buffer->content == NULL))
  259. {
  260. return false;
  261. }
  262. /* copy the number into a temporary buffer and replace '.' with the decimal point
  263. * of the current locale (for strtod)
  264. * This also takes care of '\0' not necessarily being available for marking the end of the input */
  265. for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
  266. {
  267. switch (buffer_at_offset(input_buffer)[i])
  268. {
  269. case '0':
  270. case '1':
  271. case '2':
  272. case '3':
  273. case '4':
  274. case '5':
  275. case '6':
  276. case '7':
  277. case '8':
  278. case '9':
  279. case '+':
  280. case '-':
  281. case 'e':
  282. case 'E':
  283. number_c_string[i] = buffer_at_offset(input_buffer)[i];
  284. break;
  285. case '.':
  286. number_c_string[i] = decimal_point;
  287. break;
  288. default:
  289. goto loop_end;
  290. }
  291. }
  292. loop_end:
  293. number_c_string[i] = '\0';
  294. number = strtod((const char*)number_c_string, (char**)&after_end);
  295. if (number_c_string == after_end)
  296. {
  297. return false; /* parse_error */
  298. }
  299. item->valuedouble = number;
  300. /* use saturation in case of overflow */
  301. if (number >= INT_MAX)
  302. {
  303. item->valueint = INT_MAX;
  304. }
  305. else if (number <= (double)INT_MIN)
  306. {
  307. item->valueint = INT_MIN;
  308. }
  309. else
  310. {
  311. item->valueint = (int)number;
  312. }
  313. item->type = cJSON_Number;
  314. input_buffer->offset += (size_t)(after_end - number_c_string);
  315. return true;
  316. }
  317. /* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
  318. double cJSON_SetNumberHelper(cJSON* object, double number)
  319. {
  320. if (number >= INT_MAX)
  321. {
  322. object->valueint = INT_MAX;
  323. }
  324. else if (number <= (double)INT_MIN)
  325. {
  326. object->valueint = INT_MIN;
  327. }
  328. else
  329. {
  330. object->valueint = (int)number;
  331. }
  332. return object->valuedouble = number;
  333. }
  334. char* cJSON_SetValuestring(cJSON* object, const char* valuestring)
  335. {
  336. char* copy = NULL;
  337. /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
  338. if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference))
  339. {
  340. return NULL;
  341. }
  342. if (strlen(valuestring) <= strlen(object->valuestring))
  343. {
  344. strcpy(object->valuestring, valuestring);
  345. return object->valuestring;
  346. }
  347. copy = (char*)cJSON_strdup((const unsigned char*)valuestring, &global_hooks);
  348. if (copy == NULL)
  349. {
  350. return NULL;
  351. }
  352. if (object->valuestring != NULL)
  353. {
  354. cJSON_free(object->valuestring);
  355. }
  356. object->valuestring = copy;
  357. return copy;
  358. }
  359. typedef struct
  360. {
  361. unsigned char* buffer;
  362. size_t length;
  363. size_t offset;
  364. size_t depth; /* current nesting depth (for formatted printing) */
  365. cJSON_bool noalloc;
  366. cJSON_bool format; /* is this print a formatted print */
  367. internal_hooks hooks;
  368. } printbuffer;
  369. /* realloc printbuffer if necessary to have at least "needed" bytes more */
  370. static unsigned char* ensure(printbuffer* const p, size_t needed)
  371. {
  372. unsigned char* newbuffer = NULL;
  373. size_t newsize = 0;
  374. if ((p == NULL) || (p->buffer == NULL))
  375. {
  376. return NULL;
  377. }
  378. if ((p->length > 0) && (p->offset >= p->length))
  379. {
  380. /* make sure that offset is valid */
  381. return NULL;
  382. }
  383. if (needed > INT_MAX)
  384. {
  385. /* sizes bigger than INT_MAX are currently not supported */
  386. return NULL;
  387. }
  388. needed += p->offset + 1;
  389. if (needed <= p->length)
  390. {
  391. return p->buffer + p->offset;
  392. }
  393. if (p->noalloc) {
  394. return NULL;
  395. }
  396. /* calculate new buffer size */
  397. if (needed > (INT_MAX / 2))
  398. {
  399. /* overflow of int, use INT_MAX if possible */
  400. if (needed <= INT_MAX)
  401. {
  402. newsize = INT_MAX;
  403. }
  404. else
  405. {
  406. return NULL;
  407. }
  408. }
  409. else
  410. {
  411. newsize = needed * 2;
  412. }
  413. if (p->hooks.reallocate != NULL)
  414. {
  415. /* reallocate with realloc if available */
  416. newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
  417. if (newbuffer == NULL)
  418. {
  419. p->hooks.deallocate(p->buffer);
  420. p->length = 0;
  421. p->buffer = NULL;
  422. return NULL;
  423. }
  424. }
  425. else
  426. {
  427. /* otherwise reallocate manually */
  428. newbuffer = (unsigned char*)p->hooks.allocate(newsize);
  429. if (!newbuffer)
  430. {
  431. p->hooks.deallocate(p->buffer);
  432. p->length = 0;
  433. p->buffer = NULL;
  434. return NULL;
  435. }
  436. memcpy(newbuffer, p->buffer, p->offset + 1);
  437. p->hooks.deallocate(p->buffer);
  438. }
  439. p->length = newsize;
  440. p->buffer = newbuffer;
  441. return newbuffer + p->offset;
  442. }
  443. /* calculate the new length of the string in a printbuffer and update the offset */
  444. static void update_offset(printbuffer* const buffer)
  445. {
  446. const unsigned char* buffer_pointer = NULL;
  447. if ((buffer == NULL) || (buffer->buffer == NULL))
  448. {
  449. return;
  450. }
  451. buffer_pointer = buffer->buffer + buffer->offset;
  452. buffer->offset += strlen((const char*)buffer_pointer);
  453. }
  454. /* securely comparison of floating-point variables */
  455. static cJSON_bool compare_double(double a, double b)
  456. {
  457. double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
  458. return (fabs(a - b) <= maxVal * DBL_EPSILON);
  459. }
  460. /* Render the number nicely from the given item into a string. */
  461. static cJSON_bool print_number(const cJSON* const item, printbuffer* const output_buffer)
  462. {
  463. unsigned char* output_pointer = NULL;
  464. double d = item->valuedouble;
  465. int length = 0;
  466. size_t i = 0;
  467. unsigned char number_buffer[26] = { 0 }; /* temporary buffer to print the number into */
  468. unsigned char decimal_point = get_decimal_point();
  469. double test = 0.0;
  470. if (output_buffer == NULL)
  471. {
  472. return false;
  473. }
  474. /* This checks for NaN and Infinity */
  475. if (isnan(d) || isinf(d))
  476. {
  477. length = sprintf((char*)number_buffer, "null");
  478. }
  479. else
  480. {
  481. /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
  482. length = sprintf((char*)number_buffer, "%1.15g", d);
  483. /* Check whether the original double can be recovered */
  484. if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
  485. {
  486. /* If not, print with 17 decimal places of precision */
  487. length = sprintf((char*)number_buffer, "%1.17g", d);
  488. }
  489. }
  490. /* sprintf failed or buffer overrun occurred */
  491. if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
  492. {
  493. return false;
  494. }
  495. /* reserve appropriate space in the output */
  496. output_pointer = ensure(output_buffer, (size_t)length + sizeof(""));
  497. if (output_pointer == NULL)
  498. {
  499. return false;
  500. }
  501. /* copy the printed number to the output and replace locale
  502. * dependent decimal point with '.' */
  503. for (i = 0; i < ((size_t)length); i++)
  504. {
  505. if (number_buffer[i] == decimal_point)
  506. {
  507. output_pointer[i] = '.';
  508. continue;
  509. }
  510. output_pointer[i] = number_buffer[i];
  511. }
  512. output_pointer[i] = '\0';
  513. output_buffer->offset += (size_t)length;
  514. return true;
  515. }
  516. /* parse 4 digit hexadecimal number */
  517. static unsigned parse_hex4(const unsigned char* const input)
  518. {
  519. unsigned int h = 0;
  520. size_t i = 0;
  521. for (i = 0; i < 4; i++)
  522. {
  523. /* parse digit */
  524. if ((input[i] >= '0') && (input[i] <= '9'))
  525. {
  526. h += (unsigned int)input[i] - '0';
  527. }
  528. else if ((input[i] >= 'A') && (input[i] <= 'F'))
  529. {
  530. h += (unsigned int)10 + input[i] - 'A';
  531. }
  532. else if ((input[i] >= 'a') && (input[i] <= 'f'))
  533. {
  534. h += (unsigned int)10 + input[i] - 'a';
  535. }
  536. else /* invalid */
  537. {
  538. return 0;
  539. }
  540. if (i < 3)
  541. {
  542. /* shift left to make place for the next nibble */
  543. h = h << 4;
  544. }
  545. }
  546. return h;
  547. }
  548. /* converts a UTF-16 literal to UTF-8
  549. * A literal can be one or two sequences of the form \uXXXX */
  550. static unsigned char utf16_literal_to_utf8(const unsigned char* const input_pointer, const unsigned char* const input_end, unsigned char** output_pointer)
  551. {
  552. long unsigned int codepoint = 0;
  553. unsigned int first_code = 0;
  554. const unsigned char* first_sequence = input_pointer;
  555. unsigned char utf8_length = 0;
  556. unsigned char utf8_position = 0;
  557. unsigned char sequence_length = 0;
  558. unsigned char first_byte_mark = 0;
  559. if ((input_end - first_sequence) < 6)
  560. {
  561. /* input ends unexpectedly */
  562. goto fail;
  563. }
  564. /* get the first utf16 sequence */
  565. first_code = parse_hex4(first_sequence + 2);
  566. /* check that the code is valid */
  567. if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
  568. {
  569. goto fail;
  570. }
  571. /* UTF16 surrogate pair */
  572. if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
  573. {
  574. const unsigned char* second_sequence = first_sequence + 6;
  575. unsigned int second_code = 0;
  576. sequence_length = 12; /* \uXXXX\uXXXX */
  577. if ((input_end - second_sequence) < 6)
  578. {
  579. /* input ends unexpectedly */
  580. goto fail;
  581. }
  582. if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
  583. {
  584. /* missing second half of the surrogate pair */
  585. goto fail;
  586. }
  587. /* get the second utf16 sequence */
  588. second_code = parse_hex4(second_sequence + 2);
  589. /* check that the code is valid */
  590. if ((second_code < 0xDC00) || (second_code > 0xDFFF))
  591. {
  592. /* invalid second half of the surrogate pair */
  593. goto fail;
  594. }
  595. /* calculate the unicode codepoint from the surrogate pair */
  596. codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
  597. }
  598. else
  599. {
  600. sequence_length = 6; /* \uXXXX */
  601. codepoint = first_code;
  602. }
  603. /* encode as UTF-8
  604. * takes at maximum 4 bytes to encode:
  605. * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
  606. if (codepoint < 0x80)
  607. {
  608. /* normal ascii, encoding 0xxxxxxx */
  609. utf8_length = 1;
  610. }
  611. else if (codepoint < 0x800)
  612. {
  613. /* two bytes, encoding 110xxxxx 10xxxxxx */
  614. utf8_length = 2;
  615. first_byte_mark = 0xC0; /* 11000000 */
  616. }
  617. else if (codepoint < 0x10000)
  618. {
  619. /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
  620. utf8_length = 3;
  621. first_byte_mark = 0xE0; /* 11100000 */
  622. }
  623. else if (codepoint <= 0x10FFFF)
  624. {
  625. /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
  626. utf8_length = 4;
  627. first_byte_mark = 0xF0; /* 11110000 */
  628. }
  629. else
  630. {
  631. /* invalid unicode codepoint */
  632. goto fail;
  633. }
  634. /* encode as utf8 */
  635. for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
  636. {
  637. /* 10xxxxxx */
  638. (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
  639. codepoint >>= 6;
  640. }
  641. /* encode first byte */
  642. if (utf8_length > 1)
  643. {
  644. (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
  645. }
  646. else
  647. {
  648. (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
  649. }
  650. *output_pointer += utf8_length;
  651. return sequence_length;
  652. fail:
  653. return 0;
  654. }
  655. /* Parse the input text into an unescaped cinput, and populate item. */
  656. static cJSON_bool parse_string(cJSON* const item, parse_buffer* const input_buffer)
  657. {
  658. const unsigned char* input_pointer = buffer_at_offset(input_buffer) + 1;
  659. const unsigned char* input_end = buffer_at_offset(input_buffer) + 1;
  660. unsigned char* output_pointer = NULL;
  661. unsigned char* output = NULL;
  662. /* not a string */
  663. if (buffer_at_offset(input_buffer)[0] != '\"')
  664. {
  665. goto fail;
  666. }
  667. {
  668. /* calculate approximate size of the output (overestimate) */
  669. size_t allocation_length = 0;
  670. size_t skipped_bytes = 0;
  671. while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
  672. {
  673. /* is escape sequence */
  674. if (input_end[0] == '\\')
  675. {
  676. if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
  677. {
  678. /* prevent buffer overflow when last input character is a backslash */
  679. goto fail;
  680. }
  681. skipped_bytes++;
  682. input_end++;
  683. }
  684. input_end++;
  685. }
  686. if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
  687. {
  688. goto fail; /* string ended unexpectedly */
  689. }
  690. /* This is at most how much we need for the output */
  691. allocation_length = (size_t)(input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
  692. output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
  693. if (output == NULL)
  694. {
  695. goto fail; /* allocation failure */
  696. }
  697. }
  698. output_pointer = output;
  699. /* loop through the string literal */
  700. while (input_pointer < input_end)
  701. {
  702. if (*input_pointer != '\\')
  703. {
  704. *output_pointer++ = *input_pointer++;
  705. }
  706. /* escape sequence */
  707. else
  708. {
  709. unsigned char sequence_length = 2;
  710. if ((input_end - input_pointer) < 1)
  711. {
  712. goto fail;
  713. }
  714. switch (input_pointer[1])
  715. {
  716. case 'b':
  717. *output_pointer++ = '\b';
  718. break;
  719. case 'f':
  720. *output_pointer++ = '\f';
  721. break;
  722. case 'n':
  723. *output_pointer++ = '\n';
  724. break;
  725. case 'r':
  726. *output_pointer++ = '\r';
  727. break;
  728. case 't':
  729. *output_pointer++ = '\t';
  730. break;
  731. case '\"':
  732. case '\\':
  733. case '/':
  734. *output_pointer++ = input_pointer[1];
  735. break;
  736. /* UTF-16 literal */
  737. case 'u':
  738. sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
  739. if (sequence_length == 0)
  740. {
  741. /* failed to convert UTF16-literal to UTF-8 */
  742. goto fail;
  743. }
  744. break;
  745. default:
  746. goto fail;
  747. }
  748. input_pointer += sequence_length;
  749. }
  750. }
  751. /* zero terminate the output */
  752. *output_pointer = '\0';
  753. item->type = cJSON_String;
  754. item->valuestring = (char*)output;
  755. input_buffer->offset = (size_t)(input_end - input_buffer->content);
  756. input_buffer->offset++;
  757. return true;
  758. fail:
  759. if (output != NULL)
  760. {
  761. input_buffer->hooks.deallocate(output);
  762. }
  763. if (input_pointer != NULL)
  764. {
  765. input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
  766. }
  767. return false;
  768. }
  769. /* Render the cstring provided to an escaped version that can be printed. */
  770. static cJSON_bool print_string_ptr(const unsigned char* const input, printbuffer* const output_buffer)
  771. {
  772. const unsigned char* input_pointer = NULL;
  773. unsigned char* output = NULL;
  774. unsigned char* output_pointer = NULL;
  775. size_t output_length = 0;
  776. /* numbers of additional characters needed for escaping */
  777. size_t escape_characters = 0;
  778. if (output_buffer == NULL)
  779. {
  780. return false;
  781. }
  782. /* empty string */
  783. if (input == NULL)
  784. {
  785. output = ensure(output_buffer, sizeof("\"\""));
  786. if (output == NULL)
  787. {
  788. return false;
  789. }
  790. strcpy((char*)output, "\"\"");
  791. return true;
  792. }
  793. /* set "flag" to 1 if something needs to be escaped */
  794. for (input_pointer = input; *input_pointer; input_pointer++)
  795. {
  796. switch (*input_pointer)
  797. {
  798. case '\"':
  799. case '\\':
  800. case '\b':
  801. case '\f':
  802. case '\n':
  803. case '\r':
  804. case '\t':
  805. /* one character escape sequence */
  806. escape_characters++;
  807. break;
  808. default:
  809. if (*input_pointer < 32)
  810. {
  811. /* UTF-16 escape sequence uXXXX */
  812. escape_characters += 5;
  813. }
  814. break;
  815. }
  816. }
  817. output_length = (size_t)(input_pointer - input) + escape_characters;
  818. output = ensure(output_buffer, output_length + sizeof("\"\""));
  819. if (output == NULL)
  820. {
  821. return false;
  822. }
  823. /* no characters have to be escaped */
  824. if (escape_characters == 0)
  825. {
  826. output[0] = '\"';
  827. memcpy(output + 1, input, output_length);
  828. output[output_length + 1] = '\"';
  829. output[output_length + 2] = '\0';
  830. return true;
  831. }
  832. output[0] = '\"';
  833. output_pointer = output + 1;
  834. /* copy the string */
  835. for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
  836. {
  837. if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
  838. {
  839. /* normal character, copy */
  840. *output_pointer = *input_pointer;
  841. }
  842. else
  843. {
  844. /* character needs to be escaped */
  845. *output_pointer++ = '\\';
  846. switch (*input_pointer)
  847. {
  848. case '\\':
  849. *output_pointer = '\\';
  850. break;
  851. case '\"':
  852. *output_pointer = '\"';
  853. break;
  854. case '\b':
  855. *output_pointer = 'b';
  856. break;
  857. case '\f':
  858. *output_pointer = 'f';
  859. break;
  860. case '\n':
  861. *output_pointer = 'n';
  862. break;
  863. case '\r':
  864. *output_pointer = 'r';
  865. break;
  866. case '\t':
  867. *output_pointer = 't';
  868. break;
  869. default:
  870. /* escape and print as unicode codepoint */
  871. sprintf((char*)output_pointer, "u%04x", *input_pointer);
  872. output_pointer += 4;
  873. break;
  874. }
  875. }
  876. }
  877. output[output_length + 1] = '\"';
  878. output[output_length + 2] = '\0';
  879. return true;
  880. }
  881. /* Invoke print_string_ptr (which is useful) on an item. */
  882. static cJSON_bool print_string(const cJSON* const item, printbuffer* const p)
  883. {
  884. return print_string_ptr((unsigned char*)item->valuestring, p);
  885. }
  886. /* Predeclare these prototypes. */
  887. static cJSON_bool parse_value(cJSON* const item, parse_buffer* const input_buffer);
  888. static cJSON_bool print_value(const cJSON* const item, printbuffer* const output_buffer);
  889. static cJSON_bool parse_array(cJSON* const item, parse_buffer* const input_buffer);
  890. static cJSON_bool print_array(const cJSON* const item, printbuffer* const output_buffer);
  891. static cJSON_bool parse_object(cJSON* const item, parse_buffer* const input_buffer);
  892. static cJSON_bool print_object(const cJSON* const item, printbuffer* const output_buffer);
  893. /* Utility to jump whitespace and cr/lf */
  894. static parse_buffer* buffer_skip_whitespace(parse_buffer* const buffer)
  895. {
  896. if ((buffer == NULL) || (buffer->content == NULL))
  897. {
  898. return NULL;
  899. }
  900. if (cannot_access_at_index(buffer, 0))
  901. {
  902. return buffer;
  903. }
  904. while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
  905. {
  906. buffer->offset++;
  907. }
  908. if (buffer->offset == buffer->length)
  909. {
  910. buffer->offset--;
  911. }
  912. return buffer;
  913. }
  914. /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */
  915. static parse_buffer* skip_utf8_bom(parse_buffer* const buffer)
  916. {
  917. if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0))
  918. {
  919. return NULL;
  920. }
  921. if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0))
  922. {
  923. buffer->offset += 3;
  924. }
  925. return buffer;
  926. }
  927. cJSON* cJSON_ParseWithOpts(const char* value, const char** return_parse_end, cJSON_bool require_null_terminated)
  928. {
  929. size_t buffer_length;
  930. if (NULL == value)
  931. {
  932. return NULL;
  933. }
  934. /* Adding null character size due to require_null_terminated. */
  935. buffer_length = strlen(value) + sizeof("");
  936. return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated);
  937. }
  938. /* Parse an object - create a new root, and populate. */
  939. cJSON* cJSON_ParseWithLengthOpts(const char* value, size_t buffer_length, const char** return_parse_end, cJSON_bool require_null_terminated)
  940. {
  941. parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
  942. cJSON* item = NULL;
  943. /* reset error position */
  944. global_error.json = NULL;
  945. global_error.position = 0;
  946. if (value == NULL || 0 == buffer_length)
  947. {
  948. goto fail;
  949. }
  950. buffer.content = (const unsigned char*)value;
  951. buffer.length = buffer_length;
  952. buffer.offset = 0;
  953. buffer.hooks = global_hooks;
  954. item = cJSON_New_Item(&global_hooks);
  955. if (item == NULL) /* memory fail */
  956. {
  957. goto fail;
  958. }
  959. if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
  960. {
  961. /* parse failure. ep is set. */
  962. goto fail;
  963. }
  964. /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
  965. if (require_null_terminated)
  966. {
  967. buffer_skip_whitespace(&buffer);
  968. if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
  969. {
  970. goto fail;
  971. }
  972. }
  973. if (return_parse_end)
  974. {
  975. *return_parse_end = (const char*)buffer_at_offset(&buffer);
  976. }
  977. return item;
  978. fail:
  979. if (item != NULL)
  980. {
  981. cJSON_Delete(item);
  982. }
  983. if (value != NULL)
  984. {
  985. error__ local_error;
  986. local_error.json = (const unsigned char*)value;
  987. local_error.position = 0;
  988. if (buffer.offset < buffer.length)
  989. {
  990. local_error.position = buffer.offset;
  991. }
  992. else if (buffer.length > 0)
  993. {
  994. local_error.position = buffer.length - 1;
  995. }
  996. if (return_parse_end != NULL)
  997. {
  998. *return_parse_end = (const char*)local_error.json + local_error.position;
  999. }
  1000. global_error = local_error;
  1001. }
  1002. return NULL;
  1003. }
  1004. /* Default options for cJSON_Parse */
  1005. cJSON* cJSON_Parse(const char* value)
  1006. {
  1007. return cJSON_ParseWithOpts(value, 0, 0);
  1008. }
  1009. cJSON* cJSON_ParseWithLength(const char* value, size_t buffer_length)
  1010. {
  1011. return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0);
  1012. }
  1013. #define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
  1014. static unsigned char* print(const cJSON* const item, cJSON_bool format, const internal_hooks* const hooks)
  1015. {
  1016. static const size_t default_buffer_size = 256;
  1017. printbuffer buffer[1];
  1018. unsigned char* printed = NULL;
  1019. memset(buffer, 0, sizeof(buffer));
  1020. /* create buffer */
  1021. buffer->buffer = (unsigned char*)hooks->allocate(default_buffer_size);
  1022. buffer->length = default_buffer_size;
  1023. buffer->format = format;
  1024. buffer->hooks = *hooks;
  1025. if (buffer->buffer == NULL)
  1026. {
  1027. goto fail;
  1028. }
  1029. /* print the value */
  1030. if (!print_value(item, buffer))
  1031. {
  1032. goto fail;
  1033. }
  1034. update_offset(buffer);
  1035. /* check if reallocate is available */
  1036. if (hooks->reallocate != NULL)
  1037. {
  1038. printed = (unsigned char*)hooks->reallocate(buffer->buffer, buffer->offset + 1);
  1039. if (printed == NULL) {
  1040. goto fail;
  1041. }
  1042. buffer->buffer = NULL;
  1043. }
  1044. else /* otherwise copy the JSON over to a new buffer */
  1045. {
  1046. printed = (unsigned char*)hooks->allocate(buffer->offset + 1);
  1047. if (printed == NULL)
  1048. {
  1049. goto fail;
  1050. }
  1051. memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
  1052. printed[buffer->offset] = '\0'; /* just to be sure */
  1053. /* free the buffer */
  1054. hooks->deallocate(buffer->buffer);
  1055. }
  1056. return printed;
  1057. fail:
  1058. if (buffer->buffer != NULL)
  1059. {
  1060. hooks->deallocate(buffer->buffer);
  1061. }
  1062. if (printed != NULL)
  1063. {
  1064. hooks->deallocate(printed);
  1065. }
  1066. return NULL;
  1067. }
  1068. /* Render a cJSON item/entity/structure to text. */
  1069. char* cJSON_Print(const cJSON* item)
  1070. {
  1071. return (char*)print(item, true, &global_hooks);
  1072. }
  1073. char* cJSON_PrintUnformatted(const cJSON* item)
  1074. {
  1075. return (char*)print(item, false, &global_hooks);
  1076. }
  1077. char* cJSON_PrintBuffered(const cJSON* item, int prebuffer, cJSON_bool fmt)
  1078. {
  1079. printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
  1080. if (prebuffer < 0)
  1081. {
  1082. return NULL;
  1083. }
  1084. p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
  1085. if (!p.buffer)
  1086. {
  1087. return NULL;
  1088. }
  1089. p.length = (size_t)prebuffer;
  1090. p.offset = 0;
  1091. p.noalloc = false;
  1092. p.format = fmt;
  1093. p.hooks = global_hooks;
  1094. if (!print_value(item, &p))
  1095. {
  1096. global_hooks.deallocate(p.buffer);
  1097. return NULL;
  1098. }
  1099. return (char*)p.buffer;
  1100. }
  1101. cJSON_bool cJSON_PrintPreallocated(cJSON* item, char* buffer, const int length, const cJSON_bool format)
  1102. {
  1103. printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
  1104. if ((length < 0) || (buffer == NULL))
  1105. {
  1106. return false;
  1107. }
  1108. p.buffer = (unsigned char*)buffer;
  1109. p.length = (size_t)length;
  1110. p.offset = 0;
  1111. p.noalloc = true;
  1112. p.format = format;
  1113. p.hooks = global_hooks;
  1114. return print_value(item, &p);
  1115. }
  1116. /* Parser core - when encountering text, process appropriately. */
  1117. static cJSON_bool parse_value(cJSON* const item, parse_buffer* const input_buffer)
  1118. {
  1119. if ((input_buffer == NULL) || (input_buffer->content == NULL))
  1120. {
  1121. return false; /* no input */
  1122. }
  1123. /* parse the different types of values */
  1124. /* null */
  1125. if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
  1126. {
  1127. item->type = cJSON_NULL;
  1128. input_buffer->offset += 4;
  1129. return true;
  1130. }
  1131. /* false */
  1132. if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
  1133. {
  1134. item->type = cJSON_False;
  1135. input_buffer->offset += 5;
  1136. return true;
  1137. }
  1138. /* true */
  1139. if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
  1140. {
  1141. item->type = cJSON_True;
  1142. item->valueint = 1;
  1143. input_buffer->offset += 4;
  1144. return true;
  1145. }
  1146. /* string */
  1147. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
  1148. {
  1149. return parse_string(item, input_buffer);
  1150. }
  1151. /* number */
  1152. if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
  1153. {
  1154. return parse_number(item, input_buffer);
  1155. }
  1156. /* array */
  1157. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
  1158. {
  1159. return parse_array(item, input_buffer);
  1160. }
  1161. /* object */
  1162. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
  1163. {
  1164. return parse_object(item, input_buffer);
  1165. }
  1166. return false;
  1167. }
  1168. /* Render a value to text. */
  1169. static cJSON_bool print_value(const cJSON* const item, printbuffer* const output_buffer)
  1170. {
  1171. unsigned char* output = NULL;
  1172. if ((item == NULL) || (output_buffer == NULL))
  1173. {
  1174. return false;
  1175. }
  1176. switch ((item->type) & 0xFF)
  1177. {
  1178. case cJSON_NULL:
  1179. output = ensure(output_buffer, 5);
  1180. if (output == NULL)
  1181. {
  1182. return false;
  1183. }
  1184. strcpy((char*)output, "null");
  1185. return true;
  1186. case cJSON_False:
  1187. output = ensure(output_buffer, 6);
  1188. if (output == NULL)
  1189. {
  1190. return false;
  1191. }
  1192. strcpy((char*)output, "false");
  1193. return true;
  1194. case cJSON_True:
  1195. output = ensure(output_buffer, 5);
  1196. if (output == NULL)
  1197. {
  1198. return false;
  1199. }
  1200. strcpy((char*)output, "true");
  1201. return true;
  1202. case cJSON_Number:
  1203. return print_number(item, output_buffer);
  1204. case cJSON_Raw:
  1205. {
  1206. size_t raw_length = 0;
  1207. if (item->valuestring == NULL)
  1208. {
  1209. return false;
  1210. }
  1211. raw_length = strlen(item->valuestring) + sizeof("");
  1212. output = ensure(output_buffer, raw_length);
  1213. if (output == NULL)
  1214. {
  1215. return false;
  1216. }
  1217. memcpy(output, item->valuestring, raw_length);
  1218. return true;
  1219. }
  1220. case cJSON_String:
  1221. return print_string(item, output_buffer);
  1222. case cJSON_Array:
  1223. return print_array(item, output_buffer);
  1224. case cJSON_Object:
  1225. return print_object(item, output_buffer);
  1226. default:
  1227. return false;
  1228. }
  1229. }
  1230. /* Build an array from input text. */
  1231. static cJSON_bool parse_array(cJSON* const item, parse_buffer* const input_buffer)
  1232. {
  1233. cJSON* head = NULL; /* head of the linked list */
  1234. cJSON* current_item = NULL;
  1235. if (input_buffer->depth >= CJSON_NESTING_LIMIT)
  1236. {
  1237. return false; /* to deeply nested */
  1238. }
  1239. input_buffer->depth++;
  1240. if (buffer_at_offset(input_buffer)[0] != '[')
  1241. {
  1242. /* not an array */
  1243. goto fail;
  1244. }
  1245. input_buffer->offset++;
  1246. buffer_skip_whitespace(input_buffer);
  1247. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']'))
  1248. {
  1249. /* empty array */
  1250. goto success;
  1251. }
  1252. /* check if we skipped to the end of the buffer */
  1253. if (cannot_access_at_index(input_buffer, 0))
  1254. {
  1255. input_buffer->offset--;
  1256. goto fail;
  1257. }
  1258. /* step back to character in front of the first element */
  1259. input_buffer->offset--;
  1260. /* loop through the comma separated array elements */
  1261. do
  1262. {
  1263. /* allocate next item */
  1264. cJSON* new_item = cJSON_New_Item(&(input_buffer->hooks));
  1265. if (new_item == NULL)
  1266. {
  1267. goto fail; /* allocation failure */
  1268. }
  1269. /* attach next item to list */
  1270. if (head == NULL)
  1271. {
  1272. /* start the linked list */
  1273. current_item = head = new_item;
  1274. }
  1275. else
  1276. {
  1277. /* add to the end and advance */
  1278. current_item->next = new_item;
  1279. new_item->prev = current_item;
  1280. current_item = new_item;
  1281. }
  1282. /* parse next value */
  1283. input_buffer->offset++;
  1284. buffer_skip_whitespace(input_buffer);
  1285. if (!parse_value(current_item, input_buffer))
  1286. {
  1287. goto fail; /* failed to parse value */
  1288. }
  1289. buffer_skip_whitespace(input_buffer);
  1290. } while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
  1291. if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
  1292. {
  1293. goto fail; /* expected end of array */
  1294. }
  1295. success:
  1296. input_buffer->depth--;
  1297. if (head != NULL) {
  1298. head->prev = current_item;
  1299. }
  1300. item->type = cJSON_Array;
  1301. item->child = head;
  1302. input_buffer->offset++;
  1303. return true;
  1304. fail:
  1305. if (head != NULL)
  1306. {
  1307. cJSON_Delete(head);
  1308. }
  1309. return false;
  1310. }
  1311. /* Render an array to text */
  1312. static cJSON_bool print_array(const cJSON* const item, printbuffer* const output_buffer)
  1313. {
  1314. unsigned char* output_pointer = NULL;
  1315. size_t length = 0;
  1316. cJSON* current_element = item->child;
  1317. if (output_buffer == NULL)
  1318. {
  1319. return false;
  1320. }
  1321. /* Compose the output array. */
  1322. /* opening square bracket */
  1323. output_pointer = ensure(output_buffer, 1);
  1324. if (output_pointer == NULL)
  1325. {
  1326. return false;
  1327. }
  1328. *output_pointer = '[';
  1329. output_buffer->offset++;
  1330. output_buffer->depth++;
  1331. while (current_element != NULL)
  1332. {
  1333. if (!print_value(current_element, output_buffer))
  1334. {
  1335. return false;
  1336. }
  1337. update_offset(output_buffer);
  1338. if (current_element->next)
  1339. {
  1340. length = (size_t)(output_buffer->format ? 2 : 1);
  1341. output_pointer = ensure(output_buffer, length + 1);
  1342. if (output_pointer == NULL)
  1343. {
  1344. return false;
  1345. }
  1346. *output_pointer++ = ',';
  1347. if (output_buffer->format)
  1348. {
  1349. *output_pointer++ = ' ';
  1350. }
  1351. *output_pointer = '\0';
  1352. output_buffer->offset += length;
  1353. }
  1354. current_element = current_element->next;
  1355. }
  1356. output_pointer = ensure(output_buffer, 2);
  1357. if (output_pointer == NULL)
  1358. {
  1359. return false;
  1360. }
  1361. *output_pointer++ = ']';
  1362. *output_pointer = '\0';
  1363. output_buffer->depth--;
  1364. return true;
  1365. }
  1366. /* Build an object from the text. */
  1367. static cJSON_bool parse_object(cJSON* const item, parse_buffer* const input_buffer)
  1368. {
  1369. cJSON* head = NULL; /* linked list head */
  1370. cJSON* current_item = NULL;
  1371. if (input_buffer->depth >= CJSON_NESTING_LIMIT)
  1372. {
  1373. return false; /* to deeply nested */
  1374. }
  1375. input_buffer->depth++;
  1376. if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
  1377. {
  1378. goto fail; /* not an object */
  1379. }
  1380. input_buffer->offset++;
  1381. buffer_skip_whitespace(input_buffer);
  1382. if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}'))
  1383. {
  1384. goto success; /* empty object */
  1385. }
  1386. /* check if we skipped to the end of the buffer */
  1387. if (cannot_access_at_index(input_buffer, 0))
  1388. {
  1389. input_buffer->offset--;
  1390. goto fail;
  1391. }
  1392. /* step back to character in front of the first element */
  1393. input_buffer->offset--;
  1394. /* loop through the comma separated array elements */
  1395. do
  1396. {
  1397. /* allocate next item */
  1398. cJSON* new_item = cJSON_New_Item(&(input_buffer->hooks));
  1399. if (new_item == NULL)
  1400. {
  1401. goto fail; /* allocation failure */
  1402. }
  1403. /* attach next item to list */
  1404. if (head == NULL)
  1405. {
  1406. /* start the linked list */
  1407. current_item = head = new_item;
  1408. }
  1409. else
  1410. {
  1411. /* add to the end and advance */
  1412. current_item->next = new_item;
  1413. new_item->prev = current_item;
  1414. current_item = new_item;
  1415. }
  1416. /* parse the name of the child */
  1417. input_buffer->offset++;
  1418. buffer_skip_whitespace(input_buffer);
  1419. if (!parse_string(current_item, input_buffer))
  1420. {
  1421. goto fail; /* failed to parse name */
  1422. }
  1423. buffer_skip_whitespace(input_buffer);
  1424. /* swap valuestring and string, because we parsed the name */
  1425. current_item->string = current_item->valuestring;
  1426. current_item->valuestring = NULL;
  1427. if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
  1428. {
  1429. goto fail; /* invalid object */
  1430. }
  1431. /* parse the value */
  1432. input_buffer->offset++;
  1433. buffer_skip_whitespace(input_buffer);
  1434. if (!parse_value(current_item, input_buffer))
  1435. {
  1436. goto fail; /* failed to parse value */
  1437. }
  1438. buffer_skip_whitespace(input_buffer);
  1439. } while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
  1440. if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
  1441. {
  1442. goto fail; /* expected end of object */
  1443. }
  1444. success:
  1445. input_buffer->depth--;
  1446. if (head != NULL) {
  1447. head->prev = current_item;
  1448. }
  1449. item->type = cJSON_Object;
  1450. item->child = head;
  1451. input_buffer->offset++;
  1452. return true;
  1453. fail:
  1454. if (head != NULL)
  1455. {
  1456. cJSON_Delete(head);
  1457. }
  1458. return false;
  1459. }
  1460. /* Render an object to text. */
  1461. static cJSON_bool print_object(const cJSON* const item, printbuffer* const output_buffer)
  1462. {
  1463. unsigned char* output_pointer = NULL;
  1464. size_t length = 0;
  1465. cJSON* current_item = item->child;
  1466. if (output_buffer == NULL)
  1467. {
  1468. return false;
  1469. }
  1470. /* Compose the output: */
  1471. length = (size_t)(output_buffer->format ? 2 : 1); /* fmt: {\n */
  1472. output_pointer = ensure(output_buffer, length + 1);
  1473. if (output_pointer == NULL)
  1474. {
  1475. return false;
  1476. }
  1477. *output_pointer++ = '{';
  1478. output_buffer->depth++;
  1479. if (output_buffer->format)
  1480. {
  1481. *output_pointer++ = '\n';
  1482. }
  1483. output_buffer->offset += length;
  1484. while (current_item)
  1485. {
  1486. if (output_buffer->format)
  1487. {
  1488. size_t i;
  1489. output_pointer = ensure(output_buffer, output_buffer->depth);
  1490. if (output_pointer == NULL)
  1491. {
  1492. return false;
  1493. }
  1494. for (i = 0; i < output_buffer->depth; i++)
  1495. {
  1496. *output_pointer++ = '\t';
  1497. }
  1498. output_buffer->offset += output_buffer->depth;
  1499. }
  1500. /* print key */
  1501. if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
  1502. {
  1503. return false;
  1504. }
  1505. update_offset(output_buffer);
  1506. length = (size_t)(output_buffer->format ? 2 : 1);
  1507. output_pointer = ensure(output_buffer, length);
  1508. if (output_pointer == NULL)
  1509. {
  1510. return false;
  1511. }
  1512. *output_pointer++ = ':';
  1513. if (output_buffer->format)
  1514. {
  1515. *output_pointer++ = '\t';
  1516. }
  1517. output_buffer->offset += length;
  1518. /* print value */
  1519. if (!print_value(current_item, output_buffer))
  1520. {
  1521. return false;
  1522. }
  1523. update_offset(output_buffer);
  1524. /* print comma if not last */
  1525. length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0));
  1526. output_pointer = ensure(output_buffer, length + 1);
  1527. if (output_pointer == NULL)
  1528. {
  1529. return false;
  1530. }
  1531. if (current_item->next)
  1532. {
  1533. *output_pointer++ = ',';
  1534. }
  1535. if (output_buffer->format)
  1536. {
  1537. *output_pointer++ = '\n';
  1538. }
  1539. *output_pointer = '\0';
  1540. output_buffer->offset += length;
  1541. current_item = current_item->next;
  1542. }
  1543. output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
  1544. if (output_pointer == NULL)
  1545. {
  1546. return false;
  1547. }
  1548. if (output_buffer->format)
  1549. {
  1550. size_t i;
  1551. for (i = 0; i < (output_buffer->depth - 1); i++)
  1552. {
  1553. *output_pointer++ = '\t';
  1554. }
  1555. }
  1556. *output_pointer++ = '}';
  1557. *output_pointer = '\0';
  1558. output_buffer->depth--;
  1559. return true;
  1560. }
  1561. /* Get Array size/item / object item. */
  1562. int cJSON_GetArraySize(const cJSON* array)
  1563. {
  1564. cJSON* child = NULL;
  1565. size_t size = 0;
  1566. if (array == NULL)
  1567. {
  1568. return 0;
  1569. }
  1570. child = array->child;
  1571. while (child != NULL)
  1572. {
  1573. size++;
  1574. child = child->next;
  1575. }
  1576. /* FIXME: Can overflow here. Cannot be fixed without breaking the API */
  1577. return (int)size;
  1578. }
  1579. static cJSON* get_array_item(const cJSON* array, size_t index)
  1580. {
  1581. cJSON* current_child = NULL;
  1582. if (array == NULL)
  1583. {
  1584. return NULL;
  1585. }
  1586. current_child = array->child;
  1587. while ((current_child != NULL) && (index > 0))
  1588. {
  1589. index--;
  1590. current_child = current_child->next;
  1591. }
  1592. return current_child;
  1593. }
  1594. cJSON* cJSON_GetArrayItem(const cJSON* array, int index)
  1595. {
  1596. if (index < 0)
  1597. {
  1598. return NULL;
  1599. }
  1600. return get_array_item(array, (size_t)index);
  1601. }
  1602. static cJSON* get_object_item(const cJSON* const object, const char* const name, const cJSON_bool case_sensitive)
  1603. {
  1604. cJSON* current_element = NULL;
  1605. if ((object == NULL) || (name == NULL))
  1606. {
  1607. return NULL;
  1608. }
  1609. current_element = object->child;
  1610. if (case_sensitive)
  1611. {
  1612. while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0))
  1613. {
  1614. current_element = current_element->next;
  1615. }
  1616. }
  1617. else
  1618. {
  1619. while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
  1620. {
  1621. current_element = current_element->next;
  1622. }
  1623. }
  1624. if ((current_element == NULL) || (current_element->string == NULL)) {
  1625. return NULL;
  1626. }
  1627. return current_element;
  1628. }
  1629. cJSON* cJSON_GetObjectItem(const cJSON* const object, const char* const string)
  1630. {
  1631. return get_object_item(object, string, false);
  1632. }
  1633. cJSON* cJSON_GetObjectItemCaseSensitive(const cJSON* const object, const char* const string)
  1634. {
  1635. return get_object_item(object, string, true);
  1636. }
  1637. cJSON_bool cJSON_HasObjectItem(const cJSON* object, const char* string)
  1638. {
  1639. return cJSON_GetObjectItem(object, string) ? 1 : 0;
  1640. }
  1641. /* Utility for array list handling. */
  1642. static void suffix_object(cJSON* prev, cJSON* item)
  1643. {
  1644. prev->next = item;
  1645. item->prev = prev;
  1646. }
  1647. /* Utility for handling references. */
  1648. static cJSON* create_reference(const cJSON* item, const internal_hooks* const hooks)
  1649. {
  1650. cJSON* reference = NULL;
  1651. if (item == NULL)
  1652. {
  1653. return NULL;
  1654. }
  1655. reference = cJSON_New_Item(hooks);
  1656. if (reference == NULL)
  1657. {
  1658. return NULL;
  1659. }
  1660. memcpy(reference, item, sizeof(cJSON));
  1661. reference->string = NULL;
  1662. reference->type |= cJSON_IsReference;
  1663. reference->next = reference->prev = NULL;
  1664. return reference;
  1665. }
  1666. static cJSON_bool add_item_to_array(cJSON* array, cJSON* item)
  1667. {
  1668. cJSON* child = NULL;
  1669. if ((item == NULL) || (array == NULL) || (array == item))
  1670. {
  1671. return false;
  1672. }
  1673. child = array->child;
  1674. /*
  1675. * To find the last item in array quickly, we use prev in array
  1676. */
  1677. if (child == NULL)
  1678. {
  1679. /* list is empty, start new one */
  1680. array->child = item;
  1681. item->prev = item;
  1682. item->next = NULL;
  1683. }
  1684. else
  1685. {
  1686. /* append to the end */
  1687. if (child->prev)
  1688. {
  1689. suffix_object(child->prev, item);
  1690. array->child->prev = item;
  1691. }
  1692. }
  1693. return true;
  1694. }
  1695. /* Add item to array/object. */
  1696. cJSON_bool cJSON_AddItemToArray(cJSON* array, cJSON* item)
  1697. {
  1698. return add_item_to_array(array, item);
  1699. }
  1700. #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
  1701. #pragma GCC diagnostic push
  1702. #endif
  1703. #ifdef __GNUC__
  1704. #pragma GCC diagnostic ignored "-Wcast-qual"
  1705. #endif
  1706. /* helper function to cast away const */
  1707. static void* cast_away_const(const void* string)
  1708. {
  1709. return (void*)string;
  1710. }
  1711. #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
  1712. #pragma GCC diagnostic pop
  1713. #endif
  1714. static cJSON_bool add_item_to_object(cJSON* const object, const char* const string, cJSON* const item, const internal_hooks* const hooks, const cJSON_bool constant_key)
  1715. {
  1716. char* new_key = NULL;
  1717. int new_type = cJSON_Invalid;
  1718. if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item))
  1719. {
  1720. return false;
  1721. }
  1722. if (constant_key)
  1723. {
  1724. new_key = (char*)cast_away_const(string);
  1725. new_type = item->type | cJSON_StringIsConst;
  1726. }
  1727. else
  1728. {
  1729. new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks);
  1730. if (new_key == NULL)
  1731. {
  1732. return false;
  1733. }
  1734. new_type = item->type & ~cJSON_StringIsConst;
  1735. }
  1736. if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
  1737. {
  1738. hooks->deallocate(item->string);
  1739. }
  1740. item->string = new_key;
  1741. item->type = new_type;
  1742. return add_item_to_array(object, item);
  1743. }
  1744. cJSON_bool cJSON_AddItemToObject(cJSON* object, const char* string, cJSON* item)
  1745. {
  1746. return add_item_to_object(object, string, item, &global_hooks, false);
  1747. }
  1748. /* Add an item to an object with constant string as key */
  1749. cJSON_bool cJSON_AddItemToObjectCS(cJSON* object, const char* string, cJSON* item)
  1750. {
  1751. return add_item_to_object(object, string, item, &global_hooks, true);
  1752. }
  1753. cJSON_bool cJSON_AddItemReferenceToArray(cJSON* array, cJSON* item)
  1754. {
  1755. if (array == NULL)
  1756. {
  1757. return false;
  1758. }
  1759. return add_item_to_array(array, create_reference(item, &global_hooks));
  1760. }
  1761. cJSON_bool cJSON_AddItemReferenceToObject(cJSON* object, const char* string, cJSON* item)
  1762. {
  1763. if ((object == NULL) || (string == NULL))
  1764. {
  1765. return false;
  1766. }
  1767. return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
  1768. }
  1769. cJSON* cJSON_AddNullToObject(cJSON* const object, const char* const name)
  1770. {
  1771. cJSON* null = cJSON_CreateNull();
  1772. if (add_item_to_object(object, name, null, &global_hooks, false))
  1773. {
  1774. return null;
  1775. }
  1776. cJSON_Delete(null);
  1777. return NULL;
  1778. }
  1779. cJSON* cJSON_AddTrueToObject(cJSON* const object, const char* const name)
  1780. {
  1781. cJSON* true_item = cJSON_CreateTrue();
  1782. if (add_item_to_object(object, name, true_item, &global_hooks, false))
  1783. {
  1784. return true_item;
  1785. }
  1786. cJSON_Delete(true_item);
  1787. return NULL;
  1788. }
  1789. cJSON* cJSON_AddFalseToObject(cJSON* const object, const char* const name)
  1790. {
  1791. cJSON* false_item = cJSON_CreateFalse();
  1792. if (add_item_to_object(object, name, false_item, &global_hooks, false))
  1793. {
  1794. return false_item;
  1795. }
  1796. cJSON_Delete(false_item);
  1797. return NULL;
  1798. }
  1799. cJSON* cJSON_AddBoolToObject(cJSON* const object, const char* const name, const cJSON_bool boolean)
  1800. {
  1801. cJSON* bool_item = cJSON_CreateBool(boolean);
  1802. if (add_item_to_object(object, name, bool_item, &global_hooks, false))
  1803. {
  1804. return bool_item;
  1805. }
  1806. cJSON_Delete(bool_item);
  1807. return NULL;
  1808. }
  1809. cJSON* cJSON_AddNumberToObject(cJSON* const object, const char* const name, const double number)
  1810. {
  1811. cJSON* number_item = cJSON_CreateNumber(number);
  1812. if (add_item_to_object(object, name, number_item, &global_hooks, false))
  1813. {
  1814. return number_item;
  1815. }
  1816. cJSON_Delete(number_item);
  1817. return NULL;
  1818. }
  1819. cJSON* cJSON_AddStringToObject(cJSON* const object, const char* const name, const char* const string)
  1820. {
  1821. cJSON* string_item = cJSON_CreateString(string);
  1822. if (add_item_to_object(object, name, string_item, &global_hooks, false))
  1823. {
  1824. return string_item;
  1825. }
  1826. cJSON_Delete(string_item);
  1827. return NULL;
  1828. }
  1829. cJSON* cJSON_AddRawToObject(cJSON* const object, const char* const name, const char* const raw)
  1830. {
  1831. cJSON* raw_item = cJSON_CreateRaw(raw);
  1832. if (add_item_to_object(object, name, raw_item, &global_hooks, false))
  1833. {
  1834. return raw_item;
  1835. }
  1836. cJSON_Delete(raw_item);
  1837. return NULL;
  1838. }
  1839. cJSON* cJSON_AddObjectToObject(cJSON* const object, const char* const name)
  1840. {
  1841. cJSON* object_item = cJSON_CreateObject();
  1842. if (add_item_to_object(object, name, object_item, &global_hooks, false))
  1843. {
  1844. return object_item;
  1845. }
  1846. cJSON_Delete(object_item);
  1847. return NULL;
  1848. }
  1849. cJSON* cJSON_AddArrayToObject(cJSON* const object, const char* const name)
  1850. {
  1851. cJSON* array = cJSON_CreateArray();
  1852. if (add_item_to_object(object, name, array, &global_hooks, false))
  1853. {
  1854. return array;
  1855. }
  1856. cJSON_Delete(array);
  1857. return NULL;
  1858. }
  1859. cJSON* cJSON_DetachItemViaPointer(cJSON* parent, cJSON* const item)
  1860. {
  1861. if ((parent == NULL) || (item == NULL))
  1862. {
  1863. return NULL;
  1864. }
  1865. if (item != parent->child)
  1866. {
  1867. /* not the first element */
  1868. item->prev->next = item->next;
  1869. }
  1870. if (item->next != NULL)
  1871. {
  1872. /* not the last element */
  1873. item->next->prev = item->prev;
  1874. }
  1875. if (item == parent->child)
  1876. {
  1877. /* first element */
  1878. parent->child = item->next;
  1879. }
  1880. else if (item->next == NULL)
  1881. {
  1882. /* last element */
  1883. parent->child->prev = item->prev;
  1884. }
  1885. /* make sure the detached item doesn't point anywhere anymore */
  1886. item->prev = NULL;
  1887. item->next = NULL;
  1888. return item;
  1889. }
  1890. cJSON* cJSON_DetachItemFromArray(cJSON* array, int which)
  1891. {
  1892. if (which < 0)
  1893. {
  1894. return NULL;
  1895. }
  1896. return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
  1897. }
  1898. void cJSON_DeleteItemFromArray(cJSON* array, int which)
  1899. {
  1900. cJSON_Delete(cJSON_DetachItemFromArray(array, which));
  1901. }
  1902. cJSON* cJSON_DetachItemFromObject(cJSON* object, const char* string)
  1903. {
  1904. cJSON* to_detach = cJSON_GetObjectItem(object, string);
  1905. return cJSON_DetachItemViaPointer(object, to_detach);
  1906. }
  1907. cJSON* cJSON_DetachItemFromObjectCaseSensitive(cJSON* object, const char* string)
  1908. {
  1909. cJSON* to_detach = cJSON_GetObjectItemCaseSensitive(object, string);
  1910. return cJSON_DetachItemViaPointer(object, to_detach);
  1911. }
  1912. void cJSON_DeleteItemFromObject(cJSON* object, const char* string)
  1913. {
  1914. cJSON_Delete(cJSON_DetachItemFromObject(object, string));
  1915. }
  1916. void cJSON_DeleteItemFromObjectCaseSensitive(cJSON* object, const char* string)
  1917. {
  1918. cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string));
  1919. }
  1920. /* Replace array/object items with new ones. */
  1921. cJSON_bool cJSON_InsertItemInArray(cJSON* array, int which, cJSON* newitem)
  1922. {
  1923. cJSON* after_inserted = NULL;
  1924. if (which < 0)
  1925. {
  1926. return false;
  1927. }
  1928. after_inserted = get_array_item(array, (size_t)which);
  1929. if (after_inserted == NULL)
  1930. {
  1931. return add_item_to_array(array, newitem);
  1932. }
  1933. newitem->next = after_inserted;
  1934. newitem->prev = after_inserted->prev;
  1935. after_inserted->prev = newitem;
  1936. if (after_inserted == array->child)
  1937. {
  1938. array->child = newitem;
  1939. }
  1940. else
  1941. {
  1942. newitem->prev->next = newitem;
  1943. }
  1944. return true;
  1945. }
  1946. cJSON_bool cJSON_ReplaceItemViaPointer(cJSON* const parent, cJSON* const item, cJSON* replacement)
  1947. {
  1948. if ((parent == NULL) || (replacement == NULL) || (item == NULL))
  1949. {
  1950. return false;
  1951. }
  1952. if (replacement == item)
  1953. {
  1954. return true;
  1955. }
  1956. replacement->next = item->next;
  1957. replacement->prev = item->prev;
  1958. if (replacement->next != NULL)
  1959. {
  1960. replacement->next->prev = replacement;
  1961. }
  1962. if (parent->child == item)
  1963. {
  1964. if (parent->child->prev == parent->child)
  1965. {
  1966. replacement->prev = replacement;
  1967. }
  1968. parent->child = replacement;
  1969. }
  1970. else
  1971. { /*
  1972. * To find the last item in array quickly, we use prev in array.
  1973. * We can't modify the last item's next pointer where this item was the parent's child
  1974. */
  1975. if (replacement->prev != NULL)
  1976. {
  1977. replacement->prev->next = replacement;
  1978. }
  1979. if (replacement->next == NULL)
  1980. {
  1981. parent->child->prev = replacement;
  1982. }
  1983. }
  1984. item->next = NULL;
  1985. item->prev = NULL;
  1986. cJSON_Delete(item);
  1987. return true;
  1988. }
  1989. cJSON_bool cJSON_ReplaceItemInArray(cJSON* array, int which, cJSON* newitem)
  1990. {
  1991. if (which < 0)
  1992. {
  1993. return false;
  1994. }
  1995. return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem);
  1996. }
  1997. static cJSON_bool replace_item_in_object(cJSON* object, const char* string, cJSON* replacement, cJSON_bool case_sensitive)
  1998. {
  1999. if ((replacement == NULL) || (string == NULL))
  2000. {
  2001. return false;
  2002. }
  2003. /* replace the name in the replacement */
  2004. if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
  2005. {
  2006. cJSON_free(replacement->string);
  2007. }
  2008. replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
  2009. replacement->type &= ~cJSON_StringIsConst;
  2010. return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
  2011. }
  2012. cJSON_bool cJSON_ReplaceItemInObject(cJSON* object, const char* string, cJSON* newitem)
  2013. {
  2014. return replace_item_in_object(object, string, newitem, false);
  2015. }
  2016. cJSON_bool cJSON_ReplaceItemInObjectCaseSensitive(cJSON* object, const char* string, cJSON* newitem)
  2017. {
  2018. return replace_item_in_object(object, string, newitem, true);
  2019. }
  2020. /* Create basic types: */
  2021. cJSON* cJSON_CreateNull(void)
  2022. {
  2023. cJSON* item = cJSON_New_Item(&global_hooks);
  2024. if (item)
  2025. {
  2026. item->type = cJSON_NULL;
  2027. }
  2028. return item;
  2029. }
  2030. cJSON* cJSON_CreateTrue(void)
  2031. {
  2032. cJSON* item = cJSON_New_Item(&global_hooks);
  2033. if (item)
  2034. {
  2035. item->type = cJSON_True;
  2036. }
  2037. return item;
  2038. }
  2039. cJSON* cJSON_CreateFalse(void)
  2040. {
  2041. cJSON* item = cJSON_New_Item(&global_hooks);
  2042. if (item)
  2043. {
  2044. item->type = cJSON_False;
  2045. }
  2046. return item;
  2047. }
  2048. cJSON* cJSON_CreateBool(cJSON_bool boolean)
  2049. {
  2050. cJSON* item = cJSON_New_Item(&global_hooks);
  2051. if (item)
  2052. {
  2053. item->type = boolean ? cJSON_True : cJSON_False;
  2054. }
  2055. return item;
  2056. }
  2057. cJSON* cJSON_CreateNumber(double num)
  2058. {
  2059. cJSON* item = cJSON_New_Item(&global_hooks);
  2060. if (item)
  2061. {
  2062. item->type = cJSON_Number;
  2063. item->valuedouble = num;
  2064. /* use saturation in case of overflow */
  2065. if (num >= INT_MAX)
  2066. {
  2067. item->valueint = INT_MAX;
  2068. }
  2069. else if (num <= (double)INT_MIN)
  2070. {
  2071. item->valueint = INT_MIN;
  2072. }
  2073. else
  2074. {
  2075. item->valueint = (int)num;
  2076. }
  2077. }
  2078. return item;
  2079. }
  2080. cJSON* cJSON_CreateString(const char* string)
  2081. {
  2082. cJSON* item = cJSON_New_Item(&global_hooks);
  2083. if (item)
  2084. {
  2085. item->type = cJSON_String;
  2086. item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
  2087. if (!item->valuestring)
  2088. {
  2089. cJSON_Delete(item);
  2090. return NULL;
  2091. }
  2092. }
  2093. return item;
  2094. }
  2095. cJSON* cJSON_CreateStringReference(const char* string)
  2096. {
  2097. cJSON* item = cJSON_New_Item(&global_hooks);
  2098. if (item != NULL)
  2099. {
  2100. item->type = cJSON_String | cJSON_IsReference;
  2101. item->valuestring = (char*)cast_away_const(string);
  2102. }
  2103. return item;
  2104. }
  2105. cJSON* cJSON_CreateObjectReference(const cJSON* child)
  2106. {
  2107. cJSON* item = cJSON_New_Item(&global_hooks);
  2108. if (item != NULL) {
  2109. item->type = cJSON_Object | cJSON_IsReference;
  2110. item->child = (cJSON*)cast_away_const(child);
  2111. }
  2112. return item;
  2113. }
  2114. cJSON* cJSON_CreateArrayReference(const cJSON* child) {
  2115. cJSON* item = cJSON_New_Item(&global_hooks);
  2116. if (item != NULL) {
  2117. item->type = cJSON_Array | cJSON_IsReference;
  2118. item->child = (cJSON*)cast_away_const(child);
  2119. }
  2120. return item;
  2121. }
  2122. cJSON* cJSON_CreateRaw(const char* raw)
  2123. {
  2124. cJSON* item = cJSON_New_Item(&global_hooks);
  2125. if (item)
  2126. {
  2127. item->type = cJSON_Raw;
  2128. item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
  2129. if (!item->valuestring)
  2130. {
  2131. cJSON_Delete(item);
  2132. return NULL;
  2133. }
  2134. }
  2135. return item;
  2136. }
  2137. cJSON* cJSON_CreateArray(void)
  2138. {
  2139. cJSON* item = cJSON_New_Item(&global_hooks);
  2140. if (item)
  2141. {
  2142. item->type = cJSON_Array;
  2143. }
  2144. return item;
  2145. }
  2146. cJSON* cJSON_CreateObject(void)
  2147. {
  2148. cJSON* item = cJSON_New_Item(&global_hooks);
  2149. if (item)
  2150. {
  2151. item->type = cJSON_Object;
  2152. }
  2153. return item;
  2154. }
  2155. /* Create Arrays: */
  2156. cJSON* cJSON_CreateIntArray(const int* numbers, int count)
  2157. {
  2158. size_t i = 0;
  2159. cJSON* n = NULL;
  2160. cJSON* p = NULL;
  2161. cJSON* a = NULL;
  2162. if ((count < 0) || (numbers == NULL))
  2163. {
  2164. return NULL;
  2165. }
  2166. a = cJSON_CreateArray();
  2167. for (i = 0; a && (i < (size_t)count); i++)
  2168. {
  2169. n = cJSON_CreateNumber(numbers[i]);
  2170. if (!n)
  2171. {
  2172. cJSON_Delete(a);
  2173. return NULL;
  2174. }
  2175. if (!i)
  2176. {
  2177. a->child = n;
  2178. }
  2179. else
  2180. {
  2181. suffix_object(p, n);
  2182. }
  2183. p = n;
  2184. }
  2185. if (a && a->child) {
  2186. a->child->prev = n;
  2187. }
  2188. return a;
  2189. }
  2190. cJSON* cJSON_CreateFloatArray(const float* numbers, int count)
  2191. {
  2192. size_t i = 0;
  2193. cJSON* n = NULL;
  2194. cJSON* p = NULL;
  2195. cJSON* a = NULL;
  2196. if ((count < 0) || (numbers == NULL))
  2197. {
  2198. return NULL;
  2199. }
  2200. a = cJSON_CreateArray();
  2201. for (i = 0; a && (i < (size_t)count); i++)
  2202. {
  2203. n = cJSON_CreateNumber((double)numbers[i]);
  2204. if (!n)
  2205. {
  2206. cJSON_Delete(a);
  2207. return NULL;
  2208. }
  2209. if (!i)
  2210. {
  2211. a->child = n;
  2212. }
  2213. else
  2214. {
  2215. suffix_object(p, n);
  2216. }
  2217. p = n;
  2218. }
  2219. if (a && a->child) {
  2220. a->child->prev = n;
  2221. }
  2222. return a;
  2223. }
  2224. cJSON* cJSON_CreateDoubleArray(const double* numbers, int count)
  2225. {
  2226. size_t i = 0;
  2227. cJSON* n = NULL;
  2228. cJSON* p = NULL;
  2229. cJSON* a = NULL;
  2230. if ((count < 0) || (numbers == NULL))
  2231. {
  2232. return NULL;
  2233. }
  2234. a = cJSON_CreateArray();
  2235. for (i = 0; a && (i < (size_t)count); i++)
  2236. {
  2237. n = cJSON_CreateNumber(numbers[i]);
  2238. if (!n)
  2239. {
  2240. cJSON_Delete(a);
  2241. return NULL;
  2242. }
  2243. if (!i)
  2244. {
  2245. a->child = n;
  2246. }
  2247. else
  2248. {
  2249. suffix_object(p, n);
  2250. }
  2251. p = n;
  2252. }
  2253. if (a && a->child) {
  2254. a->child->prev = n;
  2255. }
  2256. return a;
  2257. }
  2258. cJSON* cJSON_CreateStringArray(const char* const* strings, int count)
  2259. {
  2260. size_t i = 0;
  2261. cJSON* n = NULL;
  2262. cJSON* p = NULL;
  2263. cJSON* a = NULL;
  2264. if ((count < 0) || (strings == NULL))
  2265. {
  2266. return NULL;
  2267. }
  2268. a = cJSON_CreateArray();
  2269. for (i = 0; a && (i < (size_t)count); i++)
  2270. {
  2271. n = cJSON_CreateString(strings[i]);
  2272. if (!n)
  2273. {
  2274. cJSON_Delete(a);
  2275. return NULL;
  2276. }
  2277. if (!i)
  2278. {
  2279. a->child = n;
  2280. }
  2281. else
  2282. {
  2283. suffix_object(p, n);
  2284. }
  2285. p = n;
  2286. }
  2287. if (a && a->child) {
  2288. a->child->prev = n;
  2289. }
  2290. return a;
  2291. }
  2292. /* Duplication */
  2293. cJSON* cJSON_Duplicate(const cJSON* item, cJSON_bool recurse)
  2294. {
  2295. cJSON* newitem = NULL;
  2296. cJSON* child = NULL;
  2297. cJSON* next = NULL;
  2298. cJSON* newchild = NULL;
  2299. /* Bail on bad ptr */
  2300. if (!item)
  2301. {
  2302. goto fail;
  2303. }
  2304. /* Create new item */
  2305. newitem = cJSON_New_Item(&global_hooks);
  2306. if (!newitem)
  2307. {
  2308. goto fail;
  2309. }
  2310. /* Copy over all vars */
  2311. newitem->type = item->type & (~cJSON_IsReference);
  2312. newitem->valueint = item->valueint;
  2313. newitem->valuedouble = item->valuedouble;
  2314. if (item->valuestring)
  2315. {
  2316. newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
  2317. if (!newitem->valuestring)
  2318. {
  2319. goto fail;
  2320. }
  2321. }
  2322. if (item->string)
  2323. {
  2324. newitem->string = (item->type & cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
  2325. if (!newitem->string)
  2326. {
  2327. goto fail;
  2328. }
  2329. }
  2330. /* If non-recursive, then we're done! */
  2331. if (!recurse)
  2332. {
  2333. return newitem;
  2334. }
  2335. /* Walk the ->next chain for the child. */
  2336. child = item->child;
  2337. while (child != NULL)
  2338. {
  2339. newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
  2340. if (!newchild)
  2341. {
  2342. goto fail;
  2343. }
  2344. if (next != NULL)
  2345. {
  2346. /* If newitem->child already set, then crosswire ->prev and ->next and move on */
  2347. next->next = newchild;
  2348. newchild->prev = next;
  2349. next = newchild;
  2350. }
  2351. else
  2352. {
  2353. /* Set newitem->child and move to it */
  2354. newitem->child = newchild;
  2355. next = newchild;
  2356. }
  2357. child = child->next;
  2358. }
  2359. if (newitem && newitem->child)
  2360. {
  2361. newitem->child->prev = newchild;
  2362. }
  2363. return newitem;
  2364. fail:
  2365. if (newitem != NULL)
  2366. {
  2367. cJSON_Delete(newitem);
  2368. }
  2369. return NULL;
  2370. }
  2371. static void skip_oneline_comment(char** input)
  2372. {
  2373. *input += static_strlen("//");
  2374. for (; (*input)[0] != '\0'; ++(*input))
  2375. {
  2376. if ((*input)[0] == '\n') {
  2377. *input += static_strlen("\n");
  2378. return;
  2379. }
  2380. }
  2381. }
  2382. static void skip_multiline_comment(char** input)
  2383. {
  2384. *input += static_strlen("/*");
  2385. for (; (*input)[0] != '\0'; ++(*input))
  2386. {
  2387. if (((*input)[0] == '*') && ((*input)[1] == '/'))
  2388. {
  2389. *input += static_strlen("*/");
  2390. return;
  2391. }
  2392. }
  2393. }
  2394. static void minify_string(char** input, char** output) {
  2395. (*output)[0] = (*input)[0];
  2396. *input += static_strlen("\"");
  2397. *output += static_strlen("\"");
  2398. for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) {
  2399. (*output)[0] = (*input)[0];
  2400. if ((*input)[0] == '\"') {
  2401. (*output)[0] = '\"';
  2402. *input += static_strlen("\"");
  2403. *output += static_strlen("\"");
  2404. return;
  2405. }
  2406. else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) {
  2407. (*output)[1] = (*input)[1];
  2408. *input += static_strlen("\"");
  2409. *output += static_strlen("\"");
  2410. }
  2411. }
  2412. }
  2413. void cJSON_Minify(char* json)
  2414. {
  2415. char* into = json;
  2416. if (json == NULL)
  2417. {
  2418. return;
  2419. }
  2420. while (json[0] != '\0')
  2421. {
  2422. switch (json[0])
  2423. {
  2424. case ' ':
  2425. case '\t':
  2426. case '\r':
  2427. case '\n':
  2428. json++;
  2429. break;
  2430. case '/':
  2431. if (json[1] == '/')
  2432. {
  2433. skip_oneline_comment(&json);
  2434. }
  2435. else if (json[1] == '*')
  2436. {
  2437. skip_multiline_comment(&json);
  2438. }
  2439. else {
  2440. json++;
  2441. }
  2442. break;
  2443. case '\"':
  2444. minify_string(&json, (char**)&into);
  2445. break;
  2446. default:
  2447. into[0] = json[0];
  2448. json++;
  2449. into++;
  2450. }
  2451. }
  2452. /* and null-terminate. */
  2453. *into = '\0';
  2454. }
  2455. cJSON_bool cJSON_IsInvalid(const cJSON* const item)
  2456. {
  2457. if (item == NULL)
  2458. {
  2459. return false;
  2460. }
  2461. return (item->type & 0xFF) == cJSON_Invalid;
  2462. }
  2463. cJSON_bool cJSON_IsFalse(const cJSON* const item)
  2464. {
  2465. if (item == NULL)
  2466. {
  2467. return false;
  2468. }
  2469. return (item->type & 0xFF) == cJSON_False;
  2470. }
  2471. cJSON_bool cJSON_IsTrue(const cJSON* const item)
  2472. {
  2473. if (item == NULL)
  2474. {
  2475. return false;
  2476. }
  2477. return (item->type & 0xff) == cJSON_True;
  2478. }
  2479. cJSON_bool cJSON_IsBool(const cJSON* const item)
  2480. {
  2481. if (item == NULL)
  2482. {
  2483. return false;
  2484. }
  2485. return (item->type & (cJSON_True | cJSON_False)) != 0;
  2486. }
  2487. cJSON_bool cJSON_IsNull(const cJSON* const item)
  2488. {
  2489. if (item == NULL)
  2490. {
  2491. return false;
  2492. }
  2493. return (item->type & 0xFF) == cJSON_NULL;
  2494. }
  2495. cJSON_bool cJSON_IsNumber(const cJSON* const item)
  2496. {
  2497. if (item == NULL)
  2498. {
  2499. return false;
  2500. }
  2501. return (item->type & 0xFF) == cJSON_Number;
  2502. }
  2503. cJSON_bool cJSON_IsString(const cJSON* const item)
  2504. {
  2505. if (item == NULL)
  2506. {
  2507. return false;
  2508. }
  2509. return (item->type & 0xFF) == cJSON_String;
  2510. }
  2511. cJSON_bool cJSON_IsArray(const cJSON* const item)
  2512. {
  2513. if (item == NULL)
  2514. {
  2515. return false;
  2516. }
  2517. return (item->type & 0xFF) == cJSON_Array;
  2518. }
  2519. cJSON_bool cJSON_IsObject(const cJSON* const item)
  2520. {
  2521. if (item == NULL)
  2522. {
  2523. return false;
  2524. }
  2525. return (item->type & 0xFF) == cJSON_Object;
  2526. }
  2527. cJSON_bool cJSON_IsRaw(const cJSON* const item)
  2528. {
  2529. if (item == NULL)
  2530. {
  2531. return false;
  2532. }
  2533. return (item->type & 0xFF) == cJSON_Raw;
  2534. }
  2535. cJSON_bool cJSON_Compare(const cJSON* const a, const cJSON* const b, const cJSON_bool case_sensitive)
  2536. {
  2537. if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
  2538. {
  2539. return false;
  2540. }
  2541. /* check if type is valid */
  2542. switch (a->type & 0xFF)
  2543. {
  2544. case cJSON_False:
  2545. case cJSON_True:
  2546. case cJSON_NULL:
  2547. case cJSON_Number:
  2548. case cJSON_String:
  2549. case cJSON_Raw:
  2550. case cJSON_Array:
  2551. case cJSON_Object:
  2552. break;
  2553. default:
  2554. return false;
  2555. }
  2556. /* identical objects are equal */
  2557. if (a == b)
  2558. {
  2559. return true;
  2560. }
  2561. switch (a->type & 0xFF)
  2562. {
  2563. /* in these cases and equal type is enough */
  2564. case cJSON_False:
  2565. case cJSON_True:
  2566. case cJSON_NULL:
  2567. return true;
  2568. case cJSON_Number:
  2569. if (compare_double(a->valuedouble, b->valuedouble))
  2570. {
  2571. return true;
  2572. }
  2573. return false;
  2574. case cJSON_String:
  2575. case cJSON_Raw:
  2576. if ((a->valuestring == NULL) || (b->valuestring == NULL))
  2577. {
  2578. return false;
  2579. }
  2580. if (strcmp(a->valuestring, b->valuestring) == 0)
  2581. {
  2582. return true;
  2583. }
  2584. return false;
  2585. case cJSON_Array:
  2586. {
  2587. cJSON* a_element = a->child;
  2588. cJSON* b_element = b->child;
  2589. for (; (a_element != NULL) && (b_element != NULL);)
  2590. {
  2591. if (!cJSON_Compare(a_element, b_element, case_sensitive))
  2592. {
  2593. return false;
  2594. }
  2595. a_element = a_element->next;
  2596. b_element = b_element->next;
  2597. }
  2598. /* one of the arrays is longer than the other */
  2599. if (a_element != b_element) {
  2600. return false;
  2601. }
  2602. return true;
  2603. }
  2604. case cJSON_Object:
  2605. {
  2606. cJSON* a_element = NULL;
  2607. cJSON* b_element = NULL;
  2608. cJSON_ArrayForEach(a_element, a)
  2609. {
  2610. /* TODO This has O(n^2) runtime, which is horrible! */
  2611. b_element = get_object_item(b, a_element->string, case_sensitive);
  2612. if (b_element == NULL)
  2613. {
  2614. return false;
  2615. }
  2616. if (!cJSON_Compare(a_element, b_element, case_sensitive))
  2617. {
  2618. return false;
  2619. }
  2620. }
  2621. /* doing this twice, once on a and b to prevent true comparison if a subset of b
  2622. * TODO: Do this the proper way, this is just a fix for now */
  2623. cJSON_ArrayForEach(b_element, b)
  2624. {
  2625. a_element = get_object_item(a, b_element->string, case_sensitive);
  2626. if (a_element == NULL)
  2627. {
  2628. return false;
  2629. }
  2630. if (!cJSON_Compare(b_element, a_element, case_sensitive))
  2631. {
  2632. return false;
  2633. }
  2634. }
  2635. return true;
  2636. }
  2637. default:
  2638. return false;
  2639. }
  2640. }
  2641. void* cJSON_malloc(size_t size)
  2642. {
  2643. return global_hooks.allocate(size);
  2644. }
  2645. void cJSON_free(void* object)
  2646. {
  2647. global_hooks.deallocate(object);
  2648. }
  2649. #endif
  2650. ylib::json::json()
  2651. {
  2652. m_empty = false;
  2653. m_bool = false;
  2654. m_array = false;
  2655. m_obj = false;
  2656. m_string = false;
  2657. m_number = false;
  2658. m_null = false;
  2659. m_safe = false;
  2660. m_value_double = 0;
  2661. m_value_bool = false;
  2662. }
  2663. ylib::json::json(const std::string& value)
  2664. {
  2665. json();
  2666. operator=(value);
  2667. }
  2668. ylib::json::json(const bool& value)
  2669. {
  2670. json();
  2671. operator=(value);
  2672. }
  2673. ylib::json::json(const double& value)
  2674. {
  2675. json();
  2676. operator=(value);
  2677. }
  2678. ylib::json::json(const float& value)
  2679. {
  2680. json();
  2681. operator=(value);
  2682. }
  2683. ylib::json::json(const char* value)
  2684. {
  2685. json();
  2686. operator=(value);
  2687. }
  2688. ylib::json::json(const uint32& value)
  2689. {
  2690. json();
  2691. operator=(value);
  2692. }
  2693. ylib::json::json(const int32& value)
  2694. {
  2695. json();
  2696. operator=(value);
  2697. }
  2698. ylib::json::json(const uint64& value)
  2699. {
  2700. json();
  2701. operator=(value);
  2702. }
  2703. ylib::json::json(const int64& value)
  2704. {
  2705. json();
  2706. operator=(value);
  2707. }
  2708. ylib::json::json(const ylib::json& value)
  2709. {
  2710. json();
  2711. operator=(value);
  2712. }
  2713. ylib::json::json(cJSON* json, const std::string& name)
  2714. {
  2715. erase();
  2716. m_name = name;
  2717. init(json);
  2718. }
  2719. ylib::json::json(const std::vector<uint32>& value)
  2720. {
  2721. json();
  2722. operator=(value);
  2723. }
  2724. ylib::json::json(const std::vector<int32>& value)
  2725. {
  2726. json();
  2727. operator=(value);
  2728. }
  2729. ylib::json::json(const std::vector<uint64>& value)
  2730. {
  2731. json();
  2732. operator=(value);
  2733. }
  2734. ylib::json::json(const std::vector<int64>& value)
  2735. {
  2736. json();
  2737. operator=(value);
  2738. }
  2739. ylib::json::json(const std::vector<const char*>& value)
  2740. {
  2741. json();
  2742. operator=(value);
  2743. }
  2744. ylib::json::json(const std::vector<std::string>& value)
  2745. {
  2746. json();
  2747. operator=(value);
  2748. }
  2749. ylib::json::json(const std::vector<bool>& value)
  2750. {
  2751. json();
  2752. operator=(value);
  2753. }
  2754. ylib::json::json(const std::vector<double>& value)
  2755. {
  2756. json();
  2757. operator=(value);
  2758. }
  2759. ylib::json::json(const std::vector<float>& value)
  2760. {
  2761. json();
  2762. operator=(value);
  2763. }
  2764. ylib::json::json(const std::vector<ylib::json>& value)
  2765. {
  2766. json();
  2767. operator=(value);
  2768. }
  2769. ylib::json::~json()
  2770. {
  2771. erase();
  2772. }
  2773. bool ylib::json::parse(const std::string& value)
  2774. {
  2775. return __parse(value.c_str(),value.length());
  2776. }
  2777. void ylib::json::safe(bool safe)
  2778. {
  2779. m_safe = true;
  2780. }
  2781. bool ylib::json::safe()
  2782. {
  2783. return m_safe;
  2784. }
  2785. ylib::json ylib::json::from(const std::string& value)
  2786. {
  2787. ylib::json result;
  2788. result.__parse(value.c_str(),value.length());
  2789. return result;
  2790. }
  2791. void ylib::json::erase()
  2792. {
  2793. for (uint32 i = 0;i < m_arrs.size();i++)
  2794. delete m_arrs[i];
  2795. for_iter(iter, m_objs)
  2796. delete iter->second;
  2797. m_arrs.clear();
  2798. m_objs.clear();
  2799. clear_type();
  2800. }
  2801. cJSON* ylib::json::arrangement() const
  2802. {
  2803. cJSON* result = nullptr;
  2804. if (is_null())
  2805. result = cJSON_CreateNull();
  2806. else if (is_array())
  2807. {
  2808. result = cJSON_CreateArray();
  2809. for(uint32 i=0;i<m_arrs.size();i++)
  2810. {
  2811. cJSON_AddItemToArray(result, m_arrs[i]->arrangement());
  2812. }
  2813. }
  2814. else if (is_obj())
  2815. {
  2816. result = cJSON_CreateObject();
  2817. for_iter(iter, m_objs)
  2818. {
  2819. cJSON_AddItemToObject(result, iter->first.c_str(), iter->second->arrangement());
  2820. }
  2821. }
  2822. else if (is_number())
  2823. {
  2824. result = cJSON_CreateNumber(m_value_double);
  2825. }
  2826. else if (is_string())
  2827. {
  2828. result = cJSON_CreateString(m_value_string.c_str());
  2829. }
  2830. else if (is_bool())
  2831. {
  2832. result = cJSON_CreateBool(m_value_bool);
  2833. }
  2834. return result;
  2835. }
  2836. uint32 ylib::json::size() const
  2837. {
  2838. return m_arrs.size();
  2839. }
  2840. void ylib::json::resize(uint32 size)
  2841. {
  2842. erase();
  2843. m_array = true;
  2844. for (uint32 i = 0;i < size;i++)
  2845. m_arrs.push_back(new ylib::json());
  2846. }
  2847. bool ylib::json::is_empty() const
  2848. {
  2849. return m_empty;
  2850. }
  2851. bool ylib::json::is_null() const
  2852. {
  2853. return m_null;
  2854. }
  2855. bool ylib::json::is_obj() const
  2856. {
  2857. return m_obj;
  2858. }
  2859. bool ylib::json::is_array() const
  2860. {
  2861. return m_array;
  2862. }
  2863. bool ylib::json::is_bool() const
  2864. {
  2865. return m_bool;
  2866. }
  2867. bool ylib::json::is_number() const
  2868. {
  2869. return m_number;
  2870. }
  2871. bool ylib::json::is_string() const
  2872. {
  2873. return m_string;
  2874. }
  2875. bool ylib::json::exist(const std::string& name) const
  2876. {
  2877. return m_objs.find(name) != m_objs.end();
  2878. }
  2879. void ylib::json::erase(const std::string& key)
  2880. {
  2881. auto iter = this->m_objs.find(key);
  2882. if (iter != this->m_objs.end())
  2883. {
  2884. delete iter->second;
  2885. this->m_objs.erase(iter);
  2886. }
  2887. }
  2888. void ylib::json::erase(uint32 index)
  2889. {
  2890. if (this->m_arrs.size() <= index)
  2891. return;
  2892. delete this->m_arrs[index];
  2893. m_arrs.erase(this->m_arrs.begin()+index);
  2894. }
  2895. std::vector<std::string> ylib::json::keys() const
  2896. {
  2897. std::vector<std::string> key;
  2898. for_iter(iter, m_objs)
  2899. key.push_back(iter->first);
  2900. return key;
  2901. }
  2902. const std::string& ylib::json::to_string(bool format) const
  2903. {
  2904. ((std::string*)(&m_jsonstring))->clear();
  2905. cJSON* json_ptr = arrangement();
  2906. if (json_ptr == nullptr)
  2907. return m_jsonstring;
  2908. char* text = nullptr;
  2909. if(format)
  2910. text = cJSON_Print(json_ptr);
  2911. else
  2912. text = cJSON_PrintUnformatted(json_ptr);
  2913. if (text != nullptr)
  2914. {
  2915. ((std::string*)(&m_jsonstring))->append(text);
  2916. mem::free(text);
  2917. }
  2918. cJSON_Delete(json_ptr);
  2919. return m_jsonstring;
  2920. }
  2921. ylib::json& ylib::json::operator[](const std::string name)
  2922. {
  2923. m_obj = true;
  2924. auto iter = m_objs.find(name);
  2925. if (iter == m_objs.end())
  2926. {
  2927. m_objs.insert(std::pair<std::string,ylib::json*>(name, new ylib::json(nullptr,name)));
  2928. }
  2929. return *m_objs[name];
  2930. }
  2931. const ylib::json& ylib::json::operator[](const std::string name) const
  2932. {
  2933. static ylib::json null_json;
  2934. null_json.clear();
  2935. null_json.m_name = name;
  2936. auto iter = m_objs.find(name);
  2937. return iter == m_objs.end() ? null_json : *iter->second;
  2938. }
  2939. ylib::json& ylib::json::operator[](const size_t idx)
  2940. {
  2941. clear_type();
  2942. m_array = true;
  2943. if (idx >= m_arrs.size())
  2944. {
  2945. throw ylib::exception("index out of bounds,index:" + std::to_string(idx));
  2946. //THROW_FORMAT("index out of bounds,index:{}",idx);
  2947. }
  2948. m_array = true;
  2949. return *m_arrs[idx];
  2950. }
  2951. const ylib::json& ylib::json::operator[](const size_t idx) const
  2952. {
  2953. if (idx >= m_arrs.size())
  2954. {
  2955. //return ylib::json();
  2956. throw ylib::exception("index out of bounds,index:" + std::to_string(idx));
  2957. }
  2958. return *m_arrs[idx];
  2959. }
  2960. void ylib::json::operator=(ylib::json::type t)
  2961. {
  2962. erase();
  2963. if (t == ylib::json::array)
  2964. m_array = true;
  2965. else if (t == ylib::json::boolean)
  2966. m_bool = true;
  2967. else if (t == ylib::json::empty)
  2968. m_empty = true;
  2969. else if (t == ylib::json::null)
  2970. m_null = true;
  2971. else if (t == ylib::json::number)
  2972. m_number = true;
  2973. else if (t == ylib::json::obj)
  2974. m_obj = true;
  2975. else if (t == ylib::json::string)
  2976. m_string = true;
  2977. }
  2978. void ylib::json::operator=(const ylib::json& value)
  2979. {
  2980. this->erase();
  2981. for (uint32 i = 0;i < value.m_arrs.size();i++)
  2982. this->m_arrs.push_back(new ylib::json(*value.m_arrs[i]));
  2983. for_iter(iter, value.m_objs)
  2984. m_objs.insert(std::pair<std::string, ylib::json*>(iter->first, new ylib::json(*iter->second)));
  2985. this->m_array = value.m_array;
  2986. this->m_bool = value.m_bool;
  2987. this->m_empty = value.m_empty;
  2988. this->m_jsonstring = value.m_jsonstring;
  2989. this->m_name = value.m_name;
  2990. this->m_null = value.m_null;
  2991. this->m_number = value.m_number;
  2992. this->m_obj = value.m_obj;
  2993. this->m_string = value.m_string;
  2994. this->m_value_bool = value.m_value_bool;
  2995. this->m_value_double = value.m_value_double;
  2996. this->m_value_string = value.m_value_string;
  2997. }
  2998. void ylib::json::push_back(uint32 value)
  2999. {
  3000. clear_type();
  3001. m_array = true;
  3002. this->m_arrs.push_back(new json(value));
  3003. }
  3004. void ylib::json::push_back(int32 value)
  3005. {
  3006. clear_type();
  3007. m_array = true;
  3008. this->m_arrs.push_back(new json(value));
  3009. }
  3010. void ylib::json::push_back(uint64 value)
  3011. {
  3012. clear_type();
  3013. m_array = true;
  3014. this->m_arrs.push_back(new json(value));
  3015. }
  3016. void ylib::json::push_back(int64 value)
  3017. {
  3018. clear_type();
  3019. m_array = true;
  3020. this->m_arrs.push_back(new json(value));
  3021. }
  3022. void ylib::json::push_back(std::string value)
  3023. {
  3024. clear_type();
  3025. m_array = true;
  3026. this->m_arrs.push_back(new json(value));
  3027. }
  3028. void ylib::json::push_back(const bool value)
  3029. {
  3030. clear_type();
  3031. m_array = true;
  3032. this->m_arrs.push_back(new json(value));
  3033. }
  3034. void ylib::json::push_back(double value)
  3035. {
  3036. clear_type();
  3037. m_array = true;
  3038. this->m_arrs.push_back(new json(value));
  3039. }
  3040. void ylib::json::push_back(float value)
  3041. {
  3042. clear_type();
  3043. m_array = true;
  3044. this->m_arrs.push_back(new json(value));
  3045. }
  3046. void ylib::json::push_back(const char* value)
  3047. {
  3048. clear_type();
  3049. m_array = true;
  3050. this->m_arrs.push_back(new json(value));
  3051. }
  3052. void ylib::json::push_back(const ylib::json& value)
  3053. {
  3054. clear_type();
  3055. m_array = true;
  3056. this->m_arrs.push_back(new json(value));
  3057. }
  3058. void ylib::json::operator=(const std::vector<uint32>& value)
  3059. {
  3060. erase();
  3061. m_array = true;
  3062. for (uint32 i = 0;i < value.size();i++)
  3063. {
  3064. auto j = new ylib::json;
  3065. j->m_number = true;
  3066. j->m_value_double = (double)value[i];
  3067. this->m_arrs.push_back(j);
  3068. }
  3069. }
  3070. void ylib::json::operator=(const std::vector<int32>& value)
  3071. {
  3072. erase();
  3073. m_array = true;
  3074. for (uint32 i = 0;i < value.size();i++)
  3075. {
  3076. auto j = new ylib::json;
  3077. j->m_number = true;
  3078. j->m_value_double = (double)value[i];
  3079. this->m_arrs.push_back( j);
  3080. }
  3081. }
  3082. void ylib::json::operator=(const std::vector<uint64>& value)
  3083. {
  3084. erase();
  3085. m_array = true;
  3086. for (uint32 i = 0;i < value.size();i++)
  3087. {
  3088. auto j = new ylib::json;
  3089. j->m_number = true;
  3090. j->m_value_double = (double)value[i];
  3091. this->m_arrs.push_back(j);
  3092. }
  3093. }
  3094. void ylib::json::operator=(const std::vector<int64>& value)
  3095. {
  3096. erase();
  3097. m_array = true;
  3098. for (uint32 i = 0;i < value.size();i++)
  3099. {
  3100. auto j = new ylib::json;
  3101. j->m_number = true;
  3102. j->m_value_double = (double)value[i];
  3103. this->m_arrs.push_back(j);
  3104. }
  3105. }
  3106. void ylib::json::operator=(const std::vector<bool>& value)
  3107. {
  3108. erase();
  3109. m_array = true;
  3110. for (uint32 i = 0;i < value.size();i++)
  3111. {
  3112. auto j = new ylib::json;
  3113. j->m_bool = true;
  3114. j->m_value_bool = value[i];
  3115. this->m_arrs.push_back(j);
  3116. }
  3117. }
  3118. void ylib::json::operator=(const std::vector<double>& value)
  3119. {
  3120. erase();
  3121. m_array = true;
  3122. for (uint32 i = 0;i < value.size();i++)
  3123. {
  3124. auto j = new ylib::json;
  3125. j->m_number = true;
  3126. j->m_value_double = (double)value[i];
  3127. this->m_arrs.push_back(j);
  3128. }
  3129. }
  3130. void ylib::json::operator=(const std::vector<float>& value)
  3131. {
  3132. erase();
  3133. m_array = true;
  3134. for (uint32 i = 0;i < value.size();i++)
  3135. {
  3136. auto j = new ylib::json;
  3137. j->m_number = true;
  3138. j->m_value_double = (double)value[i];
  3139. this->m_arrs.push_back(j);
  3140. }
  3141. }
  3142. void ylib::json::operator=(const std::vector<const char*>& value)
  3143. {
  3144. erase();
  3145. m_array = true;
  3146. for (uint32 i = 0;i < value.size();i++)
  3147. {
  3148. auto j = new ylib::json;
  3149. j->m_string = true;
  3150. j->m_value_string = value[i];
  3151. this->m_arrs.push_back(j);
  3152. }
  3153. }
  3154. void ylib::json::operator=(const std::vector<std::string>& value)
  3155. {
  3156. erase();
  3157. m_array = true;
  3158. for (uint32 i = 0;i < value.size();i++)
  3159. {
  3160. auto j = new ylib::json;
  3161. j->m_string = true;
  3162. j->m_value_string = value[i];
  3163. this->m_arrs.push_back(j);
  3164. }
  3165. }
  3166. void ylib::json::operator=(const std::vector<ylib::json>& value)
  3167. {
  3168. erase();
  3169. m_array = true;
  3170. for (uint32 i = 0;i < value.size();i++)
  3171. {
  3172. auto j = new ylib::json;
  3173. *j = value[i];
  3174. this->m_arrs.push_back(j);
  3175. }
  3176. }
  3177. #define OPERATOR_INIT_TO_VECTOR(TYPE) \
  3178. std::vector<TYPE> vt; \
  3179. for_iter(iter, value) \
  3180. vt.push_back(*iter); \
  3181. operator=(vt)
  3182. void ylib::json::operator=(std::initializer_list<ylib::json> value)
  3183. {
  3184. OPERATOR_INIT_TO_VECTOR(ylib::json);
  3185. }
  3186. void ylib::json::operator=(std::map<const char*, ylib::json> value)
  3187. {
  3188. erase();
  3189. m_obj = true;
  3190. for_iter(iter, value)
  3191. m_objs.insert(std::pair<std::string, ylib::json*>(iter->first, new ylib::json(iter->second)));
  3192. }
  3193. void ylib::json::operator=(uint32 value)
  3194. {
  3195. operator=((uint64)value);
  3196. }
  3197. void ylib::json::operator=(int32 value)
  3198. {
  3199. operator=((uint64)value);
  3200. }
  3201. void ylib::json::operator=(uint64 value)
  3202. {
  3203. erase();
  3204. m_number = true;
  3205. m_value_double = (double)value;
  3206. }
  3207. void ylib::json::operator=(int64 value)
  3208. {
  3209. operator=((uint64)value);
  3210. }
  3211. void ylib::json::operator=(std::string value)
  3212. {
  3213. erase();
  3214. m_string = true;
  3215. m_value_string = value;
  3216. }
  3217. void ylib::json::operator=(const char* value)
  3218. {
  3219. erase();
  3220. m_string = true;
  3221. m_value_string = value;
  3222. }
  3223. void ylib::json::operator=(const bool value)
  3224. {
  3225. erase();
  3226. m_bool = true;
  3227. m_value_bool = value;
  3228. }
  3229. void ylib::json::operator=(double value)
  3230. {
  3231. erase();
  3232. m_number = true;
  3233. m_value_double = (double)value;
  3234. }
  3235. void ylib::json::operator=(float value)
  3236. {
  3237. operator=((double)value);
  3238. }
  3239. void ylib::json::init(cJSON* item)
  3240. {
  3241. erase();
  3242. if (item == nullptr)
  3243. return;
  3244. if (cJSON_IsInvalid(item))
  3245. {
  3246. m_empty = true;
  3247. return;
  3248. }
  3249. if (cJSON_IsNull(item))
  3250. {
  3251. m_null = true;
  3252. return;
  3253. }
  3254. m_bool = cJSON_IsBool(item);
  3255. m_number = cJSON_IsNumber(item);
  3256. m_obj = cJSON_IsObject(item);
  3257. m_string = cJSON_IsString(item);
  3258. m_array = cJSON_IsArray(item);
  3259. m_empty = false;
  3260. if (is_array())
  3261. {
  3262. uint32 count = cJSON_GetArraySize(item);
  3263. for (uint32 i = 0;i < count;i++)
  3264. this->m_arrs.push_back(new json(cJSON_GetArrayItem(item, i),"Index:"+std::to_string(i)));
  3265. }
  3266. else if (is_obj())
  3267. {
  3268. cJSON* item2 = cJSON_GetArrayItem(item, 0);
  3269. while (item2) {
  3270. this->m_objs.insert(std::pair<std::string,ylib::json*>(item2->string, new json(item2,item2->string)));
  3271. item2 = item2->next;
  3272. }
  3273. }
  3274. else
  3275. {
  3276. if(item->string != NULL)
  3277. m_name = item->string;
  3278. if (m_number)
  3279. {
  3280. m_value_double = item->valuedouble;
  3281. }
  3282. else if (m_string)
  3283. {
  3284. m_value_string = item->valuestring;
  3285. }
  3286. else if (m_bool)
  3287. {
  3288. m_value_bool = item->valueint;
  3289. }
  3290. }
  3291. }
  3292. bool ylib::json::is_newobj()
  3293. {
  3294. return !m_empty && !m_bool && !m_array && !m_obj && !m_string && !m_number && !m_null;
  3295. }
  3296. void ylib::json::clear_type()
  3297. {
  3298. m_empty = true;
  3299. m_bool = false;
  3300. m_array = false;
  3301. m_obj = false;
  3302. m_string = false;
  3303. m_number = false;
  3304. m_null = false;
  3305. }
  3306. bool ylib::json::__parse(const char* value, uint32 length)
  3307. {
  3308. erase();
  3309. auto item = cJSON_ParseWithLength(value,length);
  3310. if(item == nullptr)
  3311. return false;
  3312. init(item);
  3313. cJSON_Delete(item);
  3314. return true;
  3315. }
  3316. void ylib::json::clear()
  3317. {
  3318. erase();
  3319. }