json.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698
  1. #define __STDC_WANT_LIB_EXT2__ 1
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <stdarg.h>
  7. #include <ctype.h>
  8. #include <limits.h>
  9. #include "json.h"
  10. #include "MurmurHash3.h"
  11. #define MURMUR_SEED 718281828
  12. enum token_type {
  13. TOKEN_NONE = 0,
  14. TOKEN_ARRAY_START,
  15. TOKEN_ARRAY_END,
  16. TOKEN_OBJ_START,
  17. TOKEN_OBJ_END,
  18. TOKEN_STRING,
  19. TOKEN_NUMBER,
  20. TOKEN_TRUE,
  21. TOKEN_FALSE,
  22. TOKEN_NULL,
  23. TOKEN_INFINITY,
  24. TOKEN_UNDEFINED,
  25. TOKEN_NAN,
  26. TOKEN_LABEL,
  27. TOKEN_COMMA,
  28. TOKEN_COLON,
  29. TOKEN_COMMENT
  30. };
  31. struct json_obj_field {
  32. uint64_t hash;
  33. char* key;
  34. struct json_value* value;
  35. };
  36. struct token {
  37. enum token_type tokenType;
  38. struct json_value* val;
  39. };
  40. struct json_parser {
  41. int error;
  42. char* err_str;
  43. // lexing info
  44. char* source;
  45. char* end;
  46. size_t source_len;
  47. int eoi;
  48. int gotToken;
  49. char* head;
  50. int line_num; // these are 1-based
  51. int char_num;
  52. struct token cur_tok;
  53. // parsing info
  54. struct json_value** stack;
  55. int stack_cnt;
  56. int stack_alloc;
  57. };
  58. static void json_parser_free(struct json_parser* jp) {
  59. if(jp->err_str) free(jp->err_str);
  60. // if(jp->source) free(jp->source); // freed externally
  61. if(jp->stack) free(jp->stack);
  62. }
  63. // sentinels for the parser stack
  64. // the slot above contains an array, merge into it
  65. struct json_value* RESUME_ARRAY = (struct json_value*)&RESUME_ARRAY;
  66. // the slot above contains a label, the slot above that contains the object to merge into
  67. struct json_value* RESUME_OBJ = (struct json_value*)&RESUME_OBJ;
  68. // proper, safe bottom of the stack
  69. struct json_value* ROOT_VALUE = (struct json_value*)&ROOT_VALUE;
  70. static void dbg_print_token(struct token* ts);
  71. static void dbg_dump_stack(struct json_parser* jp, int depth);
  72. int json_array_push_tail(struct json_value* a, struct json_value* val) {
  73. struct json_link* node;
  74. node = malloc(sizeof(*node));
  75. if(!node) return 1;
  76. node->next = NULL;
  77. node->prev = a->arr.tail;
  78. node->v = val;
  79. if(a->len == 0) {
  80. a->arr.head = node;
  81. }
  82. else {
  83. a->arr.tail->next = node;
  84. }
  85. a->arr.tail = node;
  86. a->len++;
  87. return 0;
  88. }
  89. struct json_value* json_array_pop_tail(struct json_value* a) {
  90. struct json_value* v;
  91. struct json_link* t;
  92. if(a->len == 0) {
  93. return NULL;
  94. }
  95. a->len--;
  96. v = a->arr.tail->v;
  97. if(a->len > 0) {
  98. t = a->arr.tail;
  99. a->arr.tail = a->arr.tail->prev;
  100. a->arr.tail->next = NULL;
  101. free(t);
  102. }
  103. else {
  104. a->arr.head = a->arr.tail = NULL;
  105. }
  106. return v;
  107. }
  108. int json_array_push_head(struct json_value* a, struct json_value* val) {
  109. struct json_link* node;
  110. node = malloc(sizeof(*node));
  111. if(!node) return 1;
  112. node->prev = NULL;
  113. node->next = a->arr.head;
  114. node->v = val;
  115. if(a->len == 0) {
  116. a->arr.tail = node;
  117. }
  118. else {
  119. a->arr.head->next = node;
  120. }
  121. a->arr.head = node;
  122. a->len++;
  123. return 0;
  124. }
  125. // pops the tail
  126. struct json_value* json_array_pop_head(struct json_value* a) {
  127. struct json_value* v;
  128. struct json_link* t;
  129. if(a->len == 0) {
  130. return NULL;
  131. }
  132. a->len--;
  133. v = a->arr.head->v;
  134. if(a->len > 0) {
  135. t = a->arr.head;
  136. a->arr.head = a->arr.head->prev;
  137. a->arr.head->next = NULL;
  138. free(t);
  139. }
  140. else {
  141. a->arr.tail = a->arr.head = NULL;
  142. }
  143. return v;
  144. }
  145. size_t json_array_calc_length(struct json_value* a) {
  146. size_t len;
  147. struct json_link* n;
  148. if(a->type != JSON_TYPE_ARRAY) {
  149. return 0;
  150. }
  151. len = 0;
  152. n = a->arr.head;
  153. while(n) {
  154. len++;
  155. n = n->next;
  156. }
  157. // update the cached value too
  158. a->len = len;
  159. return len;
  160. }
  161. // uses a truncated 128bit murmur3 hash
  162. static uint64_t hash_key(char* key, intptr_t len) {
  163. uint64_t hash[2];
  164. // len is optional
  165. if(len == -1) len = strlen(key);
  166. MurmurHash3_x64_128(key, len, MURMUR_SEED, hash);
  167. return hash[0];
  168. }
  169. static int64_t find_bucket(struct json_value* obj, uint64_t hash, char* key) {
  170. int64_t startBucket, bi;
  171. bi = startBucket = hash % obj->obj.alloc_size;
  172. do {
  173. struct json_obj_field* bucket;
  174. bucket = &obj->obj.buckets[bi];
  175. // empty bucket
  176. if(bucket->key == NULL) {
  177. return bi;
  178. }
  179. if(bucket->hash == hash) {
  180. if(!strcmp(key, bucket->key)) {
  181. // bucket is the right one and contains a value already
  182. return bi;
  183. }
  184. // collision, probe next bucket
  185. }
  186. bi = (bi + 1) % obj->obj.alloc_size;
  187. } while(bi != startBucket);
  188. printf("CJSON: error in find_bucket\n");
  189. // should never reach here if the table is maintained properly
  190. return -1;
  191. }
  192. // should always be called with a power of two
  193. static int json_obj_resize(struct json_value* obj, int newSize) {
  194. struct json_obj_field* old, *op;
  195. size_t oldlen = obj->obj.alloc_size;
  196. int64_t n, bi;
  197. size_t i;
  198. old = op = obj->obj.buckets;
  199. obj->obj.alloc_size = newSize;
  200. obj->obj.buckets = calloc(1, sizeof(*obj->obj.buckets) * newSize);
  201. if(!obj->obj.buckets) return 1;
  202. for(i = 0, n = 0; i < oldlen && n < (int64_t)obj->len; i++) {
  203. if(op->key == NULL) {
  204. op++;
  205. continue;
  206. }
  207. bi = find_bucket(obj, op->hash, op->key);
  208. obj->obj.buckets[bi].value = op->value;
  209. obj->obj.buckets[bi].hash = op->hash;
  210. obj->obj.buckets[bi].key = op->key;
  211. n++;
  212. op++;
  213. }
  214. free(old);
  215. return 0;
  216. }
  217. // TODO: better return values and missing key handling
  218. // returns 0 if val is set to the value
  219. // *val == NULL && return > 0 means the key was not found;
  220. int json_obj_get_key(struct json_value* obj, char* key, struct json_value** val) {
  221. uint64_t hash;
  222. int64_t bi;
  223. hash = hash_key(key, -1);
  224. bi = find_bucket(obj, hash, key);
  225. if(bi < 0 || obj->obj.buckets[bi].key == NULL) {
  226. *val = NULL;
  227. return 1;
  228. }
  229. *val = obj->obj.buckets[bi].value;
  230. return 0;
  231. }
  232. // zero for success
  233. int json_obj_set_key(struct json_value* obj, char* key, struct json_value* val) {
  234. char* kd = strdup(key);
  235. int res = json_obj_set_key_nodup(obj, kd, val);
  236. if(res) free(kd);
  237. return res;
  238. }
  239. int json_obj_set_key_nodup(struct json_value* obj, char* key, struct json_value* val) {
  240. uint64_t hash;
  241. int64_t bi;
  242. // check size and grow if necessary
  243. if((float)obj->len / (float)obj->obj.alloc_size >= 0.75) {
  244. json_obj_resize(obj, obj->obj.alloc_size * 2);
  245. }
  246. hash = hash_key(key, -1);
  247. bi = find_bucket(obj, hash, key);
  248. if(bi < 0) return 1;
  249. obj->obj.buckets[bi].value = val;
  250. obj->obj.buckets[bi].key = key;
  251. obj->obj.buckets[bi].hash = hash;
  252. obj->len++;
  253. return 0;
  254. }
  255. char* json_obj_get_strdup(struct json_value* obj, char* key) {
  256. char* s = json_obj_get_str(obj, key);
  257. return s ? strdup(s) : NULL;
  258. }
  259. // returns pointer to the internal string, or null if it's not a string
  260. char* json_obj_get_str(struct json_value* obj, char* key) {
  261. json_value_t* val;
  262. if(json_obj_get_key(obj, key, &val)) {
  263. return NULL;
  264. }
  265. if(val->type != JSON_TYPE_STRING) {
  266. return NULL;
  267. }
  268. return val->s;
  269. }
  270. // returns an integer or the default value if it's not an integer
  271. int64_t json_obj_get_int(struct json_value* obj, char* key, int64_t def) {
  272. json_value_t* val;
  273. if(json_obj_get_key(obj, key, &val)) {
  274. return def;
  275. }
  276. if(val->type != JSON_TYPE_INT) {
  277. return def;
  278. }
  279. return val->n;
  280. }
  281. // returns a double or the default value if it's not an integer
  282. double json_obj_get_double(struct json_value* obj, char* key, double def) {
  283. json_value_t* val;
  284. if(json_obj_get_key(obj, key, &val)) {
  285. return def;
  286. }
  287. if(val->type != JSON_TYPE_DOUBLE) {
  288. return def;
  289. }
  290. return val->d;
  291. }
  292. // returns the json_value struct for a key, or null if it doesn't exist
  293. struct json_value* json_obj_get_val(struct json_value* obj, char* key) {
  294. json_value_t* val;
  295. if(json_obj_get_key(obj, key, &val)) {
  296. return NULL;
  297. }
  298. return val;
  299. }
  300. // iteration. no order. results undefined if modified while iterating
  301. // returns 0 when there is none left
  302. // set iter to NULL to start
  303. int json_obj_next(struct json_value* obj, void** iter, char** key, struct json_value** value) {
  304. struct json_obj_field* b = *iter;
  305. // struct json_obj* obj;
  306. if(obj->type != JSON_TYPE_OBJ) return 1;
  307. if(obj->len == 0) {
  308. return 0;
  309. }
  310. // a tiny bit of idiot-proofing
  311. if(b == NULL) b = &obj->obj.buckets[-1];
  312. //printf("alloc size: %d\n", obj->alloc_size);
  313. do {
  314. b++;
  315. if(b >= obj->obj.buckets + obj->obj.alloc_size) {
  316. //printf("ending next\n");
  317. // end of the list
  318. *value = NULL;
  319. *key = NULL;
  320. return 0;
  321. }
  322. } while(!b->key);
  323. *key = b->key;
  324. *value = b->value;
  325. *iter = b;
  326. return 1;
  327. }
  328. /* key, target, offset, type */
  329. // returns number of values filled
  330. int json_obj_unpack_struct(int count, struct json_value* obj, ...) {
  331. int i, ret, filled;
  332. char* key;
  333. void* target;
  334. size_t offset;
  335. enum json_type type;
  336. struct json_value* v;
  337. va_list ap;
  338. if(obj->type != JSON_TYPE_OBJ) return 0;
  339. filled = 0;
  340. va_start(ap, obj);
  341. for(i = 0; i < count; i++) {
  342. key = va_arg(ap, char*);
  343. if(!key) break;
  344. target = va_arg(ap, void*);
  345. offset = va_arg(ap, size_t);
  346. type = va_arg(ap, enum json_type);
  347. ret = json_obj_get_key(obj, key, &v);
  348. if(ret > 0) continue;
  349. if(!json_as_type(v, type, target + offset)) filled++;
  350. }
  351. va_end(ap);
  352. return filled;
  353. }
  354. /*
  355. returns an array of char* pairs, key then value
  356. int json_obj_unpack_string_array(struct json_value* obj, char*** out, size_t* len) {
  357. char** a;
  358. size_t l, i;
  359. int ret;
  360. struct json_obj* o;
  361. void* iter;
  362. char* key;
  363. struct json_value* v;
  364. // TODO: error handling
  365. if(obj->type != JSON_TYPE_OBJ) {
  366. return 1;
  367. }
  368. o = obj->v.obj;
  369. l = o->len;
  370. if(l <= 0) {
  371. return 2;
  372. }
  373. a = malloc(l * 2 * sizeof(*a));
  374. if(!a) {
  375. return 3;
  376. }
  377. // do shit
  378. i = 0;
  379. iter = NULL;
  380. while(json_obj_next(obj, &iter, &key, &v)) {
  381. a[i++] = key; // todo: strdup?
  382. ret = json_as_string(v, &a[i++]);
  383. // TODO: handle errors here
  384. }
  385. *out = a;
  386. *len = l;
  387. return 0;
  388. }
  389. */
  390. /*
  391. type coercion rules:
  392. undefined/null -> int = 0
  393. numbers, as you would expect, according to C type conversion rules
  394. obj/array -> number/string = error
  395. string/comment -> string = string
  396. number -> string = sprintf, accoding to some nice rules.
  397. string -> number = strtod/i
  398. strings are dup'd
  399. JSON_TYPE_INT is assumed to be C int
  400. numbers over 2^63 are not properly supported yet. they will be truncated to 0
  401. */
  402. // returns 0 if successful
  403. int json_as_type(struct json_value* v, enum json_type t, void* out) {
  404. int ret;
  405. int64_t i;
  406. double d;
  407. char* s;
  408. if(!v) return 1;
  409. switch(t) { // actual type
  410. case JSON_TYPE_INT:
  411. i = json_as_int(v);
  412. *((int*)out) = i;
  413. return 0;
  414. case JSON_TYPE_DOUBLE:
  415. d = json_as_double(v);
  416. *((double*)out) = d;
  417. return 0;
  418. case JSON_TYPE_STRING:
  419. s = json_as_strdup(v);
  420. *((char**)out) = s;
  421. return 0;
  422. case JSON_TYPE_OBJ:
  423. case JSON_TYPE_ARRAY:
  424. *((struct json_value**)out) = v;
  425. return 0;
  426. case JSON_TYPE_FLOAT:
  427. d = json_as_double(v);
  428. *((float*)out) = d;
  429. return 0;
  430. case JSON_TYPE_INT8:
  431. i = json_as_int(v);
  432. *((int8_t*)out) = i;
  433. return 0;
  434. case JSON_TYPE_INT16:
  435. i = json_as_int(v);
  436. *((int16_t*)out) = i;
  437. return 0;
  438. case JSON_TYPE_INT32:
  439. i = json_as_int(v);
  440. *((int32_t*)out) = i;
  441. return 0;
  442. case JSON_TYPE_INT64:
  443. i = json_as_int(v);
  444. *((int64_t*)out) = i;
  445. return 0;
  446. case JSON_TYPE_UINT8:
  447. i = json_as_int(v);
  448. *((uint8_t*)out) = i;
  449. return 0;
  450. case JSON_TYPE_UINT16:
  451. i = json_as_int(v);
  452. *((uint16_t*)out) = i;
  453. return 0;
  454. case JSON_TYPE_UINT32:
  455. i = json_as_int(v);
  456. *((uint32_t*)out) = i;
  457. return 0;
  458. case JSON_TYPE_UINT64:
  459. i = json_as_int(v);
  460. *((uint64_t*)out) = i;
  461. return 0;
  462. case JSON_TYPE_UNDEFINED:
  463. case JSON_TYPE_NULL:
  464. case JSON_TYPE_COMMENT_SINGLE:
  465. case JSON_TYPE_COMMENT_MULTI:
  466. default:
  467. return 1;
  468. }
  469. }
  470. // returns 0 for success
  471. int64_t json_as_int(struct json_value* v) {
  472. switch(v->type) { // actual type
  473. case JSON_TYPE_UNDEFINED:
  474. case JSON_TYPE_NULL:
  475. return 0;
  476. case JSON_TYPE_INT:
  477. return v->n;
  478. case JSON_TYPE_DOUBLE:
  479. return v->d;
  480. case JSON_TYPE_STRING:
  481. return strtol(v->s, NULL, 0);
  482. case JSON_TYPE_OBJ:
  483. case JSON_TYPE_ARRAY:
  484. case JSON_TYPE_COMMENT_SINGLE:
  485. case JSON_TYPE_COMMENT_MULTI:
  486. default:
  487. return 0;
  488. }
  489. }
  490. // returns 0 for success
  491. double json_as_double(struct json_value* v) {
  492. switch(v->type) { // actual type
  493. case JSON_TYPE_UNDEFINED:
  494. case JSON_TYPE_NULL:
  495. return 0.0;
  496. case JSON_TYPE_INT:
  497. return v->n;
  498. case JSON_TYPE_DOUBLE:
  499. return v->d;
  500. case JSON_TYPE_STRING:
  501. return strtod(v->s, NULL);
  502. case JSON_TYPE_OBJ:
  503. case JSON_TYPE_ARRAY:
  504. case JSON_TYPE_COMMENT_SINGLE:
  505. case JSON_TYPE_COMMENT_MULTI:
  506. default:
  507. return 0.0;
  508. }
  509. }
  510. static char* a_sprintf(char* fmt, ...) {
  511. va_list args;
  512. char* buf;
  513. size_t len;
  514. int n;
  515. va_start(args, fmt);
  516. len = vsnprintf(NULL, 0, fmt, args);
  517. buf = malloc(len + 1);
  518. if(!buf) return NULL;
  519. vsnprintf(buf, len, fmt, args);
  520. va_end (args);
  521. return buf;
  522. }
  523. // returns 0 for success
  524. char* json_as_strdup(struct json_value* v) {
  525. char* buf;
  526. size_t len;
  527. switch(v->type) { // actual type
  528. case JSON_TYPE_UNDEFINED:
  529. return strdup("undefined");
  530. case JSON_TYPE_NULL:
  531. return strdup("null");
  532. case JSON_TYPE_INT:
  533. return a_sprintf("%ld", v->n); // BUG might leak memory
  534. case JSON_TYPE_DOUBLE:
  535. return a_sprintf("%f", v->d);
  536. case JSON_TYPE_COMMENT_SINGLE:
  537. case JSON_TYPE_COMMENT_MULTI:
  538. case JSON_TYPE_STRING:
  539. return strdup(v->s);
  540. case JSON_TYPE_OBJ:
  541. return strdup("[Object]");
  542. case JSON_TYPE_ARRAY:
  543. return strdup("[Array]");
  544. default:
  545. return strdup("");
  546. }
  547. }
  548. float json_as_float(struct json_value* v) {
  549. return json_as_double(v);
  550. }
  551. // out must be big enough, at least as big as in+1 just to be safe
  552. // appends a null to out, but is also null-safe
  553. static int decode_c_escape_str(char* in, char* out, size_t len, size_t* outLen) {
  554. size_t i, o;
  555. char tmp[7];
  556. for(i = 0, o = 0; i < len; i++, o++) {
  557. if(*in == '\\') {
  558. in++;
  559. i++;
  560. switch(*in) {
  561. case '0': *out = '\0'; break;
  562. case 'r': *out = '\r'; break;
  563. case 'n': *out = '\n'; break;
  564. case 'f': *out = '\f'; break;
  565. case 'a': *out = '\a'; break;
  566. case 'b': *out = '\b'; break;
  567. case 'v': *out = '\v'; break;
  568. case 't': *out = '\t'; break;
  569. case 'x':
  570. if(len < i + 1) {
  571. // jp->error = JSON_PARSER_ERROR_UNEXPECTED_EOI;
  572. //printf("JSON: EOF in hex escape sequence\n");
  573. return 1;
  574. }
  575. if(!isxdigit(in[1])) {
  576. // malformed hex code. output an 'x' and keep going.
  577. *out = 'x';
  578. break;
  579. }
  580. tmp[0] = in[1];
  581. if(len < i + 2) {
  582. // jp->error = JSON_PARSER_ERROR_UNEXPECTED_EOI;
  583. //printf("JSON: EOF in hex escape sequence\n");
  584. return 1;
  585. }
  586. if(!isxdigit(in[2])) {
  587. // malformed hex code, but we have one digit
  588. tmp[1] = 0;
  589. *out = strtol(tmp, NULL, 16);
  590. in++; i++;
  591. break;
  592. }
  593. else {
  594. tmp[1] = in[2];
  595. tmp[2] = 0;
  596. *out = strtol(tmp, NULL, 16);
  597. in += 2; i += 2;
  598. }
  599. break;
  600. case 'u':
  601. if(in[1] == '{') {
  602. int n;
  603. int32_t code;
  604. char* s;
  605. // seek forward to the closing '}'
  606. for(n = 0, s = in + 2;; n++) {
  607. if(i + 3 >= len) {
  608. //printf("JSON: EOF in unicode escape sequence\n");
  609. return 2;
  610. }
  611. if(s[0] == '}') break;
  612. if(n == INT_MAX) {
  613. //printf("JSON: malformed unicode escape sequence\n");
  614. return 3;
  615. }
  616. if(!isxdigit(s[0])) {
  617. //printf("JSON: invalid character inside unicode escape sequence\n");
  618. break;
  619. }
  620. s++;
  621. }
  622. int nl = n > 6 ? 6 : n;
  623. if(n > 0) {
  624. strncpy(tmp, s - nl, nl);
  625. tmp[nl] = 0;
  626. code = strtol(tmp, NULL, 16);
  627. }
  628. else {
  629. code = 0;
  630. }
  631. *out = code; // todo: utf8 conversion
  632. in += n + 2; i += n + 2;
  633. }
  634. else {
  635. int n;
  636. int32_t code;
  637. char* s;
  638. // seek forward to the closing '}'
  639. for(n = 0, s = in + 1; n < 4; n++) {
  640. if(i + 2 >= len) {
  641. //printf("JSON: EOF in unicode escape sequence\n");
  642. return 2;
  643. }
  644. if(!isxdigit(s[0])) {
  645. break;
  646. }
  647. s++;
  648. }
  649. if(n > 0) {
  650. strncpy(tmp, in + 1, n);
  651. tmp[n] = 0;
  652. code = strtol(tmp, NULL, 16);
  653. }
  654. else {
  655. code = 0;
  656. }
  657. *out = code; // todo: utf8 conversion
  658. in += n; i += n;
  659. }
  660. break;
  661. default:
  662. // pass-through
  663. *out = in[0];
  664. }
  665. }
  666. else {
  667. *out = *in;
  668. }
  669. out++;
  670. in++;
  671. }
  672. *out = '\0';
  673. if(outLen) *outLen = o;
  674. return 0;
  675. }
  676. ///////////////////
  677. // Parser //
  678. ///////////////////
  679. // move forward one char
  680. static void lex_next_char(struct json_parser* jl) {
  681. if(jl->error) {
  682. //printf("JSON: next char has error\n");
  683. return;
  684. }
  685. if(*jl->head == '\n') {
  686. jl->line_num++;
  687. jl->char_num = 1;
  688. }
  689. else {
  690. jl->char_num++;
  691. }
  692. jl->head++;
  693. //printf("%c\n", *jl->head);
  694. //printf("line/char [%d/%d]\n", jl->line_num, jl->char_num);
  695. }
  696. // returns erro code.
  697. static int lex_push_token_val(struct json_parser* jp, enum token_type t, struct json_value* val) {
  698. jp->gotToken = 1;
  699. jp->cur_tok.tokenType = t;
  700. jp->cur_tok.val = val;
  701. dbg_printf("pushing token %d at %d, %d\n", t, jp->line_num, jp->char_num);
  702. return 0;
  703. }
  704. // returns error code. val set to null
  705. static int lex_push_token(struct json_parser* jl, enum token_type t) {
  706. return lex_push_token_val(jl, t, NULL);
  707. }
  708. static int lex_string_token(struct json_parser* jl) {
  709. size_t len;
  710. struct json_value* val;
  711. char* str;
  712. char delim = *jl->head;
  713. char* se = jl->head + 1;
  714. int lines = 0;
  715. int char_num = jl->char_num;
  716. // find len, count lines
  717. while(1) {
  718. if(*se == delim && *(se-1) != '\\') break;
  719. if(*se == '\0') {
  720. jl->error = JSON_LEX_ERROR_NULL_IN_STRING;
  721. return 1;
  722. }
  723. if(*se == '\n') {
  724. lines++;
  725. char_num = 1;
  726. }
  727. char_num++;
  728. se++;
  729. // printf("%d.%d\n", jl->line_num + lines, char_num);
  730. if(se > jl->end) {
  731. jl->error = JSON_LEX_ERROR_UNEXPECTED_END_OF_INPUT;
  732. return 1;
  733. }
  734. }
  735. len = se - jl->head - 1;
  736. str = malloc(len+1);
  737. if(decode_c_escape_str(jl->head + 1, str, len, NULL)) {
  738. jl->error = JSON_LEX_ERROR_INVALID_STRING;
  739. return 1;
  740. }
  741. // json value
  742. val = calloc(1, sizeof(*val));
  743. val->type = JSON_TYPE_STRING;
  744. val->s = str;
  745. lex_push_token_val(jl, TOKEN_STRING, val);
  746. // advance to the end of the string
  747. jl->head = se;
  748. jl->char_num = char_num + 1;
  749. jl->line_num += lines;
  750. return 0;
  751. }
  752. static int lex_number_token(struct json_parser* jl) {
  753. char* start, *s, *e;
  754. int is_float = 0;
  755. int negate =0;
  756. int base;
  757. start = jl->head;
  758. if(*start == '-') negate = 1;
  759. if(*start == '+' || *start == '-') start++;
  760. s = start;
  761. // check if it's a float
  762. while(s < jl->end) {
  763. if(*s == '.' || *s == 'e' || *s == 'E')
  764. is_float = 1;
  765. if(*s < '0' || *s > '9') break;
  766. s++;
  767. }
  768. s = start;
  769. struct json_value* val;
  770. val = malloc(sizeof(*val));
  771. // read the value
  772. if(is_float) {
  773. base = -1;
  774. val->d = strtod(s, &e);
  775. val->type = JSON_TYPE_DOUBLE;
  776. if(negate) val->d *= -1;
  777. }
  778. else {
  779. if(*s == '0') {
  780. if(s[1] == 'x') { // hex
  781. base = 16;
  782. s += 2;
  783. }
  784. else if(s[1] == 'b') { //binary
  785. base = 2;
  786. s += 2;
  787. }
  788. else base = 8;
  789. }
  790. else base = 10;
  791. val->n = strtol(s, &e, base);
  792. val->type = JSON_TYPE_INT;
  793. if(negate) val->n *= -1;
  794. }
  795. val->base = base;
  796. lex_push_token_val(jl, TOKEN_NUMBER, val);
  797. // advance to the end of the string
  798. jl->char_num += e - jl->head - 1;
  799. jl->head = e - 1;
  800. // printf("head %c\n", *jl->head);
  801. // printf("lc/line/char [%d/%d/%d]\n", jl->head - jl->source, jl->line_num, jl->char_num);
  802. return 0;
  803. }
  804. static int lex_label_token(struct json_parser* jl) {
  805. size_t len;
  806. struct json_value* val;
  807. char* str;
  808. char* se = jl->head;
  809. int char_num = jl->char_num;
  810. // check for boolean literals
  811. // TODO: proper ident char checking
  812. if(0 == strncasecmp(se, "true", strlen("true"))) {
  813. if(!isalnum(se[strlen("true")]) && se[strlen("true")] != '_') {
  814. lex_push_token_val(jl, TOKEN_TRUE, json_new_strn(se, strlen("true")));
  815. jl->head += strlen("true") - 1;
  816. jl->char_num += strlen("true") - 1;
  817. return 0;
  818. }
  819. }
  820. if(0 == strncasecmp(se, "false", strlen("false"))) {
  821. if(!isalnum(se[strlen("false")]) && se[strlen("false")] != '_') {
  822. lex_push_token_val(jl, TOKEN_FALSE, json_new_strn(se, strlen("false")));
  823. jl->head += strlen("false") - 1;
  824. jl->char_num += strlen("false") - 1;
  825. return 0;
  826. }
  827. }
  828. if(0 == strncasecmp(se, "null", strlen("null"))) {
  829. if(!isalnum(se[strlen("null")]) && se[strlen("null")] != '_') {
  830. lex_push_token_val(jl, TOKEN_NULL, json_new_strn(se, strlen("null")));
  831. jl->head += strlen("null") - 1;
  832. jl->char_num += strlen("null") - 1;
  833. return 0;
  834. }
  835. }
  836. if(0 == strncasecmp(se, "undefined", strlen("undefined"))) {
  837. if(!isalnum(se[strlen("undefined")]) && se[strlen("undefined")] != '_') {
  838. lex_push_token_val(jl, TOKEN_UNDEFINED, json_new_strn(se, strlen("undefined")));
  839. jl->head += strlen("undefined") - 1;
  840. jl->char_num += strlen("undefined") - 1;
  841. return 0;
  842. }
  843. }
  844. if(0 == strncasecmp(se, "infinity", strlen("infinity"))) {
  845. if(!isalnum(se[strlen("infinity")]) && se[strlen("infinity")] != '_') {
  846. lex_push_token_val(jl, TOKEN_INFINITY, json_new_strn(se, strlen("infinity")));
  847. jl->head += strlen("infinity") - 1;
  848. jl->char_num += strlen("infinity") - 1;
  849. return 0;
  850. }
  851. }
  852. //printf("error: %d\n", jl->error);
  853. // find len, count lines
  854. while(1) {
  855. if(!((*se >= 'a' && *se <= 'z')
  856. || (*se >= 'A' && *se <= 'Z')
  857. || (*se >= '0' && *se <= '9')
  858. || *se == '_' || *se == '$')
  859. ) break;
  860. char_num++;
  861. se++;
  862. //printf("%d.%d\n", jl->line_num, char_num);
  863. if(se > jl->end) {
  864. jl->error = JSON_LEX_ERROR_UNEXPECTED_END_OF_INPUT;
  865. return 1;
  866. }
  867. }
  868. len = se - jl->head;
  869. // TODO: check for null, infinity, undefined, nan
  870. str = malloc(len+1);
  871. strncpy(str, jl->head, len);
  872. str[len] = 0;
  873. // json value
  874. val = calloc(1, sizeof(*val));
  875. val->type = JSON_TYPE_STRING;
  876. val->s = str;
  877. lex_push_token_val(jl, TOKEN_LABEL, val);
  878. // advance to the end of the string
  879. jl->head = se - 1;
  880. jl->char_num = char_num - 1;
  881. //printf("head %c\n", *jl->head);
  882. //printf("lc/line/char [%d/%d/%d]\n", jl->head - jl->source, jl->line_num, jl->char_num);
  883. return 0;
  884. }
  885. static int lex_comment_token(struct json_parser* jl) {
  886. char* start, *se, *str;
  887. char delim;
  888. size_t len;
  889. int lines, char_num;
  890. struct json_value* val;
  891. lex_next_char(jl);
  892. start = se = jl->head + 1;
  893. delim = *jl->head;
  894. lines = 0;
  895. char_num = jl->char_num;
  896. if(delim == '/') { // single line comment
  897. // look for a linebreak;
  898. while(1) {
  899. if(se[0] == '\n') break;
  900. if(*se == '\0') {
  901. jl->error = JSON_LEX_ERROR_NULL_BYTE;
  902. return 1;
  903. }
  904. char_num++;
  905. se++;
  906. if(se > jl->end) {
  907. jl->error = JSON_LEX_ERROR_UNEXPECTED_END_OF_INPUT;
  908. return 1;
  909. }
  910. }
  911. }
  912. else if(delim == '*') { // multline
  913. // find len, count lines
  914. while(1) {
  915. if(se[0] == '*' && se[1] == '/') break;
  916. if(*se == '\0') {
  917. jl->error = JSON_LEX_ERROR_NULL_BYTE;
  918. return 1;
  919. }
  920. if(*se == '\n') {
  921. lines++;
  922. char_num = 1;
  923. }
  924. char_num++;
  925. se++;
  926. if(se > jl->end) {
  927. jl->error = JSON_LEX_ERROR_UNEXPECTED_END_OF_INPUT;
  928. return 1;
  929. }
  930. }
  931. }
  932. else {
  933. //printf("JSON: broken comment\n");
  934. jl->error = JSON_LEX_ERROR_INVALID_CHAR;
  935. return 1;
  936. }
  937. len = se - jl->head - 1;
  938. #if JSON_DISCARD_COMMENTS == 0
  939. str = malloc(len+1);
  940. check_oom(str)
  941. // printf("error: %d\n", jl->error);
  942. strncpy(str, start, len);
  943. //printf("error: %d\n", jl->error);
  944. // json value
  945. val = calloc(1, sizeof(*val));
  946. check_oom(val)
  947. // printf("error: %d\n", jl->error);
  948. val->type = delim == '*' ? JSON_TYPE_COMMENT_MULTI : JSON_TYPE_COMMENT_SINGLE;
  949. val->v.str = str;
  950. lex_push_token_val(jl, TOKEN_COMMENT, val);
  951. #endif // JSON_DISCARD_COMMENTS
  952. // advance to the end of the string
  953. jl->head = se;
  954. jl->char_num = char_num;
  955. jl->line_num += lines;
  956. if(delim == '*') lex_next_char(jl);
  957. // printf("--head %c\n", *jl->head);
  958. // printf("lc/line/char [%d/%d/%d]\n", jl->head - jl->source, jl->line_num, jl->char_num);
  959. return 0;
  960. }
  961. // returns false when there is no more input
  962. // formerly lex_nibble()
  963. static int lex_next_token(struct json_parser* jl) {
  964. jl->gotToken = 0;
  965. while(!jl->gotToken) {
  966. char c = *jl->head;
  967. switch(c) {
  968. case '{': lex_push_token(jl, TOKEN_OBJ_START); break;
  969. case '}': lex_push_token(jl, TOKEN_OBJ_END); break;
  970. case '[': lex_push_token(jl, TOKEN_ARRAY_START); break;
  971. case ']': lex_push_token(jl, TOKEN_ARRAY_END); break;
  972. case ',': lex_push_token(jl, TOKEN_COMMA); break;
  973. case ':': lex_push_token(jl, TOKEN_COLON); break;
  974. case '/': lex_comment_token(jl); break;
  975. case '\'':
  976. case '"':
  977. case '`':
  978. lex_string_token(jl);
  979. break;
  980. case '0': case '1': case '2': case '3': case '4':
  981. case '5': case '6': case '7': case '8': case '9':
  982. case '-': case '+': case '.':
  983. lex_number_token(jl);
  984. break;
  985. case ' ':
  986. case '\t':
  987. case '\r':
  988. case '\f':
  989. case '\v':
  990. case '\n':
  991. break;
  992. default:
  993. if(isalpha(c) || c == '_' || c == '$') {
  994. lex_label_token(jl);
  995. break;
  996. }
  997. if(c == 0) {
  998. lex_push_token(jl, TOKEN_NONE);
  999. return 1; // end of file
  1000. }
  1001. // lex error
  1002. // printf("Lexed invalid char: '%c'\n", c);
  1003. lex_push_token(jl, TOKEN_NONE);
  1004. jl->error = JSON_LEX_ERROR_INVALID_CHAR;
  1005. return 1;
  1006. }
  1007. //printf("lol\n");
  1008. lex_next_char(jl);
  1009. }
  1010. //printf("token %d:%d ", jl->line_num, jl->char_num);
  1011. dbg_print_token(&jl->cur_tok);
  1012. jl->eoi = jl->head >= jl->end;
  1013. if(jl->error) return jl->error;
  1014. return jl->eoi;
  1015. }
  1016. static int parser_indent_level = 0;
  1017. static void dbg_parser_indent(void) { return;
  1018. int i;
  1019. for(i = 0; i < parser_indent_level; i++) {
  1020. dbg_printf(" ");
  1021. }
  1022. }
  1023. static void parser_push(struct json_parser* jp, struct json_value* v) {
  1024. void* tmp;
  1025. int alloc = jp->stack_alloc;
  1026. int cnt = jp->stack_cnt;
  1027. // check size
  1028. if(cnt >= alloc) {
  1029. if(alloc == 0) alloc = 16;
  1030. alloc *= 2;
  1031. tmp = realloc(jp->stack, alloc * sizeof(*(jp->stack)));
  1032. if(!tmp) {
  1033. jp->error = JSON_ERROR_OOM;
  1034. return;
  1035. }
  1036. jp->stack = tmp;
  1037. jp->stack_alloc = alloc;
  1038. }
  1039. jp->stack[cnt] = v;
  1040. jp->stack_cnt++;
  1041. }
  1042. static struct json_value* parser_pop(struct json_parser* jp) {
  1043. if(jp->stack_cnt <= 0) {
  1044. jp->error = JSON_PARSER_ERROR_STACK_EXHAUSTED;
  1045. return NULL;
  1046. }
  1047. return jp->stack[--jp->stack_cnt];
  1048. }
  1049. static void parser_push_new_array(struct json_parser* jp) {
  1050. struct json_value* val;
  1051. val = malloc(sizeof(*val));
  1052. if(!val) {
  1053. jp->error = JSON_ERROR_OOM;
  1054. return;
  1055. }
  1056. val->type = JSON_TYPE_ARRAY;
  1057. val->len = 0;
  1058. val->base = 0;
  1059. val->arr.head = NULL;
  1060. val->arr.tail = NULL;
  1061. parser_push(jp, val);
  1062. }
  1063. static void parser_push_new_object(struct json_parser* jp) {
  1064. struct json_value* val;
  1065. val = json_new_object(8);
  1066. if(!val) {
  1067. jp->error = JSON_ERROR_OOM;
  1068. return;
  1069. }
  1070. // printf("vt: %d\n", val);
  1071. dbg_dump_stack(jp, 3);
  1072. parser_push(jp, val);
  1073. dbg_dump_stack(jp, 4);
  1074. }
  1075. // not used atm. comments will probably be moved to a side channel
  1076. static void consume_comments(struct json_parser* jp) {
  1077. while(1) {
  1078. if(jp->cur_tok.tokenType != TOKEN_COMMENT) break;
  1079. parser_push(jp, jp->cur_tok.val);
  1080. lex_next_token(jp);
  1081. }
  1082. }
  1083. static void consume_commas(struct json_parser* jp) {
  1084. // lex_next_token(jp);
  1085. while(jp->cur_tok.tokenType == TOKEN_COMMA) lex_next_token(jp);
  1086. }
  1087. static void reduce_array(struct json_parser* jp) {
  1088. /* what the stack should look like now
  1089. ...
  1090. 1 array
  1091. 0 any value
  1092. */
  1093. if(jp->stack_cnt < 2) {
  1094. dbg_printf("stack too short in reduce_array: %d \n", jp->stack_cnt);
  1095. jp->error = JSON_PARSER_ERROR_STACK_EXHAUSTED;
  1096. return;
  1097. }
  1098. struct json_value** st = jp->stack + jp->stack_cnt - 1;
  1099. struct json_value* v = st[0];
  1100. struct json_value* arr = st[-1];
  1101. if(arr == ROOT_VALUE) return;
  1102. if(arr->type != JSON_TYPE_ARRAY) {
  1103. jp->error = JSON_PARSER_ERROR_CORRUPT_STACK;
  1104. return;
  1105. }
  1106. // append v to arr
  1107. json_array_push_tail(arr, v);
  1108. jp->stack_cnt--;
  1109. }
  1110. static void reduce_object(struct json_parser* jp) {
  1111. /* what the stack should look like now
  1112. ...
  1113. 2 object
  1114. 1 label
  1115. 0 any value
  1116. */
  1117. if(jp->stack_cnt < 3) {
  1118. jp->error = JSON_PARSER_ERROR_STACK_EXHAUSTED;
  1119. return;
  1120. }
  1121. struct json_value** st = jp->stack + jp->stack_cnt - 1;
  1122. struct json_value* v = st[0];
  1123. struct json_value* l = st[-1];
  1124. struct json_value* obj = st[-2];
  1125. if(obj == ROOT_VALUE) return;
  1126. // TODO: check label type
  1127. dbg_dump_stack(jp, 10);
  1128. if(obj->type != JSON_TYPE_OBJ) {// printf("invalid obj\n");
  1129. dbg_printf("0 type: %d \n", v->type);
  1130. dbg_printf("1 type: %d \n", l->type);
  1131. dbg_printf("2 type: %d \n", obj->type);
  1132. dbg_printf("3 type: %d \n", st[-3]->type);
  1133. jp->error = JSON_PARSER_ERROR_CORRUPT_STACK;
  1134. return;
  1135. }
  1136. // insert l:v into obj
  1137. json_obj_set_key_nodup(obj, l->s, v);
  1138. free(l);
  1139. jp->stack_cnt -= 2;
  1140. }
  1141. static struct json_parser* parse_token_stream(char* source, size_t len) {
  1142. int i;
  1143. struct json_parser* jp;
  1144. jp = calloc(1, sizeof(*jp));
  1145. if(!jp) {
  1146. return NULL;
  1147. }
  1148. jp->source = source;
  1149. jp->end = source + len;
  1150. jp->source_len = len;
  1151. jp->head = source;
  1152. jp->line_num = 1; // these are 1-based
  1153. jp->char_num = 1;
  1154. i = 0;
  1155. #define next() lex_next_token(jp); //) goto UNEXPECTED_EOI;
  1156. #define is_end() if(jp->eoi) goto UNEXPECTED_EOI;
  1157. // the root value sentinel helps a few algorithms and marks a proper end of input
  1158. parser_push(jp, ROOT_VALUE);
  1159. // get the first token
  1160. lex_next_token(jp);
  1161. if(jp->cur_tok.tokenType == TOKEN_OBJ_START) {
  1162. parser_push_new_object(jp);
  1163. next();
  1164. goto PARSE_OBJ;
  1165. } else if(jp->cur_tok.tokenType == TOKEN_ARRAY_START) {
  1166. parser_push_new_array(jp);
  1167. next();
  1168. goto PARSE_ARRAY;
  1169. }
  1170. PARSE_ARRAY: // not actually starting with an array; this is just the type probing code
  1171. dbg_parser_indent();
  1172. dbg_printf("\nparse_array l:%d, c:%d \n", jp->line_num, jp->char_num);
  1173. // if(jp->eoi) goto CHECK_END;
  1174. if(jp->cur_tok.tokenType == TOKEN_ARRAY_END) {
  1175. dbg_parser_indent();dbg_printf("array- TOKEN_ARR_END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1176. parser_indent_level--;
  1177. /* what the stack should look like now
  1178. ...
  1179. 3 parent container
  1180. 2 ? possibly a label ?
  1181. 1 -- resume sentinel --
  1182. 0 array to be closed
  1183. */
  1184. {
  1185. // TODO check stack depth
  1186. struct json_value* closed = parser_pop(jp);
  1187. struct json_value* sentinel = parser_pop(jp);
  1188. // put the closed array back on the stack then reduce it appropriately
  1189. parser_push(jp, closed);
  1190. if(sentinel == RESUME_ARRAY) {
  1191. reduce_array(jp);
  1192. next();
  1193. // consume_commas(jp);
  1194. goto PARSE_ARRAY;
  1195. }
  1196. else if(sentinel == RESUME_OBJ) {
  1197. reduce_object(jp);
  1198. next();
  1199. // consume_commas(jp);
  1200. goto PARSE_OBJ;
  1201. }
  1202. else if(sentinel == ROOT_VALUE) {
  1203. // proper finish
  1204. goto END;
  1205. }
  1206. else {
  1207. goto INVALID_SENTINEL;
  1208. }
  1209. }
  1210. dbg_printf("ending array in begining of array\n");
  1211. goto UNEXPECTED_TOKEN;
  1212. }
  1213. // cycle: val, comma
  1214. switch(jp->cur_tok.tokenType) {
  1215. case TOKEN_ARRAY_START: dbg_parser_indent();dbg_printf("TOKEN_ARRAY_START l:%d, c:%d \n", jp->line_num, jp->char_num);
  1216. parser_indent_level++;
  1217. parser_push(jp, RESUME_ARRAY);
  1218. parser_push_new_array(jp);
  1219. next();
  1220. goto PARSE_ARRAY;
  1221. case TOKEN_ARRAY_END: dbg_parser_indent();dbg_printf("TOKEN_ARRAY_END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1222. parser_indent_level--;
  1223. /* what the stack should look like now
  1224. ...
  1225. 3 parent container
  1226. 2 ? possibly a label ?
  1227. 1 -- resume sentinel --
  1228. 0 array to be closed
  1229. */
  1230. {
  1231. // TODO check stack depth
  1232. struct json_value* closed = parser_pop(jp);
  1233. struct json_value* sentinel = parser_pop(jp);
  1234. // put the closed array back on the stack then reduce it appropriately
  1235. parser_push(jp, closed);
  1236. if(sentinel == RESUME_ARRAY) {
  1237. reduce_array(jp);
  1238. // consume_commas(jp);
  1239. next();
  1240. goto PARSE_ARRAY;
  1241. }
  1242. else if(sentinel == RESUME_OBJ) {
  1243. reduce_object(jp);
  1244. // consume_commas(jp);
  1245. next();
  1246. goto PARSE_OBJ;
  1247. }
  1248. else if(sentinel == ROOT_VALUE) {
  1249. // proper finish
  1250. goto END;
  1251. }
  1252. else {
  1253. goto INVALID_SENTINEL;
  1254. }
  1255. }
  1256. // consume_commas(jp);
  1257. // next();
  1258. break;
  1259. case TOKEN_OBJ_START: dbg_parser_indent();dbg_printf("PARSE_OBJ l:%d, c:%d \n", jp->line_num, jp->char_num);
  1260. parser_indent_level++;
  1261. parser_push(jp, RESUME_ARRAY);
  1262. parser_push_new_object(jp);
  1263. next();
  1264. goto PARSE_OBJ;
  1265. case TOKEN_STRING:
  1266. case TOKEN_NUMBER:
  1267. case TOKEN_NULL:
  1268. case TOKEN_UNDEFINED: dbg_parser_indent();dbg_printf("PARSE VALUE l:%d, c:%d \n", jp->line_num, jp->char_num);
  1269. parser_push(jp, jp->cur_tok.val);
  1270. reduce_array(jp);
  1271. // consume_commas(jp);
  1272. next();
  1273. goto PARSE_ARRAY;
  1274. case TOKEN_OBJ_END: dbg_parser_indent();dbg_printf("BRACKET_MISMATCH l:%d, c:%d \n", jp->line_num, jp->char_num);
  1275. goto BRACKET_MISMATCH;
  1276. case TOKEN_COMMA:
  1277. next();
  1278. goto PARSE_ARRAY;
  1279. case TOKEN_NONE:
  1280. case TOKEN_LABEL:
  1281. case TOKEN_COLON:
  1282. default: dbg_printf("UNEXPECTED_TOKEN l:%d, c:%d \n", jp->line_num, jp->char_num);
  1283. // invalid
  1284. goto UNEXPECTED_TOKEN;
  1285. }
  1286. return NULL;
  1287. PARSE_OBJ:
  1288. dbg_parser_indent();dbg_printf("\nparse_obj l:%d, c:%d \n", jp->line_num, jp->char_num);
  1289. // cycle: label, colon, val, comma
  1290. consume_commas(jp);
  1291. if(jp->cur_tok.tokenType == TOKEN_OBJ_END) {
  1292. dbg_parser_indent();dbg_printf("obj- TOKEN_OBJ_END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1293. parser_indent_level--;
  1294. /* what the stack should look like now
  1295. ...
  1296. 3 parent container
  1297. 2 ? possibly a label ?
  1298. 1 -- resume sentinel --
  1299. 0 object to be closed
  1300. */
  1301. {
  1302. // TODO check stack depth
  1303. struct json_value* closed = parser_pop(jp);
  1304. struct json_value* sentinel = parser_pop(jp);
  1305. // put the closed array back on the stack then reduce it appropriately
  1306. parser_push(jp, closed);
  1307. if(sentinel == RESUME_ARRAY) {
  1308. reduce_array(jp);
  1309. next();
  1310. // consume_commas(jp);
  1311. goto PARSE_ARRAY;
  1312. }
  1313. else if(sentinel == RESUME_OBJ) {
  1314. reduce_object(jp);
  1315. next();
  1316. // consume_commas(jp);
  1317. goto PARSE_OBJ;
  1318. }
  1319. else if(sentinel == ROOT_VALUE) {
  1320. // proper finish
  1321. goto END;
  1322. }
  1323. else {
  1324. goto INVALID_SENTINEL;
  1325. }
  1326. }
  1327. dbg_printf("ending obj in begining of obj\n");
  1328. goto UNEXPECTED_TOKEN;
  1329. }
  1330. switch(jp->cur_tok.tokenType) {
  1331. case TOKEN_LABEL:
  1332. case TOKEN_STRING:
  1333. case TOKEN_TRUE:
  1334. case TOKEN_FALSE:
  1335. case TOKEN_INFINITY:
  1336. case TOKEN_NULL:
  1337. case TOKEN_UNDEFINED:
  1338. case TOKEN_NAN:
  1339. parser_push(jp, jp->cur_tok.val);
  1340. break;
  1341. default:
  1342. // error
  1343. dbg_printf("!!!missing label\n");
  1344. goto UNEXPECTED_TOKEN;
  1345. }
  1346. dbg_dump_stack(jp, 5);
  1347. next();
  1348. if(jp->cur_tok.tokenType != TOKEN_COLON) {
  1349. // error
  1350. dbg_printf("!!!missing colon\n");
  1351. goto UNEXPECTED_TOKEN;
  1352. }
  1353. next();
  1354. switch(jp->cur_tok.tokenType) {
  1355. case TOKEN_ARRAY_START: dbg_parser_indent();dbg_printf("obj- TOKEN_ARRAY_START l:%d, c:%d \n", jp->line_num, jp->char_num);
  1356. parser_indent_level++;
  1357. parser_push(jp, RESUME_OBJ);
  1358. parser_push_new_array(jp);
  1359. next();
  1360. goto PARSE_ARRAY;
  1361. case TOKEN_OBJ_END: dbg_parser_indent();dbg_printf("obj- !!!TOKEN_OBJ_END in value slot l:%d, c:%d \n", jp->line_num, jp->char_num);
  1362. dbg_printf("!!! escaped sentinel block\n");
  1363. break;
  1364. case TOKEN_OBJ_START: dbg_parser_indent();dbg_printf("obj- TOKEN_OBJ_START l:%d, c:%d \n", jp->line_num, jp->char_num);
  1365. parser_indent_level++;
  1366. parser_push(jp, RESUME_OBJ);
  1367. parser_push_new_object(jp); // BUG
  1368. next();
  1369. goto PARSE_OBJ;
  1370. case TOKEN_STRING:
  1371. case TOKEN_NUMBER:
  1372. case TOKEN_TRUE:
  1373. case TOKEN_FALSE:
  1374. case TOKEN_INFINITY:
  1375. case TOKEN_NULL:
  1376. case TOKEN_UNDEFINED: dbg_parser_indent();dbg_printf("obj- TOKEN VALUE l:%d, c:%d \n", jp->line_num, jp->char_num);
  1377. parser_push(jp, jp->cur_tok.val);
  1378. reduce_object(jp);
  1379. next();
  1380. consume_commas(jp);
  1381. goto PARSE_OBJ;
  1382. case TOKEN_ARRAY_END: dbg_parser_indent();dbg_printf("obj- BRACE_MISMATCH l:%d, c:%d \n", jp->line_num, jp->char_num);
  1383. goto BRACE_MISMATCH;
  1384. case TOKEN_COMMA: dbg_parser_indent();dbg_printf("obj- eating comma l:%d, c:%d \n", jp->line_num, jp->char_num);
  1385. next();
  1386. goto PARSE_OBJ;
  1387. case TOKEN_NONE:
  1388. case TOKEN_LABEL:
  1389. case TOKEN_COLON:
  1390. default: dbg_printf("obj- UNEXPECTED_TOKEN l:%d, c:%d \n", jp->line_num, jp->char_num);
  1391. // invalid
  1392. goto UNEXPECTED_TOKEN;
  1393. }
  1394. dbg_parser_indent();dbg_printf("end of obj\n");
  1395. return NULL;
  1396. CHECK_END: dbg_parser_indent();dbg_printf("!!! CHECK_END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1397. if(jp->stack_cnt == 2) {
  1398. // check root value sentinel
  1399. // good, fall through to END
  1400. }
  1401. else if(jp->stack_cnt == 1) {
  1402. // the file is empty
  1403. goto UNEXPECTED_EOI;
  1404. }
  1405. else {
  1406. // stuff is left on the stack
  1407. goto UNEXPECTED_EOI;
  1408. }
  1409. //i = *((int*)0);
  1410. END: dbg_printf("!!! END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1411. if(jp->error) dbg_printf("parsing error: %d\n", jp->error);
  1412. return jp;
  1413. UNEXPECTED_EOI: // end of input
  1414. dbg_printf("!!! UNEXPECTED_EOI l:%d, c:%d \n", jp->line_num, jp->char_num);
  1415. jp->error = JSON_PARSER_ERROR_UNEXPECTED_EOI;
  1416. return jp;
  1417. UNEXPECTED_TOKEN: dbg_printf("!!! UNEXPECTED_TOKEN l:%d, c:%d \n", jp->line_num, jp->char_num);
  1418. jp->error = JSON_PARSER_ERROR_UNEXPECTED_TOKEN;
  1419. return jp;
  1420. BRACE_MISMATCH: dbg_printf("!!! BRACE_MISMATCH l:%d, c:%d \n", jp->line_num, jp->char_num);
  1421. jp->error = JSON_PARSER_ERROR_BRACE_MISMATCH;
  1422. return jp;
  1423. BRACKET_MISMATCH: dbg_printf("!!! BRACKET_MISMATCH l:%d, c:%d \n", jp->line_num, jp->char_num);
  1424. jp->error = JSON_PARSER_ERROR_BRACKET_MISMATCH;
  1425. return jp;
  1426. INVALID_SENTINEL: dbg_printf("!!! INVALID_SENTINEL l:%d, c:%d \n", jp->line_num, jp->char_num);
  1427. jp->error = JSON_PARSER_ERROR_CORRUPT_STACK;
  1428. return jp;
  1429. }
  1430. #ifndef JSON_NO_STDIO
  1431. struct json_file* json_load_path(char* path) {
  1432. struct json_file* jf;
  1433. FILE* f;
  1434. f = fopen(path, "rb");
  1435. if(!f) {
  1436. //fprintf(stderr, "JSON: no such file: \"%s\"\n", path);
  1437. return NULL;
  1438. }
  1439. jf = json_read_file(f);
  1440. fclose(f);
  1441. return jf;
  1442. }
  1443. struct json_file* json_read_file(FILE* f) {
  1444. size_t fsz;
  1445. char* contents;
  1446. struct json_file* jf;
  1447. size_t nr;
  1448. // check file size
  1449. fseek(f, 0, SEEK_END);
  1450. fsz = ftell(f);
  1451. fseek(f, 0, SEEK_SET);
  1452. contents = malloc(fsz+1);
  1453. contents[fsz] = 0; // some crt functions might read past the end otherwise
  1454. nr = fread(contents, 1, fsz, f);
  1455. jf = json_parse_string(contents, fsz);
  1456. free(contents);
  1457. return jf;
  1458. }
  1459. #endif
  1460. struct json_file* json_parse_string(char* source, size_t len) {
  1461. struct json_lexer* jl;
  1462. struct json_parser* jp;
  1463. struct json_file* jf;
  1464. jp = parse_token_stream(source, len);
  1465. if(!jp) {
  1466. //printf("JSON: failed to parse token stream \n");
  1467. return NULL;
  1468. }
  1469. jf = calloc(1, sizeof(*jf));
  1470. if(!jf) return NULL;
  1471. if(jp->stack_cnt == 1) {
  1472. jf->root = jp->stack[1];
  1473. //printf("JSON: failed to parse token stream (2)\n");
  1474. }
  1475. // else some sort of error. probably EOI
  1476. jf->error = jp->error;
  1477. if(jf->error) {
  1478. jf->error_line_num = jp->line_num;
  1479. jf->error_char_num = jp->char_num;
  1480. jf->error_str = json_get_err_str(jf->error);
  1481. }
  1482. json_parser_free(jp);
  1483. free(jp);
  1484. //json_dump_value(*jp->stack, 0, 10);
  1485. //json_dump_value(jf->root, 0, 10);
  1486. return jf;
  1487. }
  1488. static void free_array(struct json_value* arr) {
  1489. struct json_link* n, *p;
  1490. n = arr->arr.head;
  1491. while(n) {
  1492. json_free(n->v);
  1493. p = n;
  1494. n = n->next;
  1495. free(p);
  1496. }
  1497. }
  1498. static void free_obj(struct json_value* o) {
  1499. size_t freed = 0;
  1500. size_t i;
  1501. for(i = 0; i < o->obj.alloc_size && freed < o->len; i++) {
  1502. struct json_obj_field* b;
  1503. b = &o->obj.buckets[i];
  1504. if(b->key == NULL) continue;
  1505. free(b->key);
  1506. json_free(b->value);
  1507. freed++;
  1508. }
  1509. free(o->obj.buckets);
  1510. }
  1511. // must manage v's memory manually
  1512. void json_free(struct json_value* v) {
  1513. if(!v) return;
  1514. switch(v->type) {
  1515. case JSON_TYPE_STRING:
  1516. case JSON_TYPE_COMMENT_SINGLE:
  1517. case JSON_TYPE_COMMENT_MULTI:
  1518. free(v->s);
  1519. break;
  1520. case JSON_TYPE_OBJ:
  1521. free_obj(v);
  1522. break;
  1523. case JSON_TYPE_ARRAY:
  1524. free_array(v);
  1525. break;
  1526. }
  1527. free(v);
  1528. }
  1529. void json_file_free(struct json_file* jsf){
  1530. json_free(jsf->root);
  1531. if(jsf->lex_info) {
  1532. free(jsf->lex_info);
  1533. }
  1534. free(jsf);
  1535. }
  1536. #define tc(x, y, z) case x: dbg_printf(#x " foo: " y "\n", z); break;
  1537. #define tcl(x) case x: dbg_printf(#x "\n"); break;
  1538. static void dbg_print_token(struct token* ts) {
  1539. switch(ts->tokenType) {
  1540. tcl(TOKEN_NONE)
  1541. tcl(TOKEN_ARRAY_START)
  1542. tcl(TOKEN_ARRAY_END)
  1543. tcl(TOKEN_OBJ_START)
  1544. tcl(TOKEN_OBJ_END)
  1545. tc(TOKEN_STRING, "%s", ts->val->s)
  1546. tc(TOKEN_NUMBER, "%d", (int)ts->val->n)
  1547. tcl(TOKEN_NULL)
  1548. tcl(TOKEN_INFINITY)
  1549. tcl(TOKEN_UNDEFINED)
  1550. tcl(TOKEN_NAN)
  1551. tc(TOKEN_LABEL, "%s", ts->val->s)
  1552. tcl(TOKEN_COMMA)
  1553. tcl(TOKEN_COLON)
  1554. tc(TOKEN_COMMENT, "%s", ts->val->s)
  1555. }
  1556. }
  1557. static void dbg_print_value(struct json_value* v) {
  1558. if(v == ROOT_VALUE) {
  1559. dbg_printf("ROOT_VALUE sentinel\n");
  1560. return;
  1561. }
  1562. if(v == RESUME_ARRAY) {
  1563. dbg_printf("RESUME_ARRAY sentinel\n");
  1564. return;
  1565. }
  1566. if(v == RESUME_OBJ) {
  1567. dbg_printf("RESUME_OBJ sentinel\n");
  1568. return;
  1569. }
  1570. switch(v->type) {
  1571. case JSON_TYPE_UNDEFINED: dbg_printf("undefined\n"); break;
  1572. case JSON_TYPE_NULL: dbg_printf("null\n"); break;
  1573. case JSON_TYPE_INT: dbg_printf("int: %d\n", (int)v->n); break;
  1574. case JSON_TYPE_DOUBLE: dbg_printf("double %f\n", v->d); break;
  1575. case JSON_TYPE_STRING: dbg_printf("string: \"%s\"\n", v->s); break;
  1576. case JSON_TYPE_OBJ: dbg_printf("object [%d]\n", (int)v->len); break;
  1577. case JSON_TYPE_ARRAY: dbg_printf("array [%d]\n", (int)v->len); break;
  1578. case JSON_TYPE_COMMENT_SINGLE: dbg_printf("comment, single\n"); break;
  1579. case JSON_TYPE_COMMENT_MULTI: dbg_printf("comment, multiline\n"); break;
  1580. default:
  1581. dbg_printf("unknown enum: %d\n", v->type);
  1582. }
  1583. }
  1584. static void dbg_dump_stack(struct json_parser* jp, int depth) {
  1585. int i = 0;
  1586. return;
  1587. for(i = 0; i < depth && i < jp->stack_cnt; i++) {
  1588. dbg_parser_indent();
  1589. dbg_printf("%d: ", i);
  1590. dbg_print_value(jp->stack[jp->stack_cnt - i - 1]);
  1591. }
  1592. }
  1593. char* json_get_type_str(enum json_type t) {
  1594. switch(t) {
  1595. case JSON_TYPE_UNDEFINED: return "undefined";
  1596. case JSON_TYPE_NULL: return "null";
  1597. case JSON_TYPE_INT: return "integer";
  1598. case JSON_TYPE_DOUBLE: return "double";
  1599. case JSON_TYPE_STRING: return "string";
  1600. case JSON_TYPE_OBJ: return "object";
  1601. case JSON_TYPE_ARRAY: return "array";
  1602. case JSON_TYPE_COMMENT_SINGLE: return "single-line comment";
  1603. case JSON_TYPE_COMMENT_MULTI: return "multi-line comment";
  1604. default: return "Invalid JSON type";
  1605. }
  1606. }
  1607. char* json_get_err_str(enum json_error e) {
  1608. switch(e) {
  1609. case JSON_ERROR_NONE: return "No error";
  1610. case JSON_ERROR_OOM: return "Out of memory";
  1611. case JSON_LEX_ERROR_NULL_IN_STRING: return "Null byte found in string";
  1612. case JSON_LEX_ERROR_NULL_BYTE: return "Null byte found in file";
  1613. case JSON_LEX_ERROR_INVALID_STRING: return "Invalid string";
  1614. case JSON_LEX_ERROR_UNEXPECTED_END_OF_INPUT: return "Unexpected end of input in lexer";
  1615. case JSON_LEX_ERROR_INVALID_CHAR: return "Invalid character code";
  1616. case JSON_PARSER_ERROR_CORRUPT_STACK: return "Parser stack corrupted";
  1617. case JSON_PARSER_ERROR_STACK_EXHAUSTED: return "Parser stack prematurely exhausted";
  1618. case JSON_PARSER_ERROR_UNEXPECTED_EOI: return "Unexpected end of input";
  1619. case JSON_PARSER_ERROR_UNEXPECTED_TOKEN: return "Unexpected token";
  1620. case JSON_PARSER_ERROR_BRACE_MISMATCH: return "Brace mismatch";
  1621. case JSON_PARSER_ERROR_BRACKET_MISMATCH: return "Bracket mismatch";
  1622. default: return "Invalid Error Code";
  1623. }
  1624. }
  1625. struct json_value* json_deep_copy(struct json_value* v) {
  1626. struct json_value* c;
  1627. c = malloc(sizeof(*c));
  1628. c->type = v->type;
  1629. switch(v->type) {
  1630. default:
  1631. case JSON_TYPE_INT:
  1632. case JSON_TYPE_DOUBLE:
  1633. c->n = v->n;
  1634. c->base = v->base;
  1635. break;
  1636. case JSON_TYPE_ARRAY:
  1637. c->len = v->len;
  1638. if(v->len == 0) {
  1639. c->arr.head = NULL;
  1640. c->arr.tail = NULL;
  1641. }
  1642. else {
  1643. struct json_link* cl = NULL;
  1644. struct json_link* cl_last, *vl;
  1645. cl_last = NULL;
  1646. vl = v->arr.head;
  1647. while(vl) {
  1648. cl = malloc(sizeof(*cl));
  1649. cl->prev = cl_last;
  1650. if(cl_last) {
  1651. cl_last->next = cl;
  1652. }
  1653. else {
  1654. c->arr.head = cl;
  1655. }
  1656. cl->v = json_deep_copy(vl->v);
  1657. cl_last = cl;
  1658. vl = vl->next;
  1659. }
  1660. cl->next = NULL;
  1661. c->arr.tail = cl;
  1662. }
  1663. break;
  1664. case JSON_TYPE_OBJ:
  1665. c->obj.alloc_size = v->obj.alloc_size;
  1666. c->len = v->len;
  1667. c->obj.buckets = calloc(1, sizeof(c->obj.buckets) * c->obj.alloc_size);
  1668. for(size_t i = 0, j = 0; j < v->len && i < v->obj.alloc_size; i++) {
  1669. if(v->obj.buckets[i].key) {
  1670. c->obj.buckets[i].key = strdup(v->obj.buckets[i].key);
  1671. c->obj.buckets[i].hash = v->obj.buckets[i].hash;
  1672. c->obj.buckets[i].value = json_deep_copy(v->obj.buckets[i].value);
  1673. j++;
  1674. }
  1675. }
  1676. break;
  1677. }
  1678. return c;
  1679. }
  1680. // scalar and disparate types take the value of from
  1681. // appends arrays
  1682. // recursively merges objects
  1683. void json_merge(struct json_value* into, struct json_value* from) {
  1684. // append two arrays
  1685. if(into->type == JSON_TYPE_ARRAY && from->type == JSON_TYPE_ARRAY) {
  1686. struct json_link* fl;
  1687. fl = from->arr.head;
  1688. while(fl) {
  1689. json_array_push_tail(into, json_deep_copy(fl->v));
  1690. }
  1691. return;
  1692. }
  1693. // disparate or scalar types
  1694. if(into->type != JSON_TYPE_OBJ || from->type != JSON_TYPE_OBJ) {
  1695. // clean out into first
  1696. if(into->type == JSON_TYPE_ARRAY) {
  1697. free_array(into);
  1698. }
  1699. else if(into->type == JSON_TYPE_OBJ) {
  1700. free_obj(into);
  1701. }
  1702. // deep-copy an array or obect from value
  1703. if(from->type == JSON_TYPE_ARRAY || from->type == JSON_TYPE_OBJ) {
  1704. struct json_value* tmp;
  1705. tmp = json_deep_copy(from);
  1706. memcpy(into, tmp, sizeof(*into));
  1707. free(tmp);
  1708. return;
  1709. }
  1710. // simple copy of scalars
  1711. memcpy(into, from, sizeof(*into));
  1712. return;
  1713. }
  1714. // merge objects
  1715. void* fi = NULL;
  1716. char* key;
  1717. struct json_value* fv;
  1718. while(json_obj_next(from, &fi, &key, &fv)) {
  1719. struct json_value* iv;
  1720. if(json_obj_get_key(into, key, &iv)) {
  1721. json_obj_set_key(into, key, json_deep_copy(fv));
  1722. }
  1723. else { // key exists in into
  1724. json_merge(iv, fv);
  1725. }
  1726. }
  1727. }
  1728. static void spaces(int depth, int w) {
  1729. int i;
  1730. for(i = 0; i < depth * w; i++) dbg_printf(" ");
  1731. }
  1732. // shitty recursive fn for debugging
  1733. void json_dump_value(struct json_value* root, int cur_depth, int max_depth) {
  1734. void* iter;
  1735. char* key;
  1736. struct json_value* v;
  1737. //printf("-----------------------------------\n\n\n");
  1738. if(root->type == JSON_TYPE_ARRAY) {
  1739. spaces(cur_depth, 4);
  1740. dbg_printf("[\n");
  1741. // do shit
  1742. spaces(cur_depth, 4);
  1743. dbg_printf("]\n");
  1744. }
  1745. else if(root->type == JSON_TYPE_OBJ) {
  1746. dbg_printf("{\n");
  1747. // do shit
  1748. iter = NULL;
  1749. while(json_obj_next(root, &iter, &key, &v)) {
  1750. //printf("looping\n;");
  1751. spaces(cur_depth, 4);
  1752. dbg_printf("%s: ", key);
  1753. json_dump_value(v, cur_depth+1, max_depth);
  1754. }
  1755. spaces(cur_depth, 4);
  1756. dbg_printf("}\n");
  1757. }
  1758. else {
  1759. //printf("here");
  1760. dbg_print_value(root);
  1761. dbg_printf("\n");
  1762. }
  1763. }
  1764. // JSON output
  1765. static void sb_cat_escaped(struct json_write_context* ctx, char* str);
  1766. static void json_obj_to_string(struct json_write_context* sb, struct json_value* obj);
  1767. static void json_arr_to_string(struct json_write_context* sb, struct json_value* arr);
  1768. struct json_string_buffer* json_string_buffer_create(size_t initSize) {
  1769. struct json_string_buffer* b;
  1770. b = malloc(sizeof(*b));
  1771. b->length = 0;
  1772. b->alloc = initSize;
  1773. b->buf = malloc(initSize * sizeof(*b->buf));
  1774. b->buf[0] = 0;
  1775. return b;
  1776. }
  1777. void json_string_buffer_free(struct json_string_buffer* sb) {
  1778. free(sb->buf);
  1779. sb->buf = NULL;
  1780. sb->length = 0;
  1781. sb->alloc = 0;
  1782. }
  1783. static void sb_check(struct json_string_buffer* sb, size_t more) {
  1784. char* tmp;
  1785. if(sb->length + 1 + more > sb->alloc) {
  1786. tmp = realloc(sb->buf, sb->alloc * 2); // BUG: guarantee sufficient size
  1787. if(tmp) {
  1788. sb->buf = tmp;
  1789. sb->alloc *= 2;
  1790. }
  1791. else {
  1792. //fprintf(stderr, "c_json: Memory allocation failed\n");
  1793. }
  1794. }
  1795. }
  1796. // checks size and concatenates string
  1797. // does not check for newlines re: line_len
  1798. static void sb_cat(struct json_string_buffer* sb, char* str) {
  1799. // TODO: optimize
  1800. size_t len = strlen(str);
  1801. sb_check(sb, len);
  1802. memcpy(sb->buf + sb->length, str, len);
  1803. // strcat(sb->buf, str);
  1804. sb->length += len;
  1805. sb->line_len += len;
  1806. }
  1807. // checks size and concatenates a single char
  1808. static void sb_putc(struct json_string_buffer* sb, int c) {
  1809. // TODO: optimize
  1810. sb_check(sb, 1);
  1811. sb->buf[sb->length] = c;
  1812. // sb->buf[sb->length + 1] = 0;
  1813. sb->length++;
  1814. sb->line_len = (c == '\n') ? 0 : (sb->line_len + 1);
  1815. }
  1816. static char* sb_tail_check(struct json_string_buffer* sb, int more) {
  1817. sb_check(sb, more);
  1818. return sb->buf + sb->length;
  1819. }
  1820. #define sb_tail_catf(sb, fmt, ...) \
  1821. do { \
  1822. size_t _len = snprintf(NULL, 0, fmt, __VA_ARGS__); \
  1823. snprintf(sb_tail_check(sb, _len), _len + 1, fmt, __VA_ARGS__); \
  1824. sb->length += _len; \
  1825. sb->line_len += _len; \
  1826. } while(0);
  1827. void json_stringify(struct json_write_context* ctx, struct json_value* v) {
  1828. struct json_string_buffer* sb = ctx->sb;
  1829. char* float_format = ctx->fmt.floatFormat ? ctx->fmt.floatFormat : "%f";
  1830. char qc;
  1831. if(!v) {
  1832. //fprintf(stderr, "NULL value passed to %s()\n", __func__);
  1833. return;
  1834. }
  1835. switch(v->type) {
  1836. case JSON_TYPE_UNDEFINED:
  1837. sb_cat(sb, "undefined");
  1838. break;
  1839. case JSON_TYPE_NULL:
  1840. sb_cat(sb, "null");
  1841. break;
  1842. case JSON_TYPE_INT: // 2
  1843. sb_tail_catf(sb, "%ld", v->n); // TODO: handle bases, formats
  1844. break;
  1845. case JSON_TYPE_DOUBLE:
  1846. sb_tail_catf(sb, float_format, v->d); // TODO: handle infinity, nan, etc
  1847. break;
  1848. case JSON_TYPE_STRING:
  1849. qc = ctx->fmt.useSingleQuotes ? '\'' : '"';
  1850. sb_putc(sb, qc);
  1851. sb_cat_escaped(ctx, v->s);
  1852. sb_putc(sb, qc);
  1853. break;
  1854. case JSON_TYPE_OBJ:
  1855. json_obj_to_string(ctx, v);
  1856. break;
  1857. case JSON_TYPE_ARRAY: // 6
  1858. json_arr_to_string(ctx, v);
  1859. break;
  1860. case JSON_TYPE_COMMENT_SINGLE: // TODO: handle linebreaks in the comment string
  1861. sb_tail_catf(sb, "//%s\n", v->s);
  1862. break;
  1863. case JSON_TYPE_COMMENT_MULTI: // TODO: clean "*/" out of the comment string
  1864. sb_tail_catf(sb, "/* %s */\n", v->s);
  1865. break;
  1866. // default:
  1867. // fprintf(stderr, "c_json: unknown type in json_value_to_string\n");
  1868. }
  1869. }
  1870. static void ctx_indent(struct json_write_context* ctx) {
  1871. int i = 0;
  1872. int len = ctx->fmt.indentAmt * ctx->depth;
  1873. char* c = sb_tail_check(ctx->sb, len);
  1874. for(i = 0; i < len; i++) {
  1875. c[i] = ctx->fmt.indentChar;
  1876. }
  1877. c[len] = 0;
  1878. ctx->sb->length += len;
  1879. ctx->sb->line_len += len;
  1880. }
  1881. static int line_too_long(struct json_write_context* ctx) {
  1882. if(ctx->fmt.maxLineLength < 0) return 0;
  1883. if(ctx->sb->line_len <= ctx->fmt.maxLineLength) return 0;
  1884. return 1;
  1885. }
  1886. static void sb_cat_escaped(struct json_write_context* ctx, char* str) {
  1887. struct json_string_buffer* sb = ctx->sb;
  1888. char qc = ctx->fmt.useSingleQuotes ? '\'' : '"';
  1889. while(*str) {
  1890. char c = *str;
  1891. if(c == qc) sb_putc(sb, '\\');
  1892. sb_putc(sb, c);
  1893. str++;
  1894. }
  1895. }
  1896. static void json_arr_to_string(struct json_write_context* ctx, struct json_value* arr) {
  1897. struct json_link* n;
  1898. struct json_string_buffer* sb = ctx->sb;
  1899. int multiline = arr->len >= (size_t)ctx->fmt.minArraySzExpand;
  1900. sb_putc(sb, '[');
  1901. if(multiline) sb_putc(sb, '\n');
  1902. ctx->depth++;
  1903. n = arr->arr.head;
  1904. while(n) {
  1905. if(multiline) ctx_indent(ctx);
  1906. json_stringify(ctx, n->v);
  1907. n = n->next;
  1908. if(n) {
  1909. sb_putc(sb, ',');
  1910. if(!multiline) sb_putc(sb, ' ');
  1911. }
  1912. else if(ctx->fmt.trailingComma && multiline) sb_putc(sb, ',');
  1913. if(multiline) sb_putc(sb, '\n');
  1914. if(line_too_long(ctx)) {
  1915. sb_putc(sb, '\n');
  1916. ctx_indent(ctx);
  1917. }
  1918. }
  1919. ctx->depth--;
  1920. if(multiline) ctx_indent(ctx);
  1921. sb_putc(sb, ']');
  1922. }
  1923. static int key_must_have_quotes(char* key) {
  1924. if(isdigit(key[0])) return 1;
  1925. return strlen(key) != strspn(key, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$");
  1926. }
  1927. static void json_obj_to_string(struct json_write_context* ctx, struct json_value* obj) {
  1928. size_t i;
  1929. struct json_obj_field* f;
  1930. struct json_string_buffer* sb = ctx->sb;
  1931. int multiline = obj->len >= (size_t)ctx->fmt.minObjSzExpand;
  1932. int noquotes = ctx->fmt.noQuoteKeys;
  1933. char quoteChar = ctx->fmt.useSingleQuotes ? '\'' : '"';
  1934. sb_putc(sb, '{');
  1935. if(multiline) sb_putc(sb, '\n');
  1936. ctx->depth++;
  1937. size_t n = obj->len;
  1938. for(i = 0; i < obj->obj.alloc_size; i++) {
  1939. f = &obj->obj.buckets[i];
  1940. if(f->key == NULL) continue;
  1941. if(multiline) ctx_indent(ctx);
  1942. int needquotes = key_must_have_quotes(f->key);
  1943. if(!noquotes || needquotes) {
  1944. sb_putc(sb, quoteChar);
  1945. sb_cat_escaped(ctx, f->key);
  1946. sb_putc(sb, quoteChar);
  1947. }
  1948. else sb_cat(sb, f->key);
  1949. sb_putc(sb, ':');
  1950. if(ctx->fmt.objColonSpace) sb_putc(sb, ' ');
  1951. json_stringify(ctx, f->value);
  1952. if(n-- > 1) {
  1953. sb_putc(sb, ',');
  1954. if(!multiline) sb_putc(sb, ' ');
  1955. }
  1956. else if(ctx->fmt.trailingComma && multiline) sb_putc(sb, ',');
  1957. if(multiline) sb_putc(sb, '\n');
  1958. if(line_too_long(ctx)) {
  1959. sb_putc(sb, '\n');
  1960. ctx_indent(ctx);
  1961. }
  1962. }
  1963. ctx->depth--;
  1964. if(multiline) ctx_indent(ctx);
  1965. sb_putc(sb, '}');
  1966. }
  1967. struct json_value* json_new_str(char* s) {
  1968. return json_new_strn(s, strlen(s));
  1969. }
  1970. struct json_value* json_new_strn(char* s, size_t len) {
  1971. struct json_value* v;
  1972. v = malloc(sizeof(*v));
  1973. v->type = JSON_TYPE_STRING;
  1974. v->s = strndup(s, len);
  1975. v->len = len;
  1976. v->base = 0;
  1977. return v;
  1978. }
  1979. struct json_value* json_new_double(double d) {
  1980. struct json_value* v;
  1981. v = malloc(sizeof(*v));
  1982. v->type = JSON_TYPE_DOUBLE;
  1983. v->d = d;
  1984. v->len = 0;
  1985. v->base = 0;
  1986. return v;
  1987. }
  1988. struct json_value* json_new_int(int64_t n) {
  1989. struct json_value* v;
  1990. v = malloc(sizeof(*v));
  1991. v->type = JSON_TYPE_INT;
  1992. v->n = n;
  1993. v->len = 0;
  1994. v->base = 0;
  1995. return v;
  1996. }
  1997. struct json_value* json_new_array() {
  1998. struct json_value* v;
  1999. v = malloc(sizeof(*v));
  2000. v->type = JSON_TYPE_ARRAY;
  2001. v->arr.head = NULL;
  2002. v->arr.tail = NULL;
  2003. v->len = 0;
  2004. v->base = 0;
  2005. return v;
  2006. }
  2007. struct json_value* json_new_object(size_t initial_alloc_size) {
  2008. struct json_value* obj = malloc(sizeof(*obj));
  2009. obj->type = JSON_TYPE_OBJ;
  2010. obj->len = 0;
  2011. obj->base = 0;
  2012. obj->obj.alloc_size = initial_alloc_size;
  2013. obj->obj.buckets = calloc(1, sizeof(*obj->obj.buckets) * obj->obj.alloc_size);
  2014. if(!obj->obj.buckets) {
  2015. free(obj);
  2016. return NULL;
  2017. }
  2018. return obj;
  2019. }
  2020. struct json_value* json_new_null() {
  2021. struct json_value* v;
  2022. v = malloc(sizeof(*v));
  2023. v->type = JSON_TYPE_NULL;
  2024. return v;
  2025. }
  2026. struct json_value* json_new_undefined() {
  2027. struct json_value* v;
  2028. v = malloc(sizeof(*v));
  2029. v->type = JSON_TYPE_UNDEFINED;
  2030. return v;
  2031. }
  2032. struct json_value* json_new_true() {
  2033. struct json_value* v;
  2034. v = malloc(sizeof(*v));
  2035. v->type = JSON_TYPE_BOOL;
  2036. v->n = 1; // true
  2037. v->len = 0;
  2038. v->base = 0;
  2039. return v;
  2040. }
  2041. struct json_value* json_new_false() {
  2042. struct json_value* v;
  2043. v = malloc(sizeof(*v));
  2044. v->type = JSON_TYPE_BOOL;
  2045. v->n = 0; // false
  2046. v->len = 0;
  2047. v->base = 0;
  2048. return v;
  2049. }