json.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  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. return 1; // end of file
  999. }
  1000. // lex error
  1001. jl->error = JSON_LEX_ERROR_INVALID_CHAR;
  1002. return 1;
  1003. }
  1004. //printf("lol\n");
  1005. lex_next_char(jl);
  1006. }
  1007. //printf("token %d:%d ", jl->line_num, jl->char_num);
  1008. dbg_print_token(&jl->cur_tok);
  1009. jl->eoi = jl->head >= jl->end;
  1010. if(jl->error) return jl->error;
  1011. return jl->eoi;
  1012. }
  1013. static int parser_indent_level = 0;
  1014. static void dbg_parser_indent(void) { return;
  1015. int i;
  1016. for(i = 0; i < parser_indent_level; i++) {
  1017. dbg_printf(" ");
  1018. }
  1019. }
  1020. static void parser_push(struct json_parser* jp, struct json_value* v) {
  1021. void* tmp;
  1022. int alloc = jp->stack_alloc;
  1023. int cnt = jp->stack_cnt;
  1024. // check size
  1025. if(cnt >= alloc) {
  1026. if(alloc == 0) alloc = 16;
  1027. alloc *= 2;
  1028. tmp = realloc(jp->stack, alloc * sizeof(*(jp->stack)));
  1029. if(!tmp) {
  1030. jp->error = JSON_ERROR_OOM;
  1031. return;
  1032. }
  1033. jp->stack = tmp;
  1034. jp->stack_alloc = alloc;
  1035. }
  1036. jp->stack[cnt] = v;
  1037. jp->stack_cnt++;
  1038. }
  1039. static struct json_value* parser_pop(struct json_parser* jp) {
  1040. if(jp->stack_cnt <= 0) {
  1041. jp->error = JSON_PARSER_ERROR_STACK_EXHAUSTED;
  1042. return NULL;
  1043. }
  1044. return jp->stack[--jp->stack_cnt];
  1045. }
  1046. static void parser_push_new_array(struct json_parser* jp) {
  1047. struct json_value* val;
  1048. val = malloc(sizeof(*val));
  1049. if(!val) {
  1050. jp->error = JSON_ERROR_OOM;
  1051. return;
  1052. }
  1053. val->type = JSON_TYPE_ARRAY;
  1054. val->len = 0;
  1055. val->base = 0;
  1056. val->arr.head = NULL;
  1057. val->arr.tail = NULL;
  1058. parser_push(jp, val);
  1059. }
  1060. static void parser_push_new_object(struct json_parser* jp) {
  1061. struct json_value* val;
  1062. val = json_new_object(8);
  1063. if(!val) {
  1064. jp->error = JSON_ERROR_OOM;
  1065. return;
  1066. }
  1067. // printf("vt: %d\n", val);
  1068. dbg_dump_stack(jp, 3);
  1069. parser_push(jp, val);
  1070. dbg_dump_stack(jp, 4);
  1071. }
  1072. // not used atm. comments will probably be moved to a side channel
  1073. static void consume_comments(struct json_parser* jp) {
  1074. while(1) {
  1075. if(jp->cur_tok.tokenType != TOKEN_COMMENT) break;
  1076. parser_push(jp, jp->cur_tok.val);
  1077. lex_next_token(jp);
  1078. }
  1079. }
  1080. static void consume_commas(struct json_parser* jp) {
  1081. // lex_next_token(jp);
  1082. while(jp->cur_tok.tokenType == TOKEN_COMMA) lex_next_token(jp);
  1083. }
  1084. static void reduce_array(struct json_parser* jp) {
  1085. /* what the stack should look like now
  1086. ...
  1087. 1 array
  1088. 0 any value
  1089. */
  1090. if(jp->stack_cnt < 2) {
  1091. dbg_printf("stack too short in reduce_array: %d \n", jp->stack_cnt);
  1092. jp->error = JSON_PARSER_ERROR_STACK_EXHAUSTED;
  1093. return;
  1094. }
  1095. struct json_value** st = jp->stack + jp->stack_cnt - 1;
  1096. struct json_value* v = st[0];
  1097. struct json_value* arr = st[-1];
  1098. if(arr == ROOT_VALUE) return;
  1099. if(arr->type != JSON_TYPE_ARRAY) {
  1100. jp->error = JSON_PARSER_ERROR_CORRUPT_STACK;
  1101. return;
  1102. }
  1103. // append v to arr
  1104. json_array_push_tail(arr, v);
  1105. jp->stack_cnt--;
  1106. }
  1107. static void reduce_object(struct json_parser* jp) {
  1108. /* what the stack should look like now
  1109. ...
  1110. 2 object
  1111. 1 label
  1112. 0 any value
  1113. */
  1114. if(jp->stack_cnt < 3) {
  1115. jp->error = JSON_PARSER_ERROR_STACK_EXHAUSTED;
  1116. return;
  1117. }
  1118. struct json_value** st = jp->stack + jp->stack_cnt - 1;
  1119. struct json_value* v = st[0];
  1120. struct json_value* l = st[-1];
  1121. struct json_value* obj = st[-2];
  1122. if(obj == ROOT_VALUE) return;
  1123. // TODO: check label type
  1124. dbg_dump_stack(jp, 10);
  1125. if(obj->type != JSON_TYPE_OBJ) {// printf("invalid obj\n");
  1126. dbg_printf("0 type: %d \n", v->type);
  1127. dbg_printf("1 type: %d \n", l->type);
  1128. dbg_printf("2 type: %d \n", obj->type);
  1129. dbg_printf("3 type: %d \n", st[-3]->type);
  1130. jp->error = JSON_PARSER_ERROR_CORRUPT_STACK;
  1131. return;
  1132. }
  1133. // insert l:v into obj
  1134. json_obj_set_key_nodup(obj, l->s, v);
  1135. free(l);
  1136. jp->stack_cnt -= 2;
  1137. }
  1138. static struct json_parser* parse_token_stream(char* source, size_t len) {
  1139. int i;
  1140. struct json_parser* jp;
  1141. jp = calloc(1, sizeof(*jp));
  1142. if(!jp) {
  1143. return NULL;
  1144. }
  1145. jp->source = source;
  1146. jp->end = source + len;
  1147. jp->source_len = len;
  1148. jp->head = source;
  1149. jp->line_num = 1; // these are 1-based
  1150. jp->char_num = 1;
  1151. i = 0;
  1152. #define next() lex_next_token(jp); //) goto UNEXPECTED_EOI;
  1153. #define is_end() if(jp->eoi) goto UNEXPECTED_EOI;
  1154. // the root value sentinel helps a few algorithms and marks a proper end of input
  1155. parser_push(jp, ROOT_VALUE);
  1156. // get the first token
  1157. lex_next_token(jp);
  1158. if(jp->cur_tok.tokenType == TOKEN_OBJ_START) {
  1159. parser_push_new_object(jp);
  1160. next();
  1161. goto PARSE_OBJ;
  1162. } else if(jp->cur_tok.tokenType == TOKEN_ARRAY_START) {
  1163. parser_push_new_array(jp);
  1164. next();
  1165. goto PARSE_ARRAY;
  1166. }
  1167. PARSE_ARRAY: // not actually starting with an array; this is just the type probing code
  1168. dbg_parser_indent();
  1169. dbg_printf("\nparse_array l:%d, c:%d \n", jp->line_num, jp->char_num);
  1170. // if(jp->eoi) goto CHECK_END;
  1171. if(jp->cur_tok.tokenType == TOKEN_ARRAY_END) {
  1172. dbg_parser_indent();dbg_printf("array- TOKEN_ARR_END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1173. parser_indent_level--;
  1174. /* what the stack should look like now
  1175. ...
  1176. 3 parent container
  1177. 2 ? possibly a label ?
  1178. 1 -- resume sentinel --
  1179. 0 array to be closed
  1180. */
  1181. {
  1182. // TODO check stack depth
  1183. struct json_value* closed = parser_pop(jp);
  1184. struct json_value* sentinel = parser_pop(jp);
  1185. // put the closed array back on the stack then reduce it appropriately
  1186. parser_push(jp, closed);
  1187. if(sentinel == RESUME_ARRAY) {
  1188. reduce_array(jp);
  1189. next();
  1190. // consume_commas(jp);
  1191. goto PARSE_ARRAY;
  1192. }
  1193. else if(sentinel == RESUME_OBJ) {
  1194. reduce_object(jp);
  1195. next();
  1196. // consume_commas(jp);
  1197. goto PARSE_OBJ;
  1198. }
  1199. else if(sentinel == ROOT_VALUE) {
  1200. // proper finish
  1201. goto END;
  1202. }
  1203. else {
  1204. goto INVALID_SENTINEL;
  1205. }
  1206. }
  1207. dbg_printf("ending array in begining of array\n");
  1208. goto UNEXPECTED_TOKEN;
  1209. }
  1210. // cycle: val, comma
  1211. switch(jp->cur_tok.tokenType) {
  1212. case TOKEN_ARRAY_START: dbg_parser_indent();dbg_printf("TOKEN_ARRAY_START l:%d, c:%d \n", jp->line_num, jp->char_num);
  1213. parser_indent_level++;
  1214. parser_push(jp, RESUME_ARRAY);
  1215. parser_push_new_array(jp);
  1216. next();
  1217. goto PARSE_ARRAY;
  1218. case TOKEN_ARRAY_END: dbg_parser_indent();dbg_printf("TOKEN_ARRAY_END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1219. parser_indent_level--;
  1220. /* what the stack should look like now
  1221. ...
  1222. 3 parent container
  1223. 2 ? possibly a label ?
  1224. 1 -- resume sentinel --
  1225. 0 array to be closed
  1226. */
  1227. {
  1228. // TODO check stack depth
  1229. struct json_value* closed = parser_pop(jp);
  1230. struct json_value* sentinel = parser_pop(jp);
  1231. // put the closed array back on the stack then reduce it appropriately
  1232. parser_push(jp, closed);
  1233. if(sentinel == RESUME_ARRAY) {
  1234. reduce_array(jp);
  1235. // consume_commas(jp);
  1236. next();
  1237. goto PARSE_ARRAY;
  1238. }
  1239. else if(sentinel == RESUME_OBJ) {
  1240. reduce_object(jp);
  1241. // consume_commas(jp);
  1242. next();
  1243. goto PARSE_OBJ;
  1244. }
  1245. else if(sentinel == ROOT_VALUE) {
  1246. // proper finish
  1247. goto END;
  1248. }
  1249. else {
  1250. goto INVALID_SENTINEL;
  1251. }
  1252. }
  1253. // consume_commas(jp);
  1254. // next();
  1255. break;
  1256. case TOKEN_OBJ_START: dbg_parser_indent();dbg_printf("PARSE_OBJ l:%d, c:%d \n", jp->line_num, jp->char_num);
  1257. parser_indent_level++;
  1258. parser_push(jp, RESUME_ARRAY);
  1259. parser_push_new_object(jp);
  1260. next();
  1261. goto PARSE_OBJ;
  1262. case TOKEN_STRING:
  1263. case TOKEN_NUMBER:
  1264. case TOKEN_NULL:
  1265. case TOKEN_UNDEFINED: dbg_parser_indent();dbg_printf("PARSE VALUE l:%d, c:%d \n", jp->line_num, jp->char_num);
  1266. parser_push(jp, jp->cur_tok.val);
  1267. reduce_array(jp);
  1268. // consume_commas(jp);
  1269. next();
  1270. goto PARSE_ARRAY;
  1271. case TOKEN_OBJ_END: dbg_parser_indent();dbg_printf("BRACKET_MISMATCH l:%d, c:%d \n", jp->line_num, jp->char_num);
  1272. goto BRACKET_MISMATCH;
  1273. case TOKEN_COMMA:
  1274. next();
  1275. goto PARSE_ARRAY;
  1276. case TOKEN_NONE:
  1277. case TOKEN_LABEL:
  1278. case TOKEN_COLON:
  1279. default: dbg_printf("UNEXPECTED_TOKEN l:%d, c:%d \n", jp->line_num, jp->char_num);
  1280. // invalid
  1281. goto UNEXPECTED_TOKEN;
  1282. }
  1283. return NULL;
  1284. PARSE_OBJ:
  1285. dbg_parser_indent();dbg_printf("\nparse_obj l:%d, c:%d \n", jp->line_num, jp->char_num);
  1286. // cycle: label, colon, val, comma
  1287. consume_commas(jp);
  1288. if(jp->cur_tok.tokenType == TOKEN_OBJ_END) {
  1289. dbg_parser_indent();dbg_printf("obj- TOKEN_OBJ_END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1290. parser_indent_level--;
  1291. /* what the stack should look like now
  1292. ...
  1293. 3 parent container
  1294. 2 ? possibly a label ?
  1295. 1 -- resume sentinel --
  1296. 0 object to be closed
  1297. */
  1298. {
  1299. // TODO check stack depth
  1300. struct json_value* closed = parser_pop(jp);
  1301. struct json_value* sentinel = parser_pop(jp);
  1302. // put the closed array back on the stack then reduce it appropriately
  1303. parser_push(jp, closed);
  1304. if(sentinel == RESUME_ARRAY) {
  1305. reduce_array(jp);
  1306. next();
  1307. // consume_commas(jp);
  1308. goto PARSE_ARRAY;
  1309. }
  1310. else if(sentinel == RESUME_OBJ) {
  1311. reduce_object(jp);
  1312. next();
  1313. // consume_commas(jp);
  1314. goto PARSE_OBJ;
  1315. }
  1316. else if(sentinel == ROOT_VALUE) {
  1317. // proper finish
  1318. goto END;
  1319. }
  1320. else {
  1321. goto INVALID_SENTINEL;
  1322. }
  1323. }
  1324. dbg_printf("ending obj in begining of obj\n");
  1325. goto UNEXPECTED_TOKEN;
  1326. }
  1327. switch(jp->cur_tok.tokenType) {
  1328. case TOKEN_LABEL:
  1329. case TOKEN_STRING:
  1330. case TOKEN_TRUE:
  1331. case TOKEN_FALSE:
  1332. case TOKEN_INFINITY:
  1333. case TOKEN_NULL:
  1334. case TOKEN_UNDEFINED:
  1335. case TOKEN_NAN:
  1336. parser_push(jp, jp->cur_tok.val);
  1337. break;
  1338. default:
  1339. // error
  1340. dbg_printf("!!!missing label\n");
  1341. goto UNEXPECTED_TOKEN;
  1342. }
  1343. dbg_dump_stack(jp, 5);
  1344. next();
  1345. if(jp->cur_tok.tokenType != TOKEN_COLON) {
  1346. // error
  1347. dbg_printf("!!!missing colon\n");
  1348. goto UNEXPECTED_TOKEN;
  1349. }
  1350. next();
  1351. switch(jp->cur_tok.tokenType) {
  1352. case TOKEN_ARRAY_START: dbg_parser_indent();dbg_printf("obj- TOKEN_ARRAY_START l:%d, c:%d \n", jp->line_num, jp->char_num);
  1353. parser_indent_level++;
  1354. parser_push(jp, RESUME_OBJ);
  1355. parser_push_new_array(jp);
  1356. next();
  1357. goto PARSE_ARRAY;
  1358. 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);
  1359. dbg_printf("!!! escaped sentinel block\n");
  1360. break;
  1361. case TOKEN_OBJ_START: dbg_parser_indent();dbg_printf("obj- TOKEN_OBJ_START l:%d, c:%d \n", jp->line_num, jp->char_num);
  1362. parser_indent_level++;
  1363. parser_push(jp, RESUME_OBJ);
  1364. parser_push_new_object(jp); // BUG
  1365. next();
  1366. goto PARSE_OBJ;
  1367. case TOKEN_STRING:
  1368. case TOKEN_NUMBER:
  1369. case TOKEN_TRUE:
  1370. case TOKEN_FALSE:
  1371. case TOKEN_INFINITY:
  1372. case TOKEN_NULL:
  1373. case TOKEN_UNDEFINED: dbg_parser_indent();dbg_printf("obj- TOKEN VALUE l:%d, c:%d \n", jp->line_num, jp->char_num);
  1374. parser_push(jp, jp->cur_tok.val);
  1375. reduce_object(jp);
  1376. next();
  1377. consume_commas(jp);
  1378. goto PARSE_OBJ;
  1379. case TOKEN_ARRAY_END: dbg_parser_indent();dbg_printf("obj- BRACE_MISMATCH l:%d, c:%d \n", jp->line_num, jp->char_num);
  1380. goto BRACE_MISMATCH;
  1381. case TOKEN_COMMA: dbg_parser_indent();dbg_printf("obj- eating comma l:%d, c:%d \n", jp->line_num, jp->char_num);
  1382. next();
  1383. goto PARSE_OBJ;
  1384. case TOKEN_NONE:
  1385. case TOKEN_LABEL:
  1386. case TOKEN_COLON:
  1387. default: dbg_printf("obj- UNEXPECTED_TOKEN l:%d, c:%d \n", jp->line_num, jp->char_num);
  1388. // invalid
  1389. goto UNEXPECTED_TOKEN;
  1390. }
  1391. dbg_parser_indent();dbg_printf("end of obj\n");
  1392. return NULL;
  1393. CHECK_END: dbg_parser_indent();dbg_printf("!!! CHECK_END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1394. if(jp->stack_cnt == 2) {
  1395. // check root value sentinel
  1396. // good, fall through to END
  1397. }
  1398. else if(jp->stack_cnt == 1) {
  1399. // the file is empty
  1400. goto UNEXPECTED_EOI;
  1401. }
  1402. else {
  1403. // stuff is left on the stack
  1404. goto UNEXPECTED_EOI;
  1405. }
  1406. //i = *((int*)0);
  1407. END: dbg_printf("!!! END l:%d, c:%d \n", jp->line_num, jp->char_num);
  1408. if(jp->error) dbg_printf("parsing error: %d\n", jp->error);
  1409. return jp;
  1410. UNEXPECTED_EOI: // end of input
  1411. dbg_printf("!!! UNEXPECTED_EOI l:%d, c:%d \n", jp->line_num, jp->char_num);
  1412. jp->error = JSON_PARSER_ERROR_UNEXPECTED_EOI;
  1413. return jp;
  1414. UNEXPECTED_TOKEN: dbg_printf("!!! UNEXPECTED_TOKEN l:%d, c:%d \n", jp->line_num, jp->char_num);
  1415. jp->error = JSON_PARSER_ERROR_UNEXPECTED_TOKEN;
  1416. return jp;
  1417. BRACE_MISMATCH: dbg_printf("!!! BRACE_MISMATCH l:%d, c:%d \n", jp->line_num, jp->char_num);
  1418. jp->error = JSON_PARSER_ERROR_BRACE_MISMATCH;
  1419. return jp;
  1420. BRACKET_MISMATCH: dbg_printf("!!! BRACKET_MISMATCH l:%d, c:%d \n", jp->line_num, jp->char_num);
  1421. jp->error = JSON_PARSER_ERROR_BRACKET_MISMATCH;
  1422. return jp;
  1423. INVALID_SENTINEL: dbg_printf("!!! INVALID_SENTINEL l:%d, c:%d \n", jp->line_num, jp->char_num);
  1424. jp->error = JSON_PARSER_ERROR_CORRUPT_STACK;
  1425. return jp;
  1426. }
  1427. struct json_file* json_load_path(char* path) {
  1428. struct json_file* jf;
  1429. FILE* f;
  1430. f = fopen(path, "rb");
  1431. if(!f) {
  1432. //fprintf(stderr, "JSON: no such file: \"%s\"\n", path);
  1433. return NULL;
  1434. }
  1435. jf = json_read_file(f);
  1436. fclose(f);
  1437. return jf;
  1438. }
  1439. struct json_file* json_read_file(FILE* f) {
  1440. size_t fsz;
  1441. char* contents;
  1442. struct json_file* jf;
  1443. size_t nr;
  1444. // check file size
  1445. fseek(f, 0, SEEK_END);
  1446. fsz = ftell(f);
  1447. fseek(f, 0, SEEK_SET);
  1448. contents = malloc(fsz+1);
  1449. contents[fsz] = 0; // some crt functions might read past the end otherwise
  1450. nr = fread(contents, 1, fsz, f);
  1451. jf = json_parse_string(contents, fsz);
  1452. free(contents);
  1453. return jf;
  1454. }
  1455. struct json_file* json_parse_string(char* source, size_t len) {
  1456. struct json_lexer* jl;
  1457. struct json_parser* jp;
  1458. struct json_file* jf;
  1459. jp = parse_token_stream(source, len);
  1460. if(!jp) {
  1461. //printf("JSON: failed to parse token stream \n");
  1462. return NULL;
  1463. }
  1464. jf = calloc(1, sizeof(*jf));
  1465. if(!jf) return NULL;
  1466. if(jp->stack_cnt == 1) {
  1467. jf->root = jp->stack[1];
  1468. //printf("JSON: failed to parse token stream (2)\n");
  1469. }
  1470. // else some sort of error. probably EOI
  1471. jf->error = jp->error;
  1472. if(jf->error) {
  1473. jf->error_line_num = jp->line_num;
  1474. jf->error_char_num = jp->char_num;
  1475. jf->error_str = json_get_err_str(jf->error);
  1476. }
  1477. json_parser_free(jp);
  1478. free(jp);
  1479. //json_dump_value(*jp->stack, 0, 10);
  1480. //json_dump_value(jf->root, 0, 10);
  1481. return jf;
  1482. }
  1483. static void free_array(struct json_value* arr) {
  1484. struct json_link* n, *p;
  1485. n = arr->arr.head;
  1486. while(n) {
  1487. json_free(n->v);
  1488. p = n;
  1489. n = n->next;
  1490. free(p);
  1491. }
  1492. }
  1493. static void free_obj(struct json_value* o) {
  1494. size_t freed = 0;
  1495. size_t i;
  1496. for(i = 0; i < o->obj.alloc_size && freed < o->len; i++) {
  1497. struct json_obj_field* b;
  1498. b = &o->obj.buckets[i];
  1499. if(b->key == NULL) continue;
  1500. free(b->key);
  1501. json_free(b->value);
  1502. freed++;
  1503. }
  1504. free(o->obj.buckets);
  1505. }
  1506. // must manage v's memory manually
  1507. void json_free(struct json_value* v) {
  1508. if(!v) return;
  1509. switch(v->type) {
  1510. case JSON_TYPE_STRING:
  1511. case JSON_TYPE_COMMENT_SINGLE:
  1512. case JSON_TYPE_COMMENT_MULTI:
  1513. free(v->s);
  1514. break;
  1515. case JSON_TYPE_OBJ:
  1516. free_obj(v);
  1517. break;
  1518. case JSON_TYPE_ARRAY:
  1519. free_array(v);
  1520. break;
  1521. }
  1522. free(v);
  1523. }
  1524. void json_file_free(struct json_file* jsf){
  1525. json_free(jsf->root);
  1526. if(jsf->lex_info) {
  1527. free(jsf->lex_info);
  1528. }
  1529. free(jsf);
  1530. }
  1531. #define tc(x, y, z) case x: dbg_printf(#x " foo: " y "\n", z); break;
  1532. #define tcl(x) case x: dbg_printf(#x "\n"); break;
  1533. static void dbg_print_token(struct token* ts) {
  1534. switch(ts->tokenType) {
  1535. tcl(TOKEN_NONE)
  1536. tcl(TOKEN_ARRAY_START)
  1537. tcl(TOKEN_ARRAY_END)
  1538. tcl(TOKEN_OBJ_START)
  1539. tcl(TOKEN_OBJ_END)
  1540. tc(TOKEN_STRING, "%s", ts->val->s)
  1541. tc(TOKEN_NUMBER, "%d", (int)ts->val->n)
  1542. tcl(TOKEN_NULL)
  1543. tcl(TOKEN_INFINITY)
  1544. tcl(TOKEN_UNDEFINED)
  1545. tcl(TOKEN_NAN)
  1546. tc(TOKEN_LABEL, "%s", ts->val->s)
  1547. tcl(TOKEN_COMMA)
  1548. tcl(TOKEN_COLON)
  1549. tc(TOKEN_COMMENT, "%s", ts->val->s)
  1550. }
  1551. }
  1552. static void dbg_print_value(struct json_value* v) {
  1553. if(v == ROOT_VALUE) {
  1554. dbg_printf("ROOT_VALUE sentinel\n");
  1555. return;
  1556. }
  1557. if(v == RESUME_ARRAY) {
  1558. dbg_printf("RESUME_ARRAY sentinel\n");
  1559. return;
  1560. }
  1561. if(v == RESUME_OBJ) {
  1562. dbg_printf("RESUME_OBJ sentinel\n");
  1563. return;
  1564. }
  1565. switch(v->type) {
  1566. case JSON_TYPE_UNDEFINED: dbg_printf("undefined\n"); break;
  1567. case JSON_TYPE_NULL: dbg_printf("null\n"); break;
  1568. case JSON_TYPE_INT: dbg_printf("int: %d\n", (int)v->n); break;
  1569. case JSON_TYPE_DOUBLE: dbg_printf("double %f\n", v->d); break;
  1570. case JSON_TYPE_STRING: dbg_printf("string: \"%s\"\n", v->s); break;
  1571. case JSON_TYPE_OBJ: dbg_printf("object [%d]\n", (int)v->len); break;
  1572. case JSON_TYPE_ARRAY: dbg_printf("array [%d]\n", (int)v->len); break;
  1573. case JSON_TYPE_COMMENT_SINGLE: dbg_printf("comment, single\n"); break;
  1574. case JSON_TYPE_COMMENT_MULTI: dbg_printf("comment, multiline\n"); break;
  1575. default:
  1576. dbg_printf("unknown enum: %d\n", v->type);
  1577. }
  1578. }
  1579. static void dbg_dump_stack(struct json_parser* jp, int depth) {
  1580. int i = 0;
  1581. return;
  1582. for(i = 0; i < depth && i < jp->stack_cnt; i++) {
  1583. dbg_parser_indent();
  1584. dbg_printf("%d: ", i);
  1585. dbg_print_value(jp->stack[jp->stack_cnt - i - 1]);
  1586. }
  1587. }
  1588. char* json_get_type_str(enum json_type t) {
  1589. switch(t) {
  1590. case JSON_TYPE_UNDEFINED: return "undefined";
  1591. case JSON_TYPE_NULL: return "null";
  1592. case JSON_TYPE_INT: return "integer";
  1593. case JSON_TYPE_DOUBLE: return "double";
  1594. case JSON_TYPE_STRING: return "string";
  1595. case JSON_TYPE_OBJ: return "object";
  1596. case JSON_TYPE_ARRAY: return "array";
  1597. case JSON_TYPE_COMMENT_SINGLE: return "single-line comment";
  1598. case JSON_TYPE_COMMENT_MULTI: return "multi-line comment";
  1599. default: return "Invalid JSON type";
  1600. }
  1601. }
  1602. char* json_get_err_str(enum json_error e) {
  1603. switch(e) {
  1604. case JSON_ERROR_NONE: return "No error";
  1605. case JSON_ERROR_OOM: return "Out of memory";
  1606. case JSON_LEX_ERROR_NULL_IN_STRING: return "Null byte found in string";
  1607. case JSON_LEX_ERROR_NULL_BYTE: return "Null byte found in file";
  1608. case JSON_LEX_ERROR_INVALID_STRING: return "Invalid string";
  1609. case JSON_LEX_ERROR_UNEXPECTED_END_OF_INPUT: return "Unexpected end of input in lexer";
  1610. case JSON_LEX_ERROR_INVALID_CHAR: return "Invalid character code";
  1611. case JSON_PARSER_ERROR_CORRUPT_STACK: return "Parser stack corrupted";
  1612. case JSON_PARSER_ERROR_STACK_EXHAUSTED: return "Parser stack prematurely exhausted";
  1613. case JSON_PARSER_ERROR_UNEXPECTED_EOI: return "Unexpected end of input";
  1614. case JSON_PARSER_ERROR_UNEXPECTED_TOKEN: return "Unexpected token";
  1615. case JSON_PARSER_ERROR_BRACE_MISMATCH: return "Brace mismatch";
  1616. case JSON_PARSER_ERROR_BRACKET_MISMATCH: return "Bracket mismatch";
  1617. default: return "Invalid Error Code";
  1618. }
  1619. }
  1620. struct json_value* json_deep_copy(struct json_value* v) {
  1621. struct json_value* c;
  1622. c = malloc(sizeof(*c));
  1623. c->type = v->type;
  1624. switch(v->type) {
  1625. default:
  1626. case JSON_TYPE_INT:
  1627. case JSON_TYPE_DOUBLE:
  1628. c->n = v->n;
  1629. c->base = v->base;
  1630. break;
  1631. case JSON_TYPE_ARRAY:
  1632. c->len = v->len;
  1633. if(v->len == 0) {
  1634. c->arr.head = NULL;
  1635. c->arr.tail = NULL;
  1636. }
  1637. else {
  1638. struct json_link* cl = NULL;
  1639. struct json_link* cl_last, *vl;
  1640. cl_last = NULL;
  1641. vl = v->arr.head;
  1642. while(vl) {
  1643. cl = malloc(sizeof(*cl));
  1644. cl->prev = cl_last;
  1645. if(cl_last) {
  1646. cl_last->next = cl;
  1647. }
  1648. else {
  1649. c->arr.head = cl;
  1650. }
  1651. cl->v = json_deep_copy(vl->v);
  1652. cl_last = cl;
  1653. vl = vl->next;
  1654. }
  1655. cl->next = NULL;
  1656. c->arr.tail = cl;
  1657. }
  1658. break;
  1659. case JSON_TYPE_OBJ:
  1660. c->obj.alloc_size = v->obj.alloc_size;
  1661. c->len = v->len;
  1662. c->obj.buckets = calloc(1, sizeof(c->obj.buckets) * c->obj.alloc_size);
  1663. for(size_t i = 0, j = 0; j < v->len && i < v->obj.alloc_size; i++) {
  1664. if(v->obj.buckets[i].key) {
  1665. c->obj.buckets[i].key = strdup(v->obj.buckets[i].key);
  1666. c->obj.buckets[i].hash = v->obj.buckets[i].hash;
  1667. c->obj.buckets[i].value = json_deep_copy(v->obj.buckets[i].value);
  1668. j++;
  1669. }
  1670. }
  1671. break;
  1672. }
  1673. return c;
  1674. }
  1675. // scalar and disparate types take the value of from
  1676. // appends arrays
  1677. // recursively merges objects
  1678. void json_merge(struct json_value* into, struct json_value* from) {
  1679. // append two arrays
  1680. if(into->type == JSON_TYPE_ARRAY && from->type == JSON_TYPE_ARRAY) {
  1681. struct json_link* fl;
  1682. fl = from->arr.head;
  1683. while(fl) {
  1684. json_array_push_tail(into, json_deep_copy(fl->v));
  1685. }
  1686. return;
  1687. }
  1688. // disparate or scalar types
  1689. if(into->type != JSON_TYPE_OBJ || from->type != JSON_TYPE_OBJ) {
  1690. // clean out into first
  1691. if(into->type == JSON_TYPE_ARRAY) {
  1692. free_array(into);
  1693. }
  1694. else if(into->type == JSON_TYPE_OBJ) {
  1695. free_obj(into);
  1696. }
  1697. // deep-copy an array or obect from value
  1698. if(from->type == JSON_TYPE_ARRAY || from->type == JSON_TYPE_OBJ) {
  1699. struct json_value* tmp;
  1700. tmp = json_deep_copy(from);
  1701. memcpy(into, tmp, sizeof(*into));
  1702. free(tmp);
  1703. return;
  1704. }
  1705. // simple copy of scalars
  1706. memcpy(into, from, sizeof(*into));
  1707. return;
  1708. }
  1709. // merge objects
  1710. void* fi = NULL;
  1711. char* key;
  1712. struct json_value* fv;
  1713. while(json_obj_next(from, &fi, &key, &fv)) {
  1714. struct json_value* iv;
  1715. if(json_obj_get_key(into, key, &iv)) {
  1716. json_obj_set_key(into, key, json_deep_copy(fv));
  1717. }
  1718. else { // key exists in into
  1719. json_merge(iv, fv);
  1720. }
  1721. }
  1722. }
  1723. static void spaces(int depth, int w) {
  1724. int i;
  1725. for(i = 0; i < depth * w; i++) putchar(' ');
  1726. }
  1727. // shitty recursive fn for debugging
  1728. void json_dump_value(struct json_value* root, int cur_depth, int max_depth) {
  1729. void* iter;
  1730. char* key;
  1731. struct json_value* v;
  1732. //printf("-----------------------------------\n\n\n");
  1733. if(root->type == JSON_TYPE_ARRAY) {
  1734. spaces(cur_depth, 4);
  1735. dbg_printf("[\n");
  1736. // do shit
  1737. spaces(cur_depth, 4);
  1738. dbg_printf("]\n");
  1739. }
  1740. else if(root->type == JSON_TYPE_OBJ) {
  1741. dbg_printf("{\n");
  1742. // do shit
  1743. iter = NULL;
  1744. while(json_obj_next(root, &iter, &key, &v)) {
  1745. //printf("looping\n;");
  1746. spaces(cur_depth, 4);
  1747. dbg_printf("%s: ", key);
  1748. json_dump_value(v, cur_depth+1, max_depth);
  1749. }
  1750. spaces(cur_depth, 4);
  1751. dbg_printf("}\n");
  1752. }
  1753. else {
  1754. //printf("here");
  1755. dbg_print_value(root);
  1756. dbg_printf("\n");
  1757. }
  1758. }
  1759. // JSON output
  1760. static void sb_cat_escaped(struct json_write_context* ctx, char* str);
  1761. static void json_obj_to_string(struct json_write_context* sb, struct json_value* obj);
  1762. static void json_arr_to_string(struct json_write_context* sb, struct json_value* arr);
  1763. struct json_string_buffer* json_string_buffer_create(size_t initSize) {
  1764. struct json_string_buffer* b;
  1765. b = malloc(sizeof(*b));
  1766. b->length = 0;
  1767. b->alloc = initSize;
  1768. b->buf = malloc(initSize * sizeof(*b->buf));
  1769. b->buf[0] = 0;
  1770. return b;
  1771. }
  1772. void json_string_buffer_free(struct json_string_buffer* sb) {
  1773. free(sb->buf);
  1774. sb->buf = NULL;
  1775. sb->length = 0;
  1776. sb->alloc = 0;
  1777. }
  1778. static void sb_check(struct json_string_buffer* sb, size_t more) {
  1779. char* tmp;
  1780. if(sb->length + 1 + more > sb->alloc) {
  1781. tmp = realloc(sb->buf, sb->alloc * 2); // BUG: guarantee sufficient size
  1782. if(tmp) {
  1783. sb->buf = tmp;
  1784. sb->alloc *= 2;
  1785. }
  1786. else {
  1787. //fprintf(stderr, "c_json: Memory allocation failed\n");
  1788. }
  1789. }
  1790. }
  1791. // checks size and concatenates string
  1792. // does not check for newlines re: line_len
  1793. static void sb_cat(struct json_string_buffer* sb, char* str) {
  1794. // TODO: optimize
  1795. size_t len = strlen(str);
  1796. sb_check(sb, len);
  1797. memcpy(sb->buf + sb->length, str, len);
  1798. // strcat(sb->buf, str);
  1799. sb->length += len;
  1800. sb->line_len += len;
  1801. }
  1802. // checks size and concatenates a single char
  1803. static void sb_putc(struct json_string_buffer* sb, int c) {
  1804. // TODO: optimize
  1805. sb_check(sb, 1);
  1806. sb->buf[sb->length] = c;
  1807. // sb->buf[sb->length + 1] = 0;
  1808. sb->length++;
  1809. sb->line_len = (c == '\n') ? 0 : (sb->line_len + 1);
  1810. }
  1811. static char* sb_tail_check(struct json_string_buffer* sb, int more) {
  1812. sb_check(sb, more);
  1813. return sb->buf + sb->length;
  1814. }
  1815. #define sb_tail_catf(sb, fmt, ...) \
  1816. do { \
  1817. size_t _len = snprintf(NULL, 0, fmt, __VA_ARGS__); \
  1818. snprintf(sb_tail_check(sb, _len), _len + 1, fmt, __VA_ARGS__); \
  1819. sb->length += _len; \
  1820. sb->line_len += _len; \
  1821. } while(0);
  1822. void json_stringify(struct json_write_context* ctx, struct json_value* v) {
  1823. struct json_string_buffer* sb = ctx->sb;
  1824. char* float_format = ctx->fmt.floatFormat ? ctx->fmt.floatFormat : "%f";
  1825. char qc;
  1826. if(!v) {
  1827. //fprintf(stderr, "NULL value passed to %s()\n", __func__);
  1828. return;
  1829. }
  1830. switch(v->type) {
  1831. case JSON_TYPE_UNDEFINED:
  1832. sb_cat(sb, "undefined");
  1833. break;
  1834. case JSON_TYPE_NULL:
  1835. sb_cat(sb, "null");
  1836. break;
  1837. case JSON_TYPE_INT: // 2
  1838. sb_tail_catf(sb, "%ld", v->n); // TODO: handle bases, formats
  1839. break;
  1840. case JSON_TYPE_DOUBLE:
  1841. sb_tail_catf(sb, float_format, v->d); // TODO: handle infinity, nan, etc
  1842. break;
  1843. case JSON_TYPE_STRING:
  1844. qc = ctx->fmt.useSingleQuotes ? '\'' : '"';
  1845. sb_putc(sb, qc);
  1846. sb_cat_escaped(ctx, v->s);
  1847. sb_putc(sb, qc);
  1848. break;
  1849. case JSON_TYPE_OBJ:
  1850. json_obj_to_string(ctx, v);
  1851. break;
  1852. case JSON_TYPE_ARRAY: // 6
  1853. json_arr_to_string(ctx, v);
  1854. break;
  1855. case JSON_TYPE_COMMENT_SINGLE: // TODO: handle linebreaks in the comment string
  1856. sb_tail_catf(sb, "//%s\n", v->s);
  1857. break;
  1858. case JSON_TYPE_COMMENT_MULTI: // TODO: clean "*/" out of the comment string
  1859. sb_tail_catf(sb, "/* %s */\n", v->s);
  1860. break;
  1861. // default:
  1862. // fprintf(stderr, "c_json: unknown type in json_value_to_string\n");
  1863. }
  1864. }
  1865. static void ctx_indent(struct json_write_context* ctx) {
  1866. int i = 0;
  1867. int len = ctx->fmt.indentAmt * ctx->depth;
  1868. char* c = sb_tail_check(ctx->sb, len);
  1869. for(i = 0; i < len; i++) {
  1870. c[i] = ctx->fmt.indentChar;
  1871. }
  1872. c[len] = 0;
  1873. ctx->sb->length += len;
  1874. ctx->sb->line_len += len;
  1875. }
  1876. static int line_too_long(struct json_write_context* ctx) {
  1877. if(ctx->fmt.maxLineLength < 0) return 0;
  1878. if(ctx->sb->line_len <= ctx->fmt.maxLineLength) return 0;
  1879. return 1;
  1880. }
  1881. static void sb_cat_escaped(struct json_write_context* ctx, char* str) {
  1882. struct json_string_buffer* sb = ctx->sb;
  1883. char qc = ctx->fmt.useSingleQuotes ? '\'' : '"';
  1884. while(*str) {
  1885. char c = *str;
  1886. if(c == qc) sb_putc(sb, '\\');
  1887. sb_putc(sb, c);
  1888. str++;
  1889. }
  1890. }
  1891. static void json_arr_to_string(struct json_write_context* ctx, struct json_value* arr) {
  1892. struct json_link* n;
  1893. struct json_string_buffer* sb = ctx->sb;
  1894. int multiline = arr->len >= (size_t)ctx->fmt.minArraySzExpand;
  1895. sb_putc(sb, '[');
  1896. if(multiline) sb_putc(sb, '\n');
  1897. ctx->depth++;
  1898. n = arr->arr.head;
  1899. while(n) {
  1900. if(multiline) ctx_indent(ctx);
  1901. json_stringify(ctx, n->v);
  1902. n = n->next;
  1903. if(n) {
  1904. sb_putc(sb, ',');
  1905. if(!multiline) sb_putc(sb, ' ');
  1906. }
  1907. else if(ctx->fmt.trailingComma && multiline) sb_putc(sb, ',');
  1908. if(multiline) sb_putc(sb, '\n');
  1909. if(line_too_long(ctx)) {
  1910. sb_putc(sb, '\n');
  1911. ctx_indent(ctx);
  1912. }
  1913. }
  1914. ctx->depth--;
  1915. if(multiline) ctx_indent(ctx);
  1916. sb_putc(sb, ']');
  1917. }
  1918. static int key_must_have_quotes(char* key) {
  1919. if(isdigit(key[0])) return 1;
  1920. return strlen(key) != strspn(key, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$");
  1921. }
  1922. static void json_obj_to_string(struct json_write_context* ctx, struct json_value* obj) {
  1923. size_t i;
  1924. struct json_obj_field* f;
  1925. struct json_string_buffer* sb = ctx->sb;
  1926. int multiline = obj->len >= (size_t)ctx->fmt.minObjSzExpand;
  1927. int noquotes = ctx->fmt.noQuoteKeys;
  1928. char quoteChar = ctx->fmt.useSingleQuotes ? '\'' : '"';
  1929. sb_putc(sb, '{');
  1930. if(multiline) sb_putc(sb, '\n');
  1931. ctx->depth++;
  1932. size_t n = obj->len;
  1933. for(i = 0; i < obj->obj.alloc_size; i++) {
  1934. f = &obj->obj.buckets[i];
  1935. if(f->key == NULL) continue;
  1936. if(multiline) ctx_indent(ctx);
  1937. int needquotes = key_must_have_quotes(f->key);
  1938. if(!noquotes || needquotes) {
  1939. sb_putc(sb, quoteChar);
  1940. sb_cat_escaped(ctx, f->key);
  1941. sb_putc(sb, quoteChar);
  1942. }
  1943. else sb_cat(sb, f->key);
  1944. sb_putc(sb, ':');
  1945. if(ctx->fmt.objColonSpace) sb_putc(sb, ' ');
  1946. json_stringify(ctx, f->value);
  1947. if(n-- > 1) {
  1948. sb_putc(sb, ',');
  1949. if(!multiline) sb_putc(sb, ' ');
  1950. }
  1951. else if(ctx->fmt.trailingComma && multiline) sb_putc(sb, ',');
  1952. if(multiline) sb_putc(sb, '\n');
  1953. if(line_too_long(ctx)) {
  1954. sb_putc(sb, '\n');
  1955. ctx_indent(ctx);
  1956. }
  1957. }
  1958. ctx->depth--;
  1959. if(multiline) ctx_indent(ctx);
  1960. sb_putc(sb, '}');
  1961. }
  1962. struct json_value* json_new_str(char* s) {
  1963. return json_new_strn(s, strlen(s));
  1964. }
  1965. struct json_value* json_new_strn(char* s, size_t len) {
  1966. struct json_value* v;
  1967. v = malloc(sizeof(*v));
  1968. v->type = JSON_TYPE_STRING;
  1969. v->s = strndup(s, len);
  1970. v->len = len;
  1971. v->base = 0;
  1972. return v;
  1973. }
  1974. struct json_value* json_new_double(double d) {
  1975. struct json_value* v;
  1976. v = malloc(sizeof(*v));
  1977. v->type = JSON_TYPE_DOUBLE;
  1978. v->d = d;
  1979. v->len = 0;
  1980. v->base = 0;
  1981. return v;
  1982. }
  1983. struct json_value* json_new_int(int64_t n) {
  1984. struct json_value* v;
  1985. v = malloc(sizeof(*v));
  1986. v->type = JSON_TYPE_INT;
  1987. v->n = n;
  1988. v->len = 0;
  1989. v->base = 0;
  1990. return v;
  1991. }
  1992. struct json_value* json_new_array() {
  1993. struct json_value* v;
  1994. v = malloc(sizeof(*v));
  1995. v->type = JSON_TYPE_ARRAY;
  1996. v->arr.head = NULL;
  1997. v->arr.tail = NULL;
  1998. v->len = 0;
  1999. v->base = 0;
  2000. return v;
  2001. }
  2002. struct json_value* json_new_object(size_t initial_alloc_size) {
  2003. struct json_value* obj = malloc(sizeof(*obj));
  2004. obj->type = JSON_TYPE_OBJ;
  2005. obj->len = 0;
  2006. obj->base = 0;
  2007. obj->obj.alloc_size = initial_alloc_size;
  2008. obj->obj.buckets = calloc(1, sizeof(*obj->obj.buckets) * obj->obj.alloc_size);
  2009. if(!obj->obj.buckets) {
  2010. free(obj);
  2011. return NULL;
  2012. }
  2013. return obj;
  2014. }
  2015. struct json_value* json_new_null() {
  2016. struct json_value* v;
  2017. v = malloc(sizeof(*v));
  2018. v->type = JSON_TYPE_NULL;
  2019. return v;
  2020. }
  2021. struct json_value* json_new_undefined() {
  2022. struct json_value* v;
  2023. v = malloc(sizeof(*v));
  2024. v->type = JSON_TYPE_UNDEFINED;
  2025. return v;
  2026. }
  2027. struct json_value* json_new_true() {
  2028. struct json_value* v;
  2029. v = malloc(sizeof(*v));
  2030. v->type = JSON_TYPE_BOOL;
  2031. v->n = 1; // true
  2032. v->len = 0;
  2033. v->base = 0;
  2034. return v;
  2035. }
  2036. struct json_value* json_new_false() {
  2037. struct json_value* v;
  2038. v = malloc(sizeof(*v));
  2039. v->type = JSON_TYPE_BOOL;
  2040. v->n = 0; // false
  2041. v->len = 0;
  2042. v->base = 0;
  2043. return v;
  2044. }