lua_cjson.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /* CJSON - JSON support for Lua
  2. *
  3. * Copyright (c) 2010-2011 Mark Pulford <[email protected]>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /* Caveats:
  25. * - JSON "null" values are represented as lightuserdata since Lua
  26. * tables cannot contain "nil". Compare with cjson.null.
  27. * - Invalid UTF-8 characters are not detected and will be passed
  28. * untouched. If required, UTF-8 error checking should be done
  29. * outside this library.
  30. * - Javascript comments are not part of the JSON spec, and are not
  31. * currently supported.
  32. *
  33. * Note: Decoding is slower than encoding. Lua spends significant
  34. * time (30%) managing tables when parsing JSON since it is
  35. * difficult to know object/array sizes ahead of time.
  36. */
  37. #include <assert.h>
  38. #include <string.h>
  39. #include <math.h>
  40. #include <lua.h>
  41. #include <lauxlib.h>
  42. #include "strbuf.h"
  43. #ifdef MISSING_ISINF
  44. #define isinf(x) (!isnan(x) && isnan((x) - (x)))
  45. #endif
  46. #define DEFAULT_SPARSE_CONVERT 0
  47. #define DEFAULT_SPARSE_RATIO 2
  48. #define DEFAULT_SPARSE_SAFE 10
  49. #define DEFAULT_MAX_DEPTH 20
  50. #define DEFAULT_ENCODE_REFUSE_BADNUM 1
  51. #define DEFAULT_DECODE_REFUSE_BADNUM 0
  52. #define DEFAULT_ENCODE_KEEP_BUFFER 1
  53. typedef enum {
  54. T_OBJ_BEGIN,
  55. T_OBJ_END,
  56. T_ARR_BEGIN,
  57. T_ARR_END,
  58. T_STRING,
  59. T_NUMBER,
  60. T_BOOLEAN,
  61. T_NULL,
  62. T_COLON,
  63. T_COMMA,
  64. T_END,
  65. T_WHITESPACE,
  66. T_ERROR,
  67. T_UNKNOWN
  68. } json_token_type_t;
  69. static const char *json_token_type_name[] = {
  70. "T_OBJ_BEGIN",
  71. "T_OBJ_END",
  72. "T_ARR_BEGIN",
  73. "T_ARR_END",
  74. "T_STRING",
  75. "T_NUMBER",
  76. "T_BOOLEAN",
  77. "T_NULL",
  78. "T_COLON",
  79. "T_COMMA",
  80. "T_END",
  81. "T_WHITESPACE",
  82. "T_ERROR",
  83. "T_UNKNOWN",
  84. NULL
  85. };
  86. typedef struct {
  87. json_token_type_t ch2token[256];
  88. char escape2char[256]; /* Decoding */
  89. #if 0
  90. char escapes[35][8]; /* Pre-generated escape string buffer */
  91. char *char2escape[256]; /* Encoding */
  92. #endif
  93. strbuf_t encode_buf;
  94. char number_fmt[8]; /* "%.XXg\0" */
  95. int current_depth;
  96. int encode_sparse_convert;
  97. int encode_sparse_ratio;
  98. int encode_sparse_safe;
  99. int encode_max_depth;
  100. int encode_refuse_badnum;
  101. int decode_refuse_badnum;
  102. int encode_keep_buffer;
  103. int encode_number_precision;
  104. } json_config_t;
  105. typedef struct {
  106. const char *data;
  107. int index;
  108. strbuf_t *tmp; /* Temporary storage for strings */
  109. json_config_t *cfg;
  110. } json_parse_t;
  111. typedef struct {
  112. json_token_type_t type;
  113. int index;
  114. union {
  115. const char *string;
  116. double number;
  117. int boolean;
  118. } value;
  119. int string_len;
  120. } json_token_t;
  121. static const char *char2escape[256] = {
  122. "\\u0000", "\\u0001", "\\u0002", "\\u0003",
  123. "\\u0004", "\\u0005", "\\u0006", "\\u0007",
  124. "\\b", "\\t", "\\n", "\\u000b",
  125. "\\f", "\\r", "\\u000e", "\\u000f",
  126. "\\u0010", "\\u0011", "\\u0012", "\\u0013",
  127. "\\u0014", "\\u0015", "\\u0016", "\\u0017",
  128. "\\u0018", "\\u0019", "\\u001a", "\\u001b",
  129. "\\u001c", "\\u001d", "\\u001e", "\\u001f",
  130. NULL, NULL, "\\\"", NULL, NULL, NULL, NULL, NULL,
  131. NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\/",
  132. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  133. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  134. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  135. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  136. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  137. NULL, NULL, NULL, NULL, "\\\\", NULL, NULL, NULL,
  138. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  139. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  140. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  141. NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\u007f",
  142. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  143. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  144. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  145. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  146. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  147. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  148. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  149. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  150. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  151. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  152. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  153. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  154. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  155. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  156. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  157. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  158. };
  159. static int json_config_key;
  160. /* ===== CONFIGURATION ===== */
  161. static json_config_t *json_fetch_config(lua_State *l)
  162. {
  163. json_config_t *cfg;
  164. lua_pushlightuserdata(l, &json_config_key);
  165. lua_gettable(l, LUA_REGISTRYINDEX);
  166. cfg = lua_touserdata(l, -1);
  167. if (!cfg)
  168. luaL_error(l, "BUG: Unable to fetch CJSON configuration");
  169. lua_pop(l, 1);
  170. return cfg;
  171. }
  172. static void json_verify_arg_count(lua_State *l, int args)
  173. {
  174. luaL_argcheck(l, lua_gettop(l) <= args, args + 1,
  175. "found too many arguments");
  176. }
  177. /* Configures handling of extremely sparse arrays:
  178. * convert: Convert extremely sparse arrays into objects? Otherwise error.
  179. * ratio: 0: always allow sparse; 1: never allow sparse; >1: use ratio
  180. * safe: Always use an array when the max index <= safe */
  181. static int json_cfg_encode_sparse_array(lua_State *l)
  182. {
  183. json_config_t *cfg;
  184. int val;
  185. json_verify_arg_count(l, 3);
  186. cfg = json_fetch_config(l);
  187. switch (lua_gettop(l)) {
  188. case 3:
  189. val = luaL_checkinteger(l, 3);
  190. luaL_argcheck(l, val >= 0, 3, "expected integer >= 0");
  191. cfg->encode_sparse_safe = val;
  192. case 2:
  193. val = luaL_checkinteger(l, 2);
  194. luaL_argcheck(l, val >= 0, 2, "expected integer >= 0");
  195. cfg->encode_sparse_ratio = val;
  196. case 1:
  197. luaL_argcheck(l, lua_isboolean(l, 1), 1, "expected boolean");
  198. cfg->encode_sparse_convert = lua_toboolean(l, 1);
  199. }
  200. lua_pushboolean(l, cfg->encode_sparse_convert);
  201. lua_pushinteger(l, cfg->encode_sparse_ratio);
  202. lua_pushinteger(l, cfg->encode_sparse_safe);
  203. return 3;
  204. }
  205. /* Configures the maximum number of nested arrays/objects allowed when
  206. * encoding */
  207. static int json_cfg_encode_max_depth(lua_State *l)
  208. {
  209. json_config_t *cfg;
  210. int depth;
  211. json_verify_arg_count(l, 1);
  212. cfg = json_fetch_config(l);
  213. if (lua_gettop(l)) {
  214. depth = luaL_checkinteger(l, 1);
  215. luaL_argcheck(l, depth > 0, 1, "expected positive integer");
  216. cfg->encode_max_depth = depth;
  217. }
  218. lua_pushinteger(l, cfg->encode_max_depth);
  219. return 1;
  220. }
  221. static void json_set_number_precision(json_config_t *cfg, int prec)
  222. {
  223. cfg->encode_number_precision = prec;
  224. sprintf(cfg->number_fmt, "%%.%dg", prec);
  225. }
  226. /* Configures number precision when converting doubles to text */
  227. static int json_cfg_encode_number_precision(lua_State *l)
  228. {
  229. json_config_t *cfg;
  230. int precision;
  231. json_verify_arg_count(l, 1);
  232. cfg = json_fetch_config(l);
  233. if (lua_gettop(l)) {
  234. precision = luaL_checkinteger(l, 1);
  235. luaL_argcheck(l, 1 <= precision && precision <= 14, 1,
  236. "expected integer between 1 and 14");
  237. json_set_number_precision(cfg, precision);
  238. }
  239. lua_pushinteger(l, cfg->encode_number_precision);
  240. return 1;
  241. }
  242. /* Configures JSON encoding buffer persistence */
  243. static int json_cfg_encode_keep_buffer(lua_State *l)
  244. {
  245. json_config_t *cfg;
  246. json_verify_arg_count(l, 1);
  247. cfg = json_fetch_config(l);
  248. if (lua_gettop(l)) {
  249. luaL_checktype(l, 1, LUA_TBOOLEAN);
  250. cfg->encode_keep_buffer = lua_toboolean(l, 1);
  251. }
  252. lua_pushboolean(l, cfg->encode_keep_buffer);
  253. return 1;
  254. }
  255. /* On argument: decode enum and set config variables
  256. * **options must point to a NULL terminated array of 4 enums
  257. * Returns: current enum value */
  258. static void json_enum_option(lua_State *l, const char **options,
  259. int *opt1, int *opt2)
  260. {
  261. int setting;
  262. if (lua_gettop(l)) {
  263. if (lua_isboolean(l, 1))
  264. setting = lua_toboolean(l, 1) * 3;
  265. else
  266. setting = luaL_checkoption(l, 1, NULL, options);
  267. *opt1 = setting & 1 ? 1 : 0;
  268. *opt2 = setting & 2 ? 1 : 0;
  269. } else {
  270. setting = *opt1 | (*opt2 << 1);
  271. }
  272. if (setting)
  273. lua_pushstring(l, options[setting]);
  274. else
  275. lua_pushboolean(l, 0);
  276. }
  277. /* When enabled, rejects: NaN, Infinity, hexidecimal numbers */
  278. static int json_cfg_refuse_invalid_numbers(lua_State *l)
  279. {
  280. static const char *options_enc_dec[] = { "none", "encode", "decode",
  281. "both", NULL };
  282. json_config_t *cfg;
  283. json_verify_arg_count(l, 1);
  284. cfg = json_fetch_config(l);
  285. json_enum_option(l, options_enc_dec,
  286. &cfg->encode_refuse_badnum,
  287. &cfg->decode_refuse_badnum);
  288. return 1;
  289. }
  290. static int json_destroy_config(lua_State *l)
  291. {
  292. json_config_t *cfg;
  293. cfg = lua_touserdata(l, 1);
  294. if (cfg)
  295. strbuf_free(&cfg->encode_buf);
  296. cfg = NULL;
  297. return 0;
  298. }
  299. static void json_create_config(lua_State *l)
  300. {
  301. json_config_t *cfg;
  302. int i;
  303. cfg = lua_newuserdata(l, sizeof(*cfg));
  304. /* Create GC method to clean up strbuf */
  305. lua_newtable(l);
  306. lua_pushcfunction(l, json_destroy_config);
  307. lua_setfield(l, -2, "__gc");
  308. lua_setmetatable(l, -2);
  309. strbuf_init(&cfg->encode_buf, 0);
  310. cfg->encode_sparse_convert = DEFAULT_SPARSE_CONVERT;
  311. cfg->encode_sparse_ratio = DEFAULT_SPARSE_RATIO;
  312. cfg->encode_sparse_safe = DEFAULT_SPARSE_SAFE;
  313. cfg->encode_max_depth = DEFAULT_MAX_DEPTH;
  314. cfg->encode_refuse_badnum = DEFAULT_ENCODE_REFUSE_BADNUM;
  315. cfg->decode_refuse_badnum = DEFAULT_DECODE_REFUSE_BADNUM;
  316. cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER;
  317. json_set_number_precision(cfg, 14);
  318. /* Decoding init */
  319. /* Tag all characters as an error */
  320. for (i = 0; i < 256; i++)
  321. cfg->ch2token[i] = T_ERROR;
  322. /* Set tokens that require no further processing */
  323. cfg->ch2token['{'] = T_OBJ_BEGIN;
  324. cfg->ch2token['}'] = T_OBJ_END;
  325. cfg->ch2token['['] = T_ARR_BEGIN;
  326. cfg->ch2token[']'] = T_ARR_END;
  327. cfg->ch2token[','] = T_COMMA;
  328. cfg->ch2token[':'] = T_COLON;
  329. cfg->ch2token['\0'] = T_END;
  330. cfg->ch2token[' '] = T_WHITESPACE;
  331. cfg->ch2token['\t'] = T_WHITESPACE;
  332. cfg->ch2token['\n'] = T_WHITESPACE;
  333. cfg->ch2token['\r'] = T_WHITESPACE;
  334. /* Update characters that require further processing */
  335. cfg->ch2token['f'] = T_UNKNOWN; /* false? */
  336. cfg->ch2token['i'] = T_UNKNOWN; /* inf, ininity? */
  337. cfg->ch2token['I'] = T_UNKNOWN;
  338. cfg->ch2token['n'] = T_UNKNOWN; /* null, nan? */
  339. cfg->ch2token['N'] = T_UNKNOWN;
  340. cfg->ch2token['t'] = T_UNKNOWN; /* true? */
  341. cfg->ch2token['"'] = T_UNKNOWN; /* string? */
  342. cfg->ch2token['+'] = T_UNKNOWN; /* number? */
  343. cfg->ch2token['-'] = T_UNKNOWN;
  344. for (i = 0; i < 10; i++)
  345. cfg->ch2token['0' + i] = T_UNKNOWN;
  346. /* Lookup table for parsing escape characters */
  347. for (i = 0; i < 256; i++)
  348. cfg->escape2char[i] = 0; /* String error */
  349. cfg->escape2char['"'] = '"';
  350. cfg->escape2char['\\'] = '\\';
  351. cfg->escape2char['/'] = '/';
  352. cfg->escape2char['b'] = '\b';
  353. cfg->escape2char['t'] = '\t';
  354. cfg->escape2char['n'] = '\n';
  355. cfg->escape2char['f'] = '\f';
  356. cfg->escape2char['r'] = '\r';
  357. cfg->escape2char['u'] = 'u'; /* Unicode parsing required */
  358. #if 0
  359. /* Initialise separate storage for pre-generated escape codes.
  360. * Escapes 0-31 map directly, 34, 92, 127 follow afterwards to
  361. * save memory. */
  362. for (i = 0 ; i < 32; i++)
  363. sprintf(cfg->escapes[i], "\\u%04x", i);
  364. strcpy(cfg->escapes[8], "\b"); /* Override simpler escapes */
  365. strcpy(cfg->escapes[9], "\t");
  366. strcpy(cfg->escapes[10], "\n");
  367. strcpy(cfg->escapes[12], "\f");
  368. strcpy(cfg->escapes[13], "\r");
  369. strcpy(cfg->escapes[32], "\\\""); /* chr(34) */
  370. strcpy(cfg->escapes[33], "\\\\"); /* chr(92) */
  371. sprintf(cfg->escapes[34], "\\u%04x", 127); /* char(127) */
  372. /* Initialise encoding escape lookup table */
  373. for (i = 0; i < 32; i++)
  374. cfg->char2escape[i] = cfg->escapes[i];
  375. for (i = 32; i < 256; i++)
  376. cfg->char2escape[i] = NULL;
  377. cfg->char2escape[34] = cfg->escapes[32];
  378. cfg->char2escape[92] = cfg->escapes[33];
  379. cfg->char2escape[127] = cfg->escapes[34];
  380. #endif
  381. }
  382. /* ===== ENCODING ===== */
  383. static void json_encode_exception(lua_State *l, json_config_t *cfg, int lindex,
  384. const char *reason)
  385. {
  386. if (!cfg->encode_keep_buffer)
  387. strbuf_free(&cfg->encode_buf);
  388. luaL_error(l, "Cannot serialise %s: %s",
  389. lua_typename(l, lua_type(l, lindex)), reason);
  390. }
  391. /* json_append_string args:
  392. * - lua_State
  393. * - JSON strbuf
  394. * - String (Lua stack index)
  395. *
  396. * Returns nothing. Doesn't remove string from Lua stack */
  397. static void json_append_string(lua_State *l, strbuf_t *json, int lindex)
  398. {
  399. const char *escstr;
  400. int i;
  401. const char *str;
  402. size_t len;
  403. str = lua_tolstring(l, lindex, &len);
  404. /* Worst case is len * 6 (all unicode escapes).
  405. * This buffer is reused constantly for small strings
  406. * If there are any excess pages, they won't be hit anyway.
  407. * This gains ~5% speedup. */
  408. strbuf_ensure_empty_length(json, len * 6 + 2);
  409. strbuf_append_char_unsafe(json, '\"');
  410. for (i = 0; i < len; i++) {
  411. escstr = char2escape[(unsigned char)str[i]];
  412. if (escstr)
  413. strbuf_append_string(json, escstr);
  414. else
  415. strbuf_append_char_unsafe(json, str[i]);
  416. }
  417. strbuf_append_char_unsafe(json, '\"');
  418. }
  419. /* Find the size of the array on the top of the Lua stack
  420. * -1 object (not a pure array)
  421. * >=0 elements in array
  422. */
  423. static int lua_array_length(lua_State *l, json_config_t *cfg)
  424. {
  425. double k;
  426. int max;
  427. int items;
  428. max = 0;
  429. items = 0;
  430. lua_pushnil(l);
  431. /* table, startkey */
  432. while (lua_next(l, -2) != 0) {
  433. /* table, key, value */
  434. if (lua_type(l, -2) == LUA_TNUMBER &&
  435. (k = lua_tonumber(l, -2))) {
  436. /* Integer >= 1 ? */
  437. if (floor(k) == k && k >= 1) {
  438. if (k > max)
  439. max = k;
  440. items++;
  441. lua_pop(l, 1);
  442. continue;
  443. }
  444. }
  445. /* Must not be an array (non integer key) */
  446. lua_pop(l, 2);
  447. return -1;
  448. }
  449. /* Encode excessively sparse arrays as objects (if enabled) */
  450. if (cfg->encode_sparse_ratio > 0 &&
  451. max > items * cfg->encode_sparse_ratio &&
  452. max > cfg->encode_sparse_safe) {
  453. if (!cfg->encode_sparse_convert)
  454. json_encode_exception(l, cfg, -1, "excessively sparse array");
  455. return -1;
  456. }
  457. return max;
  458. }
  459. static void json_encode_descend(lua_State *l, json_config_t *cfg)
  460. {
  461. cfg->current_depth++;
  462. if (cfg->current_depth > cfg->encode_max_depth) {
  463. if (!cfg->encode_keep_buffer)
  464. strbuf_free(&cfg->encode_buf);
  465. luaL_error(l, "Cannot serialise, excessive nesting (%d)",
  466. cfg->current_depth);
  467. }
  468. }
  469. static void json_append_data(lua_State *l, json_config_t *cfg, strbuf_t *json);
  470. /* json_append_array args:
  471. * - lua_State
  472. * - JSON strbuf
  473. * - Size of passwd Lua array (top of stack) */
  474. static void json_append_array(lua_State *l, json_config_t *cfg, strbuf_t *json,
  475. int array_length)
  476. {
  477. int comma, i;
  478. json_encode_descend(l, cfg);
  479. strbuf_append_char(json, '[');
  480. comma = 0;
  481. for (i = 1; i <= array_length; i++) {
  482. if (comma)
  483. strbuf_append_char(json, ',');
  484. else
  485. comma = 1;
  486. lua_rawgeti(l, -1, i);
  487. json_append_data(l, cfg, json);
  488. lua_pop(l, 1);
  489. }
  490. strbuf_append_char(json, ']');
  491. cfg->current_depth--;
  492. }
  493. static void json_append_number(lua_State *l, strbuf_t *json, int index,
  494. json_config_t *cfg)
  495. {
  496. double num = lua_tonumber(l, index);
  497. if (cfg->encode_refuse_badnum && (isinf(num) || isnan(num)))
  498. json_encode_exception(l, cfg, index, "must not be NaN or Inf");
  499. /* Lowest double printed with %.14g is 21 characters long:
  500. * -1.7976931348623e+308
  501. *
  502. * Use 32 to include the \0, and a few extra just in case..
  503. */
  504. strbuf_append_fmt(json, 32, cfg->number_fmt, num);
  505. }
  506. static void json_append_object(lua_State *l, json_config_t *cfg,
  507. strbuf_t *json)
  508. {
  509. int comma, keytype;
  510. json_encode_descend(l, cfg);
  511. /* Object */
  512. strbuf_append_char(json, '{');
  513. lua_pushnil(l);
  514. /* table, startkey */
  515. comma = 0;
  516. while (lua_next(l, -2) != 0) {
  517. if (comma)
  518. strbuf_append_char(json, ',');
  519. else
  520. comma = 1;
  521. /* table, key, value */
  522. keytype = lua_type(l, -2);
  523. if (keytype == LUA_TNUMBER) {
  524. strbuf_append_char(json, '"');
  525. json_append_number(l, json, -2, cfg);
  526. strbuf_append_mem(json, "\":", 2);
  527. } else if (keytype == LUA_TSTRING) {
  528. json_append_string(l, json, -2);
  529. strbuf_append_char(json, ':');
  530. } else {
  531. json_encode_exception(l, cfg, -2,
  532. "table key must be a number or string");
  533. /* never returns */
  534. }
  535. /* table, key, value */
  536. json_append_data(l, cfg, json);
  537. lua_pop(l, 1);
  538. /* table, key */
  539. }
  540. strbuf_append_char(json, '}');
  541. cfg->current_depth--;
  542. }
  543. /* Serialise Lua data into JSON string. */
  544. static void json_append_data(lua_State *l, json_config_t *cfg, strbuf_t *json)
  545. {
  546. int len;
  547. switch (lua_type(l, -1)) {
  548. case LUA_TSTRING:
  549. json_append_string(l, json, -1);
  550. break;
  551. case LUA_TNUMBER:
  552. json_append_number(l, json, -1, cfg);
  553. break;
  554. case LUA_TBOOLEAN:
  555. if (lua_toboolean(l, -1))
  556. strbuf_append_mem(json, "true", 4);
  557. else
  558. strbuf_append_mem(json, "false", 5);
  559. break;
  560. case LUA_TTABLE:
  561. len = lua_array_length(l, cfg);
  562. if (len > 0)
  563. json_append_array(l, cfg, json, len);
  564. else
  565. json_append_object(l, cfg, json);
  566. break;
  567. case LUA_TNIL:
  568. strbuf_append_mem(json, "null", 4);
  569. break;
  570. case LUA_TLIGHTUSERDATA:
  571. if (lua_touserdata(l, -1) == NULL) {
  572. strbuf_append_mem(json, "null", 4);
  573. break;
  574. }
  575. default:
  576. /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD,
  577. * and LUA_TLIGHTUSERDATA) cannot be serialised */
  578. json_encode_exception(l, cfg, -1, "type not supported");
  579. /* never returns */
  580. }
  581. }
  582. static int json_encode(lua_State *l)
  583. {
  584. json_config_t *cfg;
  585. char *json;
  586. int len;
  587. /* Can't use json_verify_arg_count() since we need to ensure
  588. * there is only 1 argument */
  589. luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
  590. cfg = json_fetch_config(l);
  591. cfg->current_depth = 0;
  592. /* Reset the persistent buffer if it exists.
  593. * Otherwise allocate a new buffer. */
  594. if (strbuf_allocated(&cfg->encode_buf))
  595. strbuf_reset(&cfg->encode_buf);
  596. else
  597. strbuf_init(&cfg->encode_buf, 0);
  598. json_append_data(l, cfg, &cfg->encode_buf);
  599. json = strbuf_string(&cfg->encode_buf, &len);
  600. lua_pushlstring(l, json, len);
  601. if (!cfg->encode_keep_buffer)
  602. strbuf_free(&cfg->encode_buf);
  603. return 1;
  604. }
  605. /* ===== DECODING ===== */
  606. static void json_process_value(lua_State *l, json_parse_t *json,
  607. json_token_t *token);
  608. static int hexdigit2int(char hex)
  609. {
  610. if ('0' <= hex && hex <= '9')
  611. return hex - '0';
  612. /* Force lowercase */
  613. hex |= 0x20;
  614. if ('a' <= hex && hex <= 'f')
  615. return 10 + hex - 'a';
  616. return -1;
  617. }
  618. static int decode_hex4(const char *hex)
  619. {
  620. int digit[4];
  621. int i;
  622. /* Convert ASCII hex digit to numeric digit
  623. * Note: this returns an error for invalid hex digits, including
  624. * NULL */
  625. for (i = 0; i < 4; i++) {
  626. digit[i] = hexdigit2int(hex[i]);
  627. if (digit[i] < 0) {
  628. return -1;
  629. }
  630. }
  631. return (digit[0] << 12) +
  632. (digit[1] << 8) +
  633. (digit[2] << 4) +
  634. digit[3];
  635. }
  636. /* Converts a Unicode codepoint to UTF-8.
  637. * Returns UTF-8 string length, and up to 4 bytes in *utf8 */
  638. static int codepoint_to_utf8(char *utf8, int codepoint)
  639. {
  640. /* 0xxxxxxx */
  641. if (codepoint <= 0x7F) {
  642. utf8[0] = codepoint;
  643. return 1;
  644. }
  645. /* 110xxxxx 10xxxxxx */
  646. if (codepoint <= 0x7FF) {
  647. utf8[0] = (codepoint >> 6) | 0xC0;
  648. utf8[1] = (codepoint & 0x3F) | 0x80;
  649. return 2;
  650. }
  651. /* 1110xxxx 10xxxxxx 10xxxxxx */
  652. if (codepoint <= 0xFFFF) {
  653. utf8[0] = (codepoint >> 12) | 0xE0;
  654. utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80;
  655. utf8[2] = (codepoint & 0x3F) | 0x80;
  656. return 3;
  657. }
  658. /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
  659. if (codepoint <= 0x1FFFFF) {
  660. utf8[0] = (codepoint >> 18) | 0xF0;
  661. utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80;
  662. utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80;
  663. utf8[3] = (codepoint & 0x3F) | 0x80;
  664. return 4;
  665. }
  666. return 0;
  667. }
  668. /* Called when index pointing to beginning of UTF-16 code escape: \uXXXX
  669. * \u is guaranteed to exist, but the remaining hex characters may be
  670. * missing.
  671. * Translate to UTF-8 and append to temporary token string.
  672. * Must advance index to the next character to be processed.
  673. * Returns: 0 success
  674. * -1 error
  675. */
  676. static int json_append_unicode_escape(json_parse_t *json)
  677. {
  678. char utf8[4]; /* Surrogate pairs require 4 UTF-8 bytes */
  679. int codepoint;
  680. int surrogate_low;
  681. int len;
  682. int escape_len = 6;
  683. /* Fetch UTF-16 code unit */
  684. codepoint = decode_hex4(&json->data[json->index + 2]);
  685. if (codepoint < 0)
  686. return -1;
  687. /* UTF-16 surrogate pairs take the following 2 byte form:
  688. * 11011 x yyyyyyyyyy
  689. * When x = 0: y is the high 10 bits of the codepoint
  690. * x = 1: y is the low 10 bits of the codepoint
  691. *
  692. * Check for a surrogate pair (high or low) */
  693. if ((codepoint & 0xF800) == 0xD800) {
  694. /* Error if the 1st surrogate is not high */
  695. if (codepoint & 0x400)
  696. return -1;
  697. /* Ensure the next code is a unicode escape */
  698. if (json->data[json->index + escape_len] != '\\' ||
  699. json->data[json->index + escape_len + 1] != 'u') {
  700. return -1;
  701. }
  702. /* Fetch the next codepoint */
  703. surrogate_low = decode_hex4(&json->data[json->index + 2 + escape_len]);
  704. if (surrogate_low < 0)
  705. return -1;
  706. /* Error if the 2nd code is not a low surrogate */
  707. if ((surrogate_low & 0xFC00) != 0xDC00)
  708. return -1;
  709. /* Calculate Unicode codepoint */
  710. codepoint = (codepoint & 0x3FF) << 10;
  711. surrogate_low &= 0x3FF;
  712. codepoint = (codepoint | surrogate_low) + 0x10000;
  713. escape_len = 12;
  714. }
  715. /* Convert codepoint to UTF-8 */
  716. len = codepoint_to_utf8(utf8, codepoint);
  717. if (!len)
  718. return -1;
  719. /* Append bytes and advance parse index */
  720. strbuf_append_mem_unsafe(json->tmp, utf8, len);
  721. json->index += escape_len;
  722. return 0;
  723. }
  724. static void json_set_token_error(json_token_t *token, json_parse_t *json,
  725. const char *errtype)
  726. {
  727. token->type = T_ERROR;
  728. token->index = json->index;
  729. token->value.string = errtype;
  730. }
  731. static void json_next_string_token(json_parse_t *json, json_token_t *token)
  732. {
  733. char *escape2char = json->cfg->escape2char;
  734. char ch;
  735. /* Caller must ensure a string is next */
  736. assert(json->data[json->index] == '"');
  737. /* Skip " */
  738. json->index++;
  739. /* json->tmp is the temporary strbuf used to accumulate the
  740. * decoded string value. */
  741. strbuf_reset(json->tmp);
  742. while ((ch = json->data[json->index]) != '"') {
  743. if (!ch) {
  744. /* Premature end of the string */
  745. json_set_token_error(token, json, "unexpected end of string");
  746. return;
  747. }
  748. /* Handle escapes */
  749. if (ch == '\\') {
  750. /* Fetch escape character */
  751. ch = json->data[json->index + 1];
  752. /* Translate escape code and append to tmp string */
  753. ch = escape2char[(unsigned char)ch];
  754. if (ch == 'u') {
  755. if (json_append_unicode_escape(json) == 0)
  756. continue;
  757. json_set_token_error(token, json,
  758. "invalid unicode escape code");
  759. return;
  760. }
  761. if (!ch) {
  762. json_set_token_error(token, json, "invalid escape code");
  763. return;
  764. }
  765. /* Skip '\' */
  766. json->index++;
  767. }
  768. /* Append normal character or translated single character
  769. * Unicode escapes are handled above */
  770. strbuf_append_char_unsafe(json->tmp, ch);
  771. json->index++;
  772. }
  773. json->index++; /* Eat final quote (") */
  774. strbuf_ensure_null(json->tmp);
  775. token->type = T_STRING;
  776. token->value.string = strbuf_string(json->tmp, &token->string_len);
  777. }
  778. /* JSON numbers should take the following form:
  779. * -?(0|[1-9]|[1-9][0-9]+)(.[0-9]+)?([eE][-+]?[0-9]+)?
  780. *
  781. * json_next_number_token() uses strtod() which allows other forms:
  782. * - numbers starting with '+'
  783. * - NaN, -NaN, infinity, -infinity
  784. * - hexidecimal numbers
  785. * - numbers with leading zeros
  786. *
  787. * json_is_invalid_number() detects "numbers" which may pass strtod()'s
  788. * error checking, but should not be allowed with strict JSON.
  789. *
  790. * json_is_invalid_number() may pass numbers which cause strtod()
  791. * to generate an error.
  792. */
  793. static int json_is_invalid_number(json_parse_t *json)
  794. {
  795. int i = json->index;
  796. /* Reject numbers starting with + */
  797. if (json->data[i] == '+')
  798. return 1;
  799. /* Skip minus sign if it exists */
  800. if (json->data[i] == '-')
  801. i++;
  802. /* Reject numbers starting with 0x, or leading zeros */
  803. if (json->data[i] == '0') {
  804. int ch2 = json->data[i + 1];
  805. if ((ch2 | 0x20) == 'x' || /* Hex */
  806. ('0' <= ch2 && ch2 <= '9')) /* Leading zero */
  807. return 1;
  808. return 0;
  809. } else if (json->data[i] <= '9') {
  810. return 0; /* Ordinary number */
  811. }
  812. /* Reject inf/nan */
  813. if (!strncasecmp(&json->data[i], "inf", 3))
  814. return 1;
  815. if (!strncasecmp(&json->data[i], "nan", 3))
  816. return 1;
  817. /* Pass all other numbers which may still be invalid, but
  818. * strtod() will catch them. */
  819. return 0;
  820. }
  821. static void json_next_number_token(json_parse_t *json, json_token_t *token)
  822. {
  823. const char *startptr;
  824. char *endptr;
  825. token->type = T_NUMBER;
  826. startptr = &json->data[json->index];
  827. token->value.number = strtod(&json->data[json->index], &endptr);
  828. if (startptr == endptr)
  829. json_set_token_error(token, json, "invalid number");
  830. else
  831. json->index += endptr - startptr; /* Skip the processed number */
  832. return;
  833. }
  834. /* Fills in the token struct.
  835. * T_STRING will return a pointer to the json_parse_t temporary string
  836. * T_ERROR will leave the json->index pointer at the error.
  837. */
  838. static void json_next_token(json_parse_t *json, json_token_t *token)
  839. {
  840. json_token_type_t *ch2token = json->cfg->ch2token;
  841. int ch;
  842. /* Eat whitespace. FIXME: UGLY */
  843. token->type = ch2token[(unsigned char)json->data[json->index]];
  844. while (token->type == T_WHITESPACE)
  845. token->type = ch2token[(unsigned char)json->data[++json->index]];
  846. token->index = json->index;
  847. /* Don't advance the pointer for an error or the end */
  848. if (token->type == T_ERROR) {
  849. json_set_token_error(token, json, "invalid token");
  850. return;
  851. }
  852. if (token->type == T_END) {
  853. return;
  854. }
  855. /* Found a known single character token, advance index and return */
  856. if (token->type != T_UNKNOWN) {
  857. json->index++;
  858. return;
  859. }
  860. /* Process characters which triggered T_UNKNOWN */
  861. ch = json->data[json->index];
  862. /* Must use strncmp() to match the front of the JSON string.
  863. * JSON identifier must be lowercase.
  864. * When strict_numbers if disabled, either case is allowed for
  865. * Infinity/NaN (since we are no longer following the spec..) */
  866. if (ch == '"') {
  867. json_next_string_token(json, token);
  868. return;
  869. } else if (ch == '-' || ('0' <= ch && ch <= '9')) {
  870. if (json->cfg->decode_refuse_badnum && json_is_invalid_number(json)) {
  871. json_set_token_error(token, json, "invalid number");
  872. return;
  873. }
  874. json_next_number_token(json, token);
  875. return;
  876. } else if (!strncmp(&json->data[json->index], "true", 4)) {
  877. token->type = T_BOOLEAN;
  878. token->value.boolean = 1;
  879. json->index += 4;
  880. return;
  881. } else if (!strncmp(&json->data[json->index], "false", 5)) {
  882. token->type = T_BOOLEAN;
  883. token->value.boolean = 0;
  884. json->index += 5;
  885. return;
  886. } else if (!strncmp(&json->data[json->index], "null", 4)) {
  887. token->type = T_NULL;
  888. json->index += 4;
  889. return;
  890. } else if (!json->cfg->decode_refuse_badnum &&
  891. json_is_invalid_number(json)) {
  892. /* When refuse_badnum is disabled, only attempt to process
  893. * numbers we know are invalid JSON (Inf, NaN, hex)
  894. * This is required to generate an appropriate token error,
  895. * otherwise all bad tokens will register as "invalid number"
  896. */
  897. json_next_number_token(json, token);
  898. return;
  899. }
  900. /* Token starts with t/f/n but isn't recognised above. */
  901. json_set_token_error(token, json, "invalid token");
  902. }
  903. /* This function does not return.
  904. * DO NOT CALL WITH DYNAMIC MEMORY ALLOCATED.
  905. * The only supported exception is the temporary parser string
  906. * json->tmp struct.
  907. * json and token should exist on the stack somewhere.
  908. * luaL_error() will long_jmp and release the stack */
  909. static void json_throw_parse_error(lua_State *l, json_parse_t *json,
  910. const char *exp, json_token_t *token)
  911. {
  912. const char *found;
  913. strbuf_free(json->tmp);
  914. if (token->type == T_ERROR)
  915. found = token->value.string;
  916. else
  917. found = json_token_type_name[token->type];
  918. /* Note: token->index is 0 based, display starting from 1 */
  919. luaL_error(l, "Expected %s but found %s at character %d",
  920. exp, found, token->index + 1);
  921. }
  922. static void json_decode_checkstack(lua_State *l, json_parse_t *json, int n)
  923. {
  924. if (lua_checkstack(l, n))
  925. return;
  926. strbuf_free(json->tmp);
  927. luaL_error(l, "Too many nested data structures");
  928. }
  929. static void json_parse_object_context(lua_State *l, json_parse_t *json)
  930. {
  931. json_token_t token;
  932. /* 3 slots required:
  933. * .., table, key, value */
  934. json_decode_checkstack(l, json, 3);
  935. lua_newtable(l);
  936. json_next_token(json, &token);
  937. /* Handle empty objects */
  938. if (token.type == T_OBJ_END) {
  939. return;
  940. }
  941. while (1) {
  942. if (token.type != T_STRING)
  943. json_throw_parse_error(l, json, "object key string", &token);
  944. /* Push key */
  945. lua_pushlstring(l, token.value.string, token.string_len);
  946. json_next_token(json, &token);
  947. if (token.type != T_COLON)
  948. json_throw_parse_error(l, json, "colon", &token);
  949. /* Fetch value */
  950. json_next_token(json, &token);
  951. json_process_value(l, json, &token);
  952. /* Set key = value */
  953. lua_rawset(l, -3);
  954. json_next_token(json, &token);
  955. if (token.type == T_OBJ_END)
  956. return;
  957. if (token.type != T_COMMA)
  958. json_throw_parse_error(l, json, "comma or object end", &token);
  959. json_next_token(json, &token);
  960. }
  961. }
  962. /* Handle the array context */
  963. static void json_parse_array_context(lua_State *l, json_parse_t *json)
  964. {
  965. json_token_t token;
  966. int i;
  967. /* 2 slots required:
  968. * .., table, value */
  969. json_decode_checkstack(l, json, 2);
  970. lua_newtable(l);
  971. json_next_token(json, &token);
  972. /* Handle empty arrays */
  973. if (token.type == T_ARR_END)
  974. return;
  975. for (i = 1; ; i++) {
  976. json_process_value(l, json, &token);
  977. lua_rawseti(l, -2, i); /* arr[i] = value */
  978. json_next_token(json, &token);
  979. if (token.type == T_ARR_END)
  980. return;
  981. if (token.type != T_COMMA)
  982. json_throw_parse_error(l, json, "comma or array end", &token);
  983. json_next_token(json, &token);
  984. }
  985. }
  986. /* Handle the "value" context */
  987. static void json_process_value(lua_State *l, json_parse_t *json,
  988. json_token_t *token)
  989. {
  990. switch (token->type) {
  991. case T_STRING:
  992. lua_pushlstring(l, token->value.string, token->string_len);
  993. break;;
  994. case T_NUMBER:
  995. lua_pushnumber(l, token->value.number);
  996. break;;
  997. case T_BOOLEAN:
  998. lua_pushboolean(l, token->value.boolean);
  999. break;;
  1000. case T_OBJ_BEGIN:
  1001. json_parse_object_context(l, json);
  1002. break;;
  1003. case T_ARR_BEGIN:
  1004. json_parse_array_context(l, json);
  1005. break;;
  1006. case T_NULL:
  1007. /* In Lua, setting "t[k] = nil" will delete k from the table.
  1008. * Hence a NULL pointer lightuserdata object is used instead */
  1009. lua_pushlightuserdata(l, NULL);
  1010. break;;
  1011. default:
  1012. json_throw_parse_error(l, json, "value", token);
  1013. }
  1014. }
  1015. /* json_text must be null terminated string */
  1016. static void lua_json_decode(lua_State *l, const char *json_text, int json_len)
  1017. {
  1018. json_parse_t json;
  1019. json_token_t token;
  1020. json.cfg = json_fetch_config(l);
  1021. json.data = json_text;
  1022. json.index = 0;
  1023. /* Ensure the temporary buffer can hold the entire string.
  1024. * This means we no longer need to do length checks since the decoded
  1025. * string must be smaller than the entire json string */
  1026. json.tmp = strbuf_new(json_len);
  1027. json_next_token(&json, &token);
  1028. json_process_value(l, &json, &token);
  1029. /* Ensure there is no more input left */
  1030. json_next_token(&json, &token);
  1031. if (token.type != T_END)
  1032. json_throw_parse_error(l, &json, "the end", &token);
  1033. strbuf_free(json.tmp);
  1034. }
  1035. static int json_decode(lua_State *l)
  1036. {
  1037. const char *json;
  1038. size_t len;
  1039. json_verify_arg_count(l, 1);
  1040. json = luaL_checklstring(l, 1, &len);
  1041. /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3)
  1042. *
  1043. * CJSON can support any simple data type, hence only the first
  1044. * character is guaranteed to be ASCII (at worst: '"'). This is
  1045. * still enough to detect whether the wrong encoding is in use. */
  1046. if (len >= 2 && (!json[0] || !json[1]))
  1047. luaL_error(l, "JSON parser does not support UTF-16 or UTF-32");
  1048. lua_json_decode(l, json, len);
  1049. return 1;
  1050. }
  1051. /* ===== INITIALISATION ===== */
  1052. int luaopen_cjson(lua_State *l)
  1053. {
  1054. luaL_Reg reg[] = {
  1055. { "encode", json_encode },
  1056. { "decode", json_decode },
  1057. { "encode_sparse_array", json_cfg_encode_sparse_array },
  1058. { "encode_max_depth", json_cfg_encode_max_depth },
  1059. { "encode_number_precision", json_cfg_encode_number_precision },
  1060. { "encode_keep_buffer", json_cfg_encode_keep_buffer },
  1061. { "refuse_invalid_numbers", json_cfg_refuse_invalid_numbers },
  1062. { NULL, NULL }
  1063. };
  1064. /* Use json_fetch_config as a pointer.
  1065. * It's faster than using a config string, and more unique */
  1066. lua_pushlightuserdata(l, &json_config_key);
  1067. json_create_config(l);
  1068. lua_settable(l, LUA_REGISTRYINDEX);
  1069. luaL_register(l, "cjson", reg);
  1070. /* Set cjson.null */
  1071. lua_pushlightuserdata(l, NULL);
  1072. lua_setfield(l, -2, "null");
  1073. /* Set cjson.version */
  1074. lua_pushliteral(l, VERSION);
  1075. lua_setfield(l, -2, "version");
  1076. /* Return cjson table */
  1077. return 1;
  1078. }
  1079. /* vi:ai et sw=4 ts=4:
  1080. */