lua_cjson.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /*
  2. @module json
  3. @summary json生成和解析库
  4. @version 1.0
  5. @date 2020.02.18
  6. @demo json
  7. @tag LUAT_USE_CJSON
  8. */
  9. /* Lua CJSON - JSON support for Lua
  10. *
  11. * Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au>
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining
  14. * a copy of this software and associated documentation files (the
  15. * "Software"), to deal in the Software without restriction, including
  16. * without limitation the rights to use, copy, modify, merge, publish,
  17. * distribute, sublicense, and/or sell copies of the Software, and to
  18. * permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be
  22. * included in all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  27. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  28. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  29. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  30. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. /* Caveats:
  33. * - JSON "null" values are represented as lightuserdata since Lua
  34. * tables cannot contain "nil". Compare with cjson.null.
  35. * - Invalid UTF-8 characters are not detected and will be passed
  36. * untouched. If required, UTF-8 error checking should be done
  37. * outside this library.
  38. * - Javascript comments are not part of the JSON spec, and are not
  39. * currently supported.
  40. *
  41. * Note: Decoding is slower than encoding. Lua spends significant
  42. * time (30%) managing tables when parsing JSON since it is
  43. * difficult to know object/array sizes ahead of time.
  44. */
  45. #include <assert.h>
  46. #include <string.h>
  47. #include <math.h>
  48. #include <limits.h>
  49. #include <lua.h>
  50. #include <lauxlib.h>
  51. #include "strbuf.h"
  52. #include "fpconv.h"
  53. #include "printf.h"
  54. #define MIN_OPT_LEVEL 2
  55. #define LUAT_LOG_TAG "json"
  56. #include "luat_log.h"
  57. //#include <lrodefs.h>
  58. //#include <auxmods.h>
  59. #ifdef WIN32
  60. #define snprintf _snprintf
  61. typedef unsigned char u_char;
  62. /*
  63. * This array is designed for mapping upper and lower case letter
  64. * together for a case independent comparison. The mappings are
  65. * based upon ascii character sequences.
  66. */
  67. static const u_char charmap[] = {
  68. '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
  69. '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
  70. '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
  71. '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
  72. '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
  73. '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
  74. '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
  75. '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
  76. '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  77. '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  78. '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  79. '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
  80. '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  81. '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  82. '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  83. '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
  84. '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  85. '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
  86. '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
  87. '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
  88. '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
  89. '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
  90. '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  91. '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  92. '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
  93. '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
  94. '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
  95. '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
  96. '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  97. '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  98. '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  99. '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
  100. };
  101. #ifndef LUA_USE_WINDOWS
  102. int
  103. strcasecmp(const char *s1, const char *s2)
  104. {
  105. const u_char *cm = charmap;
  106. const u_char *us1 = (const u_char *)s1;
  107. const u_char *us2 = (const u_char *)s2;
  108. while (cm[*us1] == cm[*us2++])
  109. if (*us1++ == '\0')
  110. return (0);
  111. return (cm[*us1] - cm[*--us2]);
  112. }
  113. int
  114. strncasecmp(const char *s1, const char *s2, size_t n)
  115. {
  116. if (n != 0) {
  117. const u_char *cm = charmap;
  118. const u_char *us1 = (const u_char *)s1;
  119. const u_char *us2 = (const u_char *)s2;
  120. do {
  121. if (cm[*us1] != cm[*us2++])
  122. return (cm[*us1] - cm[*--us2]);
  123. if (*us1++ == '\0')
  124. break;
  125. } while (--n != 0);
  126. }
  127. return (0);
  128. }
  129. #endif
  130. #endif
  131. #define ENABLE_CJSON_GLOBAL
  132. #define MISSING_ISINF
  133. #ifndef CJSON_MODNAME
  134. #define CJSON_MODNAME "json"
  135. #endif
  136. #ifndef CJSON_VERSION
  137. #define CJSON_VERSION "2.1.0"
  138. #endif
  139. #ifndef assert
  140. #define assert(x)
  141. #endif
  142. #if !defined(isnan)
  143. #define isnan(x) (0)
  144. #endif
  145. /* Workaround for Solaris platforms missing isinf() */
  146. #if !defined(isinf) && (defined(USE_INTERNAL_ISINF) || defined(MISSING_ISINF))
  147. #define isinf(x) (!isnan(x) && isnan((x) - (x)))
  148. #endif
  149. #define DEFAULT_SPARSE_CONVERT 0
  150. #define DEFAULT_SPARSE_RATIO 2
  151. #define DEFAULT_SPARSE_SAFE 10
  152. #define DEFAULT_ENCODE_MAX_DEPTH 1000
  153. #define DEFAULT_DECODE_MAX_DEPTH 1000
  154. #define DEFAULT_ENCODE_INVALID_NUMBERS 0
  155. #define DEFAULT_DECODE_INVALID_NUMBERS 1
  156. #define DEFAULT_ENCODE_KEEP_BUFFER 0
  157. #define DEFAULT_ENCODE_NUMBER_PRECISION 7
  158. #ifdef DISABLE_INVALID_NUMBERS
  159. #undef DEFAULT_DECODE_INVALID_NUMBERS
  160. #define DEFAULT_DECODE_INVALID_NUMBERS 0
  161. #endif
  162. typedef enum {
  163. T_OBJ_BEGIN,
  164. T_OBJ_END,
  165. T_ARR_BEGIN,
  166. T_ARR_END,
  167. T_STRING,
  168. T_NUMBER,
  169. T_INTEGER,
  170. T_BOOLEAN,
  171. T_NULL,
  172. T_COLON,
  173. T_COMMA,
  174. T_END,
  175. T_WHITESPACE,
  176. T_ERROR,
  177. T_UNKNOWN
  178. } json_token_type_t;
  179. static const char *json_token_type_name[] = {
  180. "T_OBJ_BEGIN",
  181. "T_OBJ_END",
  182. "T_ARR_BEGIN",
  183. "T_ARR_END",
  184. "T_STRING",
  185. "T_NUMBER",
  186. "T_INTEGER",
  187. "T_BOOLEAN",
  188. "T_NULL",
  189. "T_COLON",
  190. "T_COMMA",
  191. "T_END",
  192. "T_WHITESPACE",
  193. "T_ERROR",
  194. "T_UNKNOWN",
  195. NULL
  196. };
  197. // typedef struct {
  198. // //json_token_type_t ch2token[256];
  199. // //char escape2char[256]; /* Decoding */
  200. // /* encode_buf is only allocated and used when
  201. // * encode_keep_buffer is set */
  202. // //strbuf_t encode_buf;
  203. // //int encode_sparse_convert;
  204. // //int encode_sparse_ratio;
  205. // //int encode_sparse_safe;
  206. // //int encode_max_depth;
  207. // //int encode_invalid_numbers; /* 2 => Encode as "null" */
  208. // //int encode_number_precision;
  209. // //int encode_keep_buffer;
  210. // //int decode_invalid_numbers;
  211. // //int decode_max_depth;
  212. // } json_config_t;
  213. typedef struct {
  214. const char *data;
  215. const char *ptr;
  216. strbuf_t *tmp; /* Temporary storage for strings */
  217. //json_config_t *cfg;
  218. int current_depth;
  219. } json_parse_t;
  220. typedef struct {
  221. json_token_type_t type;
  222. int index;
  223. union {
  224. const char *string;
  225. lua_Number number;
  226. lua_Integer lint;
  227. int boolean;
  228. } value;
  229. int string_len;
  230. } json_token_t;
  231. // static const char *char2escape[256] = {
  232. // "\\u0000", "\\u0001", "\\u0002", "\\u0003",
  233. // "\\u0004", "\\u0005", "\\u0006", "\\u0007",
  234. // "\\b", "\\t", "\\n", "\\u000b",
  235. // "\\f", "\\r", "\\u000e", "\\u000f",
  236. // "\\u0010", "\\u0011", "\\u0012", "\\u0013",
  237. // "\\u0014", "\\u0015", "\\u0016", "\\u0017",
  238. // "\\u0018", "\\u0019", "\\u001a", "\\u001b",
  239. // "\\u001c", "\\u001d", "\\u001e", "\\u001f",
  240. // NULL, NULL, "\\\"", NULL, NULL, NULL, NULL, NULL,
  241. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\/",
  242. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  243. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  244. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  245. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  246. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  247. // NULL, NULL, NULL, NULL, "\\\\", NULL, NULL, NULL,
  248. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  249. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  250. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  251. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, "\\u007f",
  252. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  253. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  254. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  255. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  256. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  257. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  258. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  259. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  260. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  261. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  262. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  263. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  264. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  265. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  266. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  267. // NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  268. // };
  269. static const char* char2escape(unsigned char c) {
  270. switch (c)
  271. {
  272. case 0x00: return "\\u0000";
  273. case 0x01: return "\\u0001";
  274. case 0x02: return "\\u0002";
  275. case 0x03: return "\\u0003";
  276. case 0x04: return "\\u0004";
  277. case 0x05: return "\\u0005";
  278. case 0x06: return "\\u0006";
  279. case 0x07: return "\\u0007";
  280. case 0x08: return "\\b";
  281. case 0x09: return "\\t";
  282. case 0x0a: return "\\b";
  283. case 0x0b: return "\\u000b";
  284. case 0x0c: return "\\f";
  285. case 0x0d: return "\\r";
  286. case 0x0e: return "\\u000e";
  287. case 0x0f: return "\\u000f";
  288. case 0x10: return "\\u0010";
  289. case 0x11: return "\\u0011";
  290. case 0x12: return "\\u0012";
  291. case 0x13: return "\\u0013";
  292. case 0x14: return "\\u0014";
  293. case 0x15: return "\\u0015";
  294. case 0x16: return "\\u0016";
  295. case 0x17: return "\\u0017";
  296. case 0x18: return "\\u0018";
  297. case 0x19: return "\\u0019";
  298. case 0x1a: return "\\u001a";
  299. case 0x1b: return "\\u001b";
  300. case 0x1c: return "\\u001c";
  301. case 0x1d: return "\\u001d";
  302. case 0x1e: return "\\u001e";
  303. case 0x1f: return "\\u001f";
  304. case '\"': return "\\\"";
  305. case '/' : return "\\/";
  306. case '\\': return "\\\\";
  307. case 0x7f: return "\\u007f";
  308. default:
  309. return NULL;
  310. }
  311. }
  312. //-----------------------------------------------
  313. static json_token_type_t ch2token(int ch) {
  314. switch (ch)
  315. {
  316. /* Set tokens that require no further processing */
  317. case '{': return T_OBJ_BEGIN;
  318. case '}': return T_OBJ_END;
  319. case '[': return T_ARR_BEGIN;
  320. case ']': return T_ARR_END;
  321. case ',': return T_COMMA;
  322. case ':': return T_COLON;
  323. case '\0': return T_END;
  324. case ' ': return T_WHITESPACE;
  325. case '\t': return T_WHITESPACE;
  326. case '\n': return T_WHITESPACE;
  327. case '\r': return T_WHITESPACE;
  328. /* Update characters that require further processing */
  329. case 'f': return T_UNKNOWN; /* false? */
  330. case 'i': return T_UNKNOWN; /* inf, ininity? */
  331. case 'I': return T_UNKNOWN;
  332. case 'n': return T_UNKNOWN; /* null, nan? */
  333. case 'N': return T_UNKNOWN;
  334. case 't': return T_UNKNOWN; /* true? */
  335. case '"': return T_UNKNOWN; /* string? */
  336. case '+': return T_UNKNOWN; /* number? */
  337. case '-': return T_UNKNOWN;
  338. case '0':
  339. case '1':
  340. case '2':
  341. case '3':
  342. case '4':
  343. case '5':
  344. case '6':
  345. case '7':
  346. case '8':
  347. case '9':
  348. return T_UNKNOWN;
  349. default:
  350. return T_ERROR;
  351. }
  352. }
  353. static char escape2char(unsigned char c) {
  354. switch (c) {
  355. case '"': return '"';
  356. case '\\':return '\\';
  357. case '/': return '/';
  358. case 'b': return '\b';
  359. case 't': return '\t';
  360. case 'n': return '\n';
  361. case 'f': return '\f';
  362. case 'r': return '\r';
  363. case 'u': return 'u'; /* Unicode parsing required */
  364. default:
  365. return 0;
  366. }
  367. }
  368. #if defined(DISABLE_INVALID_NUMBERS) && !defined(USE_INTERNAL_FPCONV)
  369. void json_verify_invalid_number_setting(lua_State *l, int *setting)
  370. {
  371. if (*setting == 1) {
  372. *setting = 0;
  373. luaL_error(l, "Infinity, NaN, and/or hexadecimal numbers are not supported.");
  374. }
  375. }
  376. #else
  377. #define json_verify_invalid_number_setting(l, s) do { } while(0)
  378. #endif
  379. /* ===== ENCODING ===== */
  380. static void json_encode_exception(lua_State *l, strbuf_t *json, int lindex,
  381. const char *reason)
  382. {
  383. //if (!cfg->encode_keep_buffer)
  384. strbuf_free(json);
  385. luaL_error(l, "Cannot serialise %s: %s",
  386. lua_typename(l, lua_type(l, lindex)), reason);
  387. }
  388. /* json_append_string args:
  389. * - lua_State
  390. * - JSON strbuf
  391. * - String (Lua stack index)
  392. *
  393. * Returns nothing. Doesn't remove string from Lua stack */
  394. static void json_append_string(lua_State *l, strbuf_t *json, int lindex)
  395. {
  396. const char *escstr;
  397. int i;
  398. const char *str;
  399. size_t len;
  400. str = lua_tolstring(l, lindex, &len);
  401. /* Worst case is len * 6 (all unicode escapes).
  402. * This buffer is reused constantly for small strings
  403. * If there are any excess pages, they won't be hit anyway.
  404. * This gains ~5% speedup. */
  405. strbuf_ensure_empty_length(json, len * 6 + 2);
  406. if (json->is_err)
  407. return;
  408. strbuf_append_char_unsafe(json, '\"');
  409. for (i = 0; i < len; i++) {
  410. escstr = char2escape((unsigned char)str[i]);
  411. if (escstr)
  412. strbuf_append_string(json, escstr);
  413. else
  414. strbuf_append_char_unsafe(json, str[i]);
  415. }
  416. strbuf_append_char_unsafe(json, '\"');
  417. }
  418. /* Find the size of the array on the top of the Lua stack
  419. * -1 object (not a pure array)
  420. * >=0 elements in array
  421. */
  422. static int lua_array_length(lua_State *l, strbuf_t *json)
  423. {
  424. double k;
  425. int max;
  426. int items;
  427. max = 0;
  428. items = 0;
  429. lua_pushnil(l);
  430. /* table, startkey */
  431. while (lua_next(l, -2) != 0) {
  432. /* table, key, value */
  433. if (lua_type(l, -2) == LUA_TNUMBER &&
  434. (k = lua_tonumber(l, -2))) {
  435. /* Integer >= 1 ? */
  436. if (floor(k) == k && k >= 1) {
  437. if (k > max)
  438. max = k;
  439. items++;
  440. lua_pop(l, 1);
  441. continue;
  442. }
  443. }
  444. /* Must not be an array (non integer key) */
  445. lua_pop(l, 2);
  446. return -1;
  447. }
  448. /* Encode excessively sparse arrays as objects (if enabled) */
  449. if (DEFAULT_SPARSE_RATIO > 0 &&
  450. max > items * DEFAULT_SPARSE_RATIO &&
  451. max > DEFAULT_SPARSE_SAFE) {
  452. if (!DEFAULT_SPARSE_CONVERT)
  453. json_encode_exception(l, json, -1, "excessively sparse array");
  454. return -1;
  455. }
  456. return max;
  457. }
  458. static void json_check_encode_depth(lua_State *l,
  459. int current_depth, strbuf_t *json)
  460. {
  461. /* Ensure there are enough slots free to traverse a table (key,
  462. * value) and push a string for a potential error message.
  463. *
  464. * Unlike "decode", the key and value are still on the stack when
  465. * lua_checkstack() is called. Hence an extra slot for luaL_error()
  466. * below is required just in case the next check to lua_checkstack()
  467. * fails.
  468. *
  469. * While this won't cause a crash due to the EXTRA_STACK reserve
  470. * slots, it would still be an improper use of the API. */
  471. if (current_depth <= DEFAULT_ENCODE_MAX_DEPTH && lua_checkstack(l, 3))
  472. return;
  473. //if (!cfg->encode_keep_buffer)
  474. strbuf_free(json);
  475. luaL_error(l, "Cannot serialise, excessive nesting (%d)",
  476. current_depth);
  477. }
  478. static void json_append_data(lua_State *l,
  479. int current_depth, strbuf_t *json);
  480. /* json_append_array args:
  481. * - lua_State
  482. * - JSON strbuf
  483. * - Size of passwd Lua array (top of stack) */
  484. static void json_append_array(lua_State *l, int current_depth,
  485. strbuf_t *json, int array_length)
  486. {
  487. int comma, i;
  488. strbuf_append_char(json, '[');
  489. comma = 0;
  490. for (i = 1; i <= array_length; i++) {
  491. if (comma)
  492. strbuf_append_char(json, ',');
  493. else
  494. comma = 1;
  495. lua_rawgeti(l, -1, i);
  496. json_append_data(l, current_depth, json);
  497. lua_pop(l, 1);
  498. }
  499. strbuf_append_char(json, ']');
  500. }
  501. typedef struct {
  502. char encode_number_precision_mode; /*浮点数转化模式 默认为'g',可配置为'f' */
  503. unsigned char encode_number_precision; /*浮点数转化精度 默认为7,可配置为0~14 */
  504. } json_easy_config_t;
  505. static json_easy_config_t json_easy_config = {'g',DEFAULT_ENCODE_NUMBER_PRECISION};
  506. static void json_append_number(lua_State *l,
  507. strbuf_t *json, int lindex)
  508. {
  509. double num = lua_tonumber(l, lindex);
  510. int len;
  511. //printf("json num = %ld, %f %ld", num, num, 100);
  512. if (DEFAULT_ENCODE_INVALID_NUMBERS == 0) {
  513. /* Prevent encoding invalid numbers */
  514. if (isinf(num) || isnan(num))
  515. json_encode_exception(l, json, lindex, "must not be NaN or Inf");
  516. } else if (DEFAULT_ENCODE_INVALID_NUMBERS == 1) {
  517. /* Encode invalid numbers, but handle "nan" separately
  518. * since some platforms may encode as "-nan". */
  519. if (isnan(num)) {
  520. strbuf_append_mem(json, "nan", 3);
  521. return;
  522. }
  523. } else {
  524. /* Encode invalid numbers as "null" */
  525. if (isinf(num) || isnan(num)) {
  526. strbuf_append_mem(json, "null", 4);
  527. return;
  528. }
  529. }
  530. strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE);
  531. if (json->is_err)
  532. return;
  533. if (lua_isinteger(l, lindex)) {
  534. len = snprintf_(strbuf_empty_ptr(json), FPCONV_G_FMT_BUFSIZE, "%ld", lua_tointeger(l, lindex));
  535. }
  536. else {
  537. //len = snprintf_("%f", strbuf_empty_ptr(json), FPCONV_G_FMT_BUFSIZE, lua_tonumber(l, lindex));
  538. if (json_easy_config.encode_number_precision_mode == 'f')
  539. {
  540. len = fpconv_f_fmt(strbuf_empty_ptr(json), num, json_easy_config.encode_number_precision);
  541. }
  542. else
  543. {
  544. len = fpconv_g_fmt(strbuf_empty_ptr(json), num, json_easy_config.encode_number_precision);
  545. }
  546. }
  547. //len = fpconv_g_fmt(strbuf_empty_ptr(json), num, DEFAULT_ENCODE_NUMBER_PRECISION);
  548. strbuf_extend_length(json, len);
  549. }
  550. static void json_append_object(lua_State *l,
  551. int current_depth, strbuf_t *json)
  552. {
  553. int comma, keytype;
  554. /* Object */
  555. strbuf_append_char(json, '{');
  556. lua_pushnil(l);
  557. /* table, startkey */
  558. comma = 0;
  559. while (lua_next(l, -2) != 0) {
  560. if (comma)
  561. strbuf_append_char(json, ',');
  562. else
  563. comma = 1;
  564. /* table, key, value */
  565. keytype = lua_type(l, -2);
  566. if (keytype == LUA_TNUMBER) {
  567. strbuf_append_char(json, '"');
  568. json_append_number(l, json, -2);
  569. strbuf_append_mem(json, "\":", 2);
  570. } else if (keytype == LUA_TSTRING) {
  571. json_append_string(l, json, -2);
  572. strbuf_append_char(json, ':');
  573. } else {
  574. json_encode_exception(l, json, -2,
  575. "table key must be a number or string");
  576. /* never returns */
  577. }
  578. /* table, key, value */
  579. json_append_data(l, current_depth, json);
  580. lua_pop(l, 1);
  581. /* table, key */
  582. }
  583. strbuf_append_char(json, '}');
  584. }
  585. /* Serialise Lua data into JSON string. */
  586. static void json_append_data(lua_State *l,
  587. int current_depth, strbuf_t *json)
  588. {
  589. int len;
  590. switch (lua_type(l, -1)) {
  591. case LUA_TSTRING:
  592. json_append_string(l, json, -1);
  593. break;
  594. case LUA_TNUMBER:
  595. json_append_number(l, json, -1);
  596. break;
  597. case LUA_TBOOLEAN:
  598. if (lua_toboolean(l, -1))
  599. strbuf_append_mem(json, "true", 4);
  600. else
  601. strbuf_append_mem(json, "false", 5);
  602. break;
  603. case LUA_TTABLE:
  604. current_depth++;
  605. json_check_encode_depth(l, current_depth, json);
  606. len = lua_array_length(l, json);
  607. if (len > 0)
  608. json_append_array(l, current_depth, json, len);
  609. else
  610. json_append_object(l, current_depth, json);
  611. break;
  612. case LUA_TNIL:
  613. strbuf_append_mem(json, "null", 4);
  614. break;
  615. case LUA_TLIGHTUSERDATA:
  616. if (lua_touserdata(l, -1) == NULL) {
  617. strbuf_append_mem(json, "null", 4);
  618. break;
  619. }
  620. default:
  621. /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD,
  622. * and LUA_TLIGHTUSERDATA) cannot be serialised */
  623. json_encode_exception(l, json, -1, "type not supported");
  624. /* never returns */
  625. }
  626. }
  627. static int json_encode(lua_State *l)
  628. {
  629. //json_config_t *cfg = json_fetch_config(l);
  630. strbuf_t local_encode_buf;
  631. strbuf_t *encode_buf;
  632. char *json;
  633. int len;
  634. int ret;
  635. luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
  636. encode_buf = &local_encode_buf;
  637. ret = strbuf_init(encode_buf, 0);
  638. if (ret) {
  639. LLOGE("json encode out of memory!!!");
  640. return 0;
  641. }
  642. json_append_data(l, 0, encode_buf);
  643. // check if err
  644. if (local_encode_buf.is_err) {
  645. LLOGE("json encode failed by memory less");
  646. lua_pushnil(l);
  647. }
  648. else {
  649. json = strbuf_string(encode_buf, &len);
  650. lua_pushlstring(l, json, len);
  651. }
  652. strbuf_free(encode_buf);
  653. return 1;
  654. }
  655. /* ===== DECODING ===== */
  656. static void json_process_value(lua_State *l, json_parse_t *json,
  657. json_token_t *token);
  658. static int hexdigit2int(char hex)
  659. {
  660. if ('0' <= hex && hex <= '9')
  661. return hex - '0';
  662. /* Force lowercase */
  663. hex |= 0x20;
  664. if ('a' <= hex && hex <= 'f')
  665. return 10 + hex - 'a';
  666. return -1;
  667. }
  668. static int decode_hex4(const char *hex)
  669. {
  670. int digit[4];
  671. int i;
  672. /* Convert ASCII hex digit to numeric digit
  673. * Note: this returns an error for invalid hex digits, including
  674. * NULL */
  675. for (i = 0; i < 4; i++) {
  676. digit[i] = hexdigit2int(hex[i]);
  677. if (digit[i] < 0) {
  678. return -1;
  679. }
  680. }
  681. return (digit[0] << 12) +
  682. (digit[1] << 8) +
  683. (digit[2] << 4) +
  684. digit[3];
  685. }
  686. /* Converts a Unicode codepoint to UTF-8.
  687. * Returns UTF-8 string length, and up to 4 bytes in *utf8 */
  688. static int codepoint_to_utf8(char *utf8, int codepoint)
  689. {
  690. /* 0xxxxxxx */
  691. if (codepoint <= 0x7F) {
  692. utf8[0] = codepoint;
  693. return 1;
  694. }
  695. /* 110xxxxx 10xxxxxx */
  696. if (codepoint <= 0x7FF) {
  697. utf8[0] = (codepoint >> 6) | 0xC0;
  698. utf8[1] = (codepoint & 0x3F) | 0x80;
  699. return 2;
  700. }
  701. /* 1110xxxx 10xxxxxx 10xxxxxx */
  702. if (codepoint <= 0xFFFF) {
  703. utf8[0] = (codepoint >> 12) | 0xE0;
  704. utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80;
  705. utf8[2] = (codepoint & 0x3F) | 0x80;
  706. return 3;
  707. }
  708. /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
  709. if (codepoint <= 0x1FFFFF) {
  710. utf8[0] = (codepoint >> 18) | 0xF0;
  711. utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80;
  712. utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80;
  713. utf8[3] = (codepoint & 0x3F) | 0x80;
  714. return 4;
  715. }
  716. return 0;
  717. }
  718. /* Called when index pointing to beginning of UTF-16 code escape: \uXXXX
  719. * \u is guaranteed to exist, but the remaining hex characters may be
  720. * missing.
  721. * Translate to UTF-8 and append to temporary token string.
  722. * Must advance index to the next character to be processed.
  723. * Returns: 0 success
  724. * -1 error
  725. */
  726. static int json_append_unicode_escape(json_parse_t *json)
  727. {
  728. char utf8[4]; /* Surrogate pairs require 4 UTF-8 bytes */
  729. int codepoint;
  730. int surrogate_low;
  731. int len;
  732. int escape_len = 6;
  733. /* Fetch UTF-16 code unit */
  734. codepoint = decode_hex4(json->ptr + 2);
  735. if (codepoint < 0)
  736. return -1;
  737. /* UTF-16 surrogate pairs take the following 2 byte form:
  738. * 11011 x yyyyyyyyyy
  739. * When x = 0: y is the high 10 bits of the codepoint
  740. * x = 1: y is the low 10 bits of the codepoint
  741. *
  742. * Check for a surrogate pair (high or low) */
  743. if ((codepoint & 0xF800) == 0xD800) {
  744. /* Error if the 1st surrogate is not high */
  745. if (codepoint & 0x400)
  746. return -1;
  747. /* Ensure the next code is a unicode escape */
  748. if (*(json->ptr + escape_len) != '\\' ||
  749. *(json->ptr + escape_len + 1) != 'u') {
  750. return -1;
  751. }
  752. /* Fetch the next codepoint */
  753. surrogate_low = decode_hex4(json->ptr + 2 + escape_len);
  754. if (surrogate_low < 0)
  755. return -1;
  756. /* Error if the 2nd code is not a low surrogate */
  757. if ((surrogate_low & 0xFC00) != 0xDC00)
  758. return -1;
  759. /* Calculate Unicode codepoint */
  760. codepoint = (codepoint & 0x3FF) << 10;
  761. surrogate_low &= 0x3FF;
  762. codepoint = (codepoint | surrogate_low) + 0x10000;
  763. escape_len = 12;
  764. }
  765. /* Convert codepoint to UTF-8 */
  766. len = codepoint_to_utf8(utf8, codepoint);
  767. if (!len)
  768. return -1;
  769. /* Append bytes and advance parse index */
  770. strbuf_append_mem_unsafe(json->tmp, utf8, len);
  771. json->ptr += escape_len;
  772. return 0;
  773. }
  774. static void json_set_token_error(json_token_t *token, json_parse_t *json,
  775. const char *errtype)
  776. {
  777. token->type = T_ERROR;
  778. token->index = json->ptr - json->data;
  779. token->value.string = errtype;
  780. }
  781. static void json_next_string_token(json_parse_t *json, json_token_t *token)
  782. {
  783. //char *escape2char = json->cfg->escape2char;
  784. char ch;
  785. /* Caller must ensure a string is next */
  786. assert(*json->ptr == '"');
  787. /* Skip " */
  788. json->ptr++;
  789. /* json->tmp is the temporary strbuf used to accumulate the
  790. * decoded string value.
  791. * json->tmp is sized to handle JSON containing only a string value.
  792. */
  793. strbuf_reset(json->tmp);
  794. while ((ch = *json->ptr) != '"') {
  795. if (!ch) {
  796. /* Premature end of the string */
  797. json_set_token_error(token, json, "unexpected end of string");
  798. return;
  799. }
  800. /* Handle escapes */
  801. if (ch == '\\') {
  802. /* Fetch escape character */
  803. ch = *(json->ptr + 1);
  804. /* Translate escape code and append to tmp string */
  805. ch = escape2char((unsigned char)ch);
  806. if (ch == 'u') {
  807. if (json_append_unicode_escape(json) == 0)
  808. continue;
  809. json_set_token_error(token, json,
  810. "invalid unicode escape code");
  811. return;
  812. }
  813. if (!ch) {
  814. json_set_token_error(token, json, "invalid escape code");
  815. return;
  816. }
  817. /* Skip '\' */
  818. json->ptr++;
  819. }
  820. /* Append normal character or translated single character
  821. * Unicode escapes are handled above */
  822. strbuf_append_char_unsafe(json->tmp, ch);
  823. json->ptr++;
  824. }
  825. json->ptr++; /* Eat final quote (") */
  826. strbuf_ensure_null(json->tmp);
  827. token->type = T_STRING;
  828. token->value.string = strbuf_string(json->tmp, &token->string_len);
  829. }
  830. /* JSON numbers should take the following form:
  831. * -?(0|[1-9]|[1-9][0-9]+)(.[0-9]+)?([eE][-+]?[0-9]+)?
  832. *
  833. * json_next_number_token() uses strtod() which allows other forms:
  834. * - numbers starting with '+'
  835. * - NaN, -NaN, infinity, -infinity
  836. * - hexadecimal numbers
  837. * - numbers with leading zeros
  838. *
  839. * json_is_invalid_number() detects "numbers" which may pass strtod()'s
  840. * error checking, but should not be allowed with strict JSON.
  841. *
  842. * json_is_invalid_number() may pass numbers which cause strtod()
  843. * to generate an error.
  844. */
  845. static int json_is_invalid_number(json_parse_t *json)
  846. {
  847. const char *p = json->ptr;
  848. /* Reject numbers starting with + */
  849. if (*p == '+')
  850. return 1;
  851. /* Skip minus sign if it exists */
  852. if (*p == '-')
  853. p++;
  854. /* Reject numbers starting with 0x, or leading zeros */
  855. if (*p == '0') {
  856. int ch2 = *(p + 1);
  857. if ((ch2 | 0x20) == 'x' || /* Hex */
  858. ('0' <= ch2 && ch2 <= '9')) /* Leading zero */
  859. return 1;
  860. return 0;
  861. } else if (*p <= '9') {
  862. return 0; /* Ordinary number */
  863. }
  864. /* Reject inf/nan */
  865. if (!strncasecmp(p, "inf", 3))
  866. return 1;
  867. if (!strncasecmp(p, "nan", 3))
  868. return 1;
  869. /* Pass all other numbers which may still be invalid, but
  870. * strtod() will catch them. */
  871. return 0;
  872. }
  873. static void json_next_number_token(json_parse_t *json, json_token_t *token)
  874. {
  875. char *endptr;
  876. unsigned len = 0;
  877. int int_val = 0;
  878. token->type = T_NUMBER;
  879. token->value.number = lua_str2number(json->ptr, &endptr);
  880. if (json->ptr == endptr)
  881. json_set_token_error(token, json, "invalid number");
  882. else {
  883. // 额外尝试是否为整型
  884. #ifndef LUAT_LIB_JSON_DECODE_INTEGER_DISABLE
  885. len = (endptr - json->ptr);
  886. if (len == 1 && (*json->ptr) == '0') {
  887. token->type = T_INTEGER;
  888. token->value.lint = 0;
  889. }
  890. else {
  891. for (size_t i = 0; i < len; i++)
  892. {
  893. if (json->ptr[i] == '.') {
  894. int_val = -1;
  895. break;
  896. }
  897. }
  898. if (int_val == 0) {
  899. int_val = 0;
  900. int_val = strtol(json->ptr, &endptr, 0);
  901. if (int_val != 0) {
  902. token->type = T_INTEGER;
  903. token->value.lint = int_val;
  904. }
  905. }
  906. }
  907. #endif
  908. json->ptr = endptr; /* Skip the processed number */
  909. }
  910. return;
  911. }
  912. /* Fills in the token struct.
  913. * T_STRING will return a pointer to the json_parse_t temporary string
  914. * T_ERROR will leave the json->ptr pointer at the error.
  915. */
  916. static void json_next_token(json_parse_t *json, json_token_t *token)
  917. {
  918. //const json_token_type_t *ch2token = json->cfg->ch2token;
  919. int ch;
  920. /* Eat whitespace. */
  921. while (1) {
  922. ch = (unsigned char)*(json->ptr);
  923. token->type = ch2token(ch);
  924. if (token->type != T_WHITESPACE)
  925. break;
  926. json->ptr++;
  927. }
  928. /* Store location of new token. Required when throwing errors
  929. * for unexpected tokens (syntax errors). */
  930. token->index = json->ptr - json->data;
  931. /* Don't advance the pointer for an error or the end */
  932. if (token->type == T_ERROR) {
  933. json_set_token_error(token, json, "invalid token");
  934. return;
  935. }
  936. if (token->type == T_END) {
  937. return;
  938. }
  939. /* Found a known single character token, advance index and return */
  940. if (token->type != T_UNKNOWN) {
  941. json->ptr++;
  942. return;
  943. }
  944. /* Process characters which triggered T_UNKNOWN
  945. *
  946. * Must use strncmp() to match the front of the JSON string.
  947. * JSON identifier must be lowercase.
  948. * When strict_numbers if disabled, either case is allowed for
  949. * Infinity/NaN (since we are no longer following the spec..) */
  950. if (ch == '"') {
  951. json_next_string_token(json, token);
  952. return;
  953. } else if (ch == '-' || ('0' <= ch && ch <= '9')) {
  954. if (!DEFAULT_ENCODE_INVALID_NUMBERS && json_is_invalid_number(json)) {
  955. json_set_token_error(token, json, "invalid number");
  956. return;
  957. }
  958. json_next_number_token(json, token);
  959. return;
  960. } else if (!strncmp(json->ptr, "true", 4)) {
  961. token->type = T_BOOLEAN;
  962. token->value.boolean = 1;
  963. json->ptr += 4;
  964. return;
  965. } else if (!strncmp(json->ptr, "false", 5)) {
  966. token->type = T_BOOLEAN;
  967. token->value.boolean = 0;
  968. json->ptr += 5;
  969. return;
  970. } else if (!strncmp(json->ptr, "null", 4)) {
  971. token->type = T_NULL;
  972. json->ptr += 4;
  973. return;
  974. } else if (DEFAULT_ENCODE_INVALID_NUMBERS &&
  975. json_is_invalid_number(json)) {
  976. /* When decode_invalid_numbers is enabled, only attempt to process
  977. * numbers we know are invalid JSON (Inf, NaN, hex)
  978. * This is required to generate an appropriate token error,
  979. * otherwise all bad tokens will register as "invalid number"
  980. */
  981. json_next_number_token(json, token);
  982. return;
  983. }
  984. /* Token starts with t/f/n but isn't recognised above. */
  985. json_set_token_error(token, json, "invalid token");
  986. }
  987. /* This function does not return.
  988. * DO NOT CALL WITH DYNAMIC MEMORY ALLOCATED.
  989. * The only supported exception is the temporary parser string
  990. * json->tmp struct.
  991. * json and token should exist on the stack somewhere.
  992. * luaL_error() will long_jmp and release the stack */
  993. static void json_throw_parse_error(lua_State *l, json_parse_t *json,
  994. const char *exp, json_token_t *token)
  995. {
  996. const char *found;
  997. strbuf_free(json->tmp);
  998. if (token->type == T_ERROR)
  999. found = token->value.string;
  1000. else
  1001. found = json_token_type_name[token->type];
  1002. /* Note: token->index is 0 based, display starting from 1 */
  1003. luaL_error(l, "Expected %s but found %s at character %d",
  1004. exp, found, token->index + 1);
  1005. }
  1006. static inline void json_decode_ascend(json_parse_t *json)
  1007. {
  1008. json->current_depth--;
  1009. }
  1010. static void json_decode_descend(lua_State *l, json_parse_t *json, int slots)
  1011. {
  1012. json->current_depth++;
  1013. if (json->current_depth <= DEFAULT_DECODE_MAX_DEPTH &&
  1014. lua_checkstack(l, slots)) {
  1015. return;
  1016. }
  1017. strbuf_free(json->tmp);
  1018. luaL_error(l, "Found too many nested data structures (%d) at character %d",
  1019. json->current_depth, json->ptr - json->data);
  1020. }
  1021. static void json_parse_object_context(lua_State *l, json_parse_t *json)
  1022. {
  1023. json_token_t token;
  1024. /* 3 slots required:
  1025. * .., table, key, value */
  1026. json_decode_descend(l, json, 3);
  1027. lua_newtable(l);
  1028. json_next_token(json, &token);
  1029. /* Handle empty objects */
  1030. if (token.type == T_OBJ_END) {
  1031. json_decode_ascend(json);
  1032. return;
  1033. }
  1034. while (1) {
  1035. if (token.type != T_STRING)
  1036. json_throw_parse_error(l, json, "object key string", &token);
  1037. /* Push key */
  1038. lua_pushlstring(l, token.value.string, token.string_len);
  1039. json_next_token(json, &token);
  1040. if (token.type != T_COLON)
  1041. json_throw_parse_error(l, json, "colon", &token);
  1042. /* Fetch value */
  1043. json_next_token(json, &token);
  1044. json_process_value(l, json, &token);
  1045. /* Set key = value */
  1046. lua_rawset(l, -3);
  1047. json_next_token(json, &token);
  1048. if (token.type == T_OBJ_END) {
  1049. json_decode_ascend(json);
  1050. return;
  1051. }
  1052. if (token.type != T_COMMA)
  1053. json_throw_parse_error(l, json, "comma or object end", &token);
  1054. json_next_token(json, &token);
  1055. }
  1056. }
  1057. /* Handle the array context */
  1058. static void json_parse_array_context(lua_State *l, json_parse_t *json)
  1059. {
  1060. json_token_t token;
  1061. int i;
  1062. /* 2 slots required:
  1063. * .., table, value */
  1064. json_decode_descend(l, json, 2);
  1065. lua_newtable(l);
  1066. json_next_token(json, &token);
  1067. /* Handle empty arrays */
  1068. if (token.type == T_ARR_END) {
  1069. json_decode_ascend(json);
  1070. return;
  1071. }
  1072. for (i = 1; ; i++) {
  1073. json_process_value(l, json, &token);
  1074. lua_rawseti(l, -2, i); /* arr[i] = value */
  1075. json_next_token(json, &token);
  1076. if (token.type == T_ARR_END) {
  1077. json_decode_ascend(json);
  1078. return;
  1079. }
  1080. if (token.type != T_COMMA)
  1081. json_throw_parse_error(l, json, "comma or array end", &token);
  1082. json_next_token(json, &token);
  1083. }
  1084. }
  1085. /* Handle the "value" context */
  1086. static void json_process_value(lua_State *l, json_parse_t *json,
  1087. json_token_t *token)
  1088. {
  1089. switch (token->type) {
  1090. case T_STRING:
  1091. lua_pushlstring(l, token->value.string, token->string_len);
  1092. break;;
  1093. case T_NUMBER:
  1094. lua_pushnumber(l, token->value.number);
  1095. break;;
  1096. case T_INTEGER:
  1097. lua_pushinteger(l, token->value.lint);
  1098. break;;
  1099. case T_BOOLEAN:
  1100. lua_pushboolean(l, token->value.boolean);
  1101. break;;
  1102. case T_OBJ_BEGIN:
  1103. json_parse_object_context(l, json);
  1104. break;;
  1105. case T_ARR_BEGIN:
  1106. json_parse_array_context(l, json);
  1107. break;;
  1108. case T_NULL:
  1109. /* In Lua, setting "t[k] = nil" will delete k from the table.
  1110. * Hence a NULL pointer lightuserdata object is used instead */
  1111. lua_pushlightuserdata(l, NULL);
  1112. break;;
  1113. default:
  1114. json_throw_parse_error(l, json, "value", token);
  1115. }
  1116. }
  1117. static int json_decode(lua_State *l)
  1118. {
  1119. json_parse_t json;
  1120. json_token_t token;
  1121. size_t json_len;
  1122. luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
  1123. //json.cfg = json_fetch_config(l);
  1124. json.data = luaL_checklstring(l, 1, &json_len);
  1125. json.current_depth = 0;
  1126. json.ptr = json.data;
  1127. /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3)
  1128. *
  1129. * CJSON can support any simple data type, hence only the first
  1130. * character is guaranteed to be ASCII (at worst: '"'). This is
  1131. * still enough to detect whether the wrong encoding is in use. */
  1132. if (json_len >= 2 && (!json.data[0] || !json.data[1]))
  1133. luaL_error(l, "JSON parser does not support UTF-16 or UTF-32");
  1134. /* Ensure the temporary buffer can hold the entire string.
  1135. * This means we no longer need to do length checks since the decoded
  1136. * string must be smaller than the entire json string */
  1137. json.tmp = strbuf_new(json_len);
  1138. if (json.tmp == NULL) {
  1139. LLOGE("json decode out of memory!");
  1140. return 0;
  1141. }
  1142. json_next_token(&json, &token);
  1143. json_process_value(l, &json, &token);
  1144. /* Ensure there is no more input left */
  1145. json_next_token(&json, &token);
  1146. if (token.type != T_END)
  1147. json_throw_parse_error(l, &json, "the end", &token);
  1148. strbuf_free(json.tmp);
  1149. return 1;
  1150. }
  1151. /* ===== INITIALISATION ===== */
  1152. #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
  1153. /* Compatibility for Lua 5.1.
  1154. *
  1155. * luaL_setfuncs() is used to create a module table where the functions have
  1156. * json_config_t as their first upvalue. Code borrowed from Lua 5.2 source. */
  1157. static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)
  1158. {
  1159. int i;
  1160. luaL_checkstack(l, nup, "too many upvalues");
  1161. for (; reg->name != NULL; reg++) { /* fill the table with given functions */
  1162. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  1163. lua_pushvalue(l, -nup);
  1164. lua_pushcclosure(l, reg->func, nup); /* closure with those upvalues */
  1165. lua_setfield(l, -(nup + 2), reg->name);
  1166. }
  1167. lua_pop(l, nup); /* remove upvalues */
  1168. }
  1169. #endif
  1170. /* Call target function in protected mode with all supplied args.
  1171. * Assumes target function only returns a single non-nil value.
  1172. * Convert and return thrown errors as: nil, "error message" */
  1173. static int json_protect_conversion(lua_State *l)
  1174. {
  1175. int err;
  1176. /* Deliberately throw an error for invalid arguments */
  1177. luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
  1178. /* pcall() the function stored as upvalue(1) */
  1179. lua_pushvalue(l, lua_upvalueindex(1));
  1180. lua_insert(l, 1);
  1181. err = lua_pcall(l, 1, 1, 0);
  1182. if (!err)
  1183. return 1;
  1184. if (err == LUA_ERRRUN) {
  1185. lua_pushnil(l);
  1186. lua_insert(l, -2);
  1187. return 2;
  1188. }
  1189. /* Since we are not using a custom error handler, the only remaining
  1190. * errors are memory related */
  1191. return luaL_error(l, "Memory allocation error in CJSON protected call");
  1192. }
  1193. /*
  1194. 将对象序列化为json字符串
  1195. @api json.encode(obj,t)
  1196. @obj 需要序列化的对象
  1197. @string 浮点数精度和模式,这项不存在的时候,为默认值"7g",数字只支持"0~14",模式只支持"f/g"
  1198. @return string 序列化后的json字符串, 失败的话返回nil
  1199. @return string 序列化失败的报错信息
  1200. @usage
  1201. json.encode(obj)-->浮点数用%.7g的方式转换为字符串
  1202. json.encode(obj,"12f")-->浮点数用%.12f的方式转换为字符串
  1203. */
  1204. static int l_json_encode_safe(lua_State *L) {
  1205. int top = lua_gettop(L);
  1206. if(top <= 1)
  1207. {
  1208. json_easy_config.encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
  1209. json_easy_config.encode_number_precision_mode = 'g';
  1210. }
  1211. else{
  1212. size_t len;
  1213. int prec = 0;
  1214. const char* mode = luaL_optlstring(L, top, "7g",&len);
  1215. for(int i = 0; i<len+1; i++)
  1216. {
  1217. if( *(mode+i) >= '0' && *(mode+i) <= '9' )
  1218. {
  1219. prec *= 10;
  1220. prec += *(mode+i)-'0';
  1221. }
  1222. else
  1223. {
  1224. if(*(mode+i) == 'f')
  1225. {
  1226. json_easy_config.encode_number_precision_mode = *(mode+i);
  1227. }
  1228. else
  1229. {
  1230. json_easy_config.encode_number_precision_mode = 'g';
  1231. }
  1232. break;
  1233. }
  1234. }
  1235. if (prec>=0 && prec<=14)
  1236. {
  1237. json_easy_config.encode_number_precision = prec;
  1238. }
  1239. else
  1240. {
  1241. json_easy_config.encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
  1242. }
  1243. --top;
  1244. lua_settop(L,top);
  1245. }
  1246. lua_pushcfunction(L, json_encode);
  1247. lua_insert(L, 1);
  1248. int status = lua_pcall(L, top, 1, 0);
  1249. if (status != LUA_OK) {
  1250. lua_pushnil(L);
  1251. lua_insert(L, 1);
  1252. return 2;
  1253. }
  1254. return 1;
  1255. }
  1256. // /*
  1257. // 设置encode数字精度和模式
  1258. // @api json.encode_number_precision
  1259. // @param string 浮点数精度和模式,这项不存在的时候,为默认值"7g",数字只支持"0~14",模式只支持"f/g"
  1260. // @return nil 无返回值
  1261. // @usage
  1262. // json.encode_number_precision()-->浮点数用%.7g的方式转换为字符串
  1263. // json.encode_number_precision("2")-->浮点数用%.2g的方式转换为字符串
  1264. // json.encode_number_precision("10f")-->浮点数用%.10f的方式转换为字符串
  1265. // */
  1266. // static int l_json_cfg_encode_number_precision(lua_State *L) {
  1267. // int top = lua_gettop(L);
  1268. // if(top <= 0)
  1269. // {
  1270. // json_easy_config.encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
  1271. // json_easy_config.encode_number_precision_mode = 'g';
  1272. // }
  1273. // else{
  1274. // size_t len;
  1275. // int prec = 0;
  1276. // const char* mode = luaL_optlstring(L, top, "7g",&len);
  1277. // for(int i = 0; i<len+1; i++)
  1278. // {
  1279. // if( *(mode+i) >= '0' && *(mode+i) <= '9' )
  1280. // {
  1281. // prec *= 10;
  1282. // prec += *(mode+i)-'0';
  1283. // }
  1284. // else
  1285. // {
  1286. // if(*(mode+i) == 'f')
  1287. // {
  1288. // json_easy_config.encode_number_precision_mode = *(mode+i);
  1289. // }
  1290. // else
  1291. // {
  1292. // json_easy_config.encode_number_precision_mode = 'g';
  1293. // }
  1294. // break;
  1295. // }
  1296. // }
  1297. // if (prec>=0 && prec<=14)
  1298. // {
  1299. // json_easy_config.encode_number_precision = prec;
  1300. // }
  1301. // else
  1302. // {
  1303. // json_easy_config.encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;
  1304. // }
  1305. // --top;
  1306. // lua_settop(L,top);
  1307. // }
  1308. // return 0;
  1309. // }
  1310. /*
  1311. 将字符串反序列化为对象
  1312. @api json.decode(str)
  1313. @string 需要反序列化的json字符串
  1314. @return obj 反序列化后的对象(通常是table), 失败的话返回nil
  1315. @return result 成功返回1,否则返回0
  1316. @return err 反序列化失败的报错信息
  1317. @usage
  1318. json.decode("[1,2,3,4,5,6]")
  1319. */
  1320. static int l_json_decode_safe(lua_State *L) {
  1321. int top = lua_gettop(L);
  1322. lua_pushcfunction(L, json_decode);
  1323. lua_insert(L, 1);
  1324. int status = lua_pcall(L, top, 1, 0);
  1325. if (status != LUA_OK) {
  1326. const char *msg = lua_tostring(L, -1);
  1327. lua_pushnil(L);
  1328. lua_pushboolean(L, 0);
  1329. lua_pushstring(L, msg);
  1330. return 3;
  1331. }
  1332. else {
  1333. lua_pushinteger(L, 1);
  1334. return 2;
  1335. }
  1336. }
  1337. #include "rotable2.h"
  1338. static const rotable_Reg_t reg_json[] = {
  1339. { "encode", ROREG_FUNC(l_json_encode_safe)},
  1340. { "decode", ROREG_FUNC(l_json_decode_safe)},
  1341. // { "encode_sparse_array", json_cfg_encode_sparse_array },
  1342. // { "encode_max_depth", json_cfg_encode_max_depth },
  1343. // { "decode_max_depth", json_cfg_decode_max_depth },
  1344. // { "encode_number_precision", l_json_cfg_encode_number_precision , 0},
  1345. // { "encode_keep_buffer", json_cfg_encode_keep_buffer },
  1346. // { "encode_invalid_numbers", json_cfg_encode_invalid_numbers },
  1347. // { "decode_invalid_numbers", json_cfg_decode_invalid_numbers },
  1348. // { "new", lua_cjson_new },
  1349. { NULL, ROREG_INT(0)}
  1350. };
  1351. /* Return cjson module table */
  1352. static int lua_cjson_new(lua_State *l)
  1353. {
  1354. /* Initialise number conversions */
  1355. //fpconv_init();
  1356. /* cjson module table */
  1357. //lua_newtable(l);
  1358. /* Register functions with config data as upvalue */
  1359. //json_create_config(l);
  1360. //luaL_setfuncs(l, reg, 1);
  1361. /* Set cjson.null */
  1362. //lua_pushlightuserdata(l, NULL);
  1363. //lua_setfield(l, -2, "null");
  1364. // /* Set module name / version fields */
  1365. // lua_pushliteral(l, CJSON_MODNAME);
  1366. // lua_setfield(l, -2, "_NAME");
  1367. // lua_pushliteral(l, CJSON_VERSION);
  1368. // lua_setfield(l, -2, "_VERSION");
  1369. luat_newlib2(l, reg_json);
  1370. return 1;
  1371. }
  1372. // /* Return cjson.safe module table */
  1373. // static int lua_cjson_safe_new(lua_State *l)
  1374. // {
  1375. // const char *func[] = { "decode", "encode", NULL };
  1376. // int i;
  1377. // lua_cjson_new(l);
  1378. // /* Fix new() method */
  1379. // lua_pushcfunction(l, lua_cjson_safe_new);
  1380. // lua_setfield(l, -2, "new");
  1381. // for (i = 0; func[i]; i++) {
  1382. // lua_getfield(l, -1, func[i]);
  1383. // lua_pushcclosure(l, json_protect_conversion, 1);
  1384. // lua_setfield(l, -2, func[i]);
  1385. // }
  1386. // return 1;
  1387. // }
  1388. int luaopen_cjson(lua_State *l)
  1389. {
  1390. lua_cjson_new(l);
  1391. // #ifdef ENABLE_CJSON_GLOBAL
  1392. // /* Register a global "cjson" table. */
  1393. // lua_pushvalue(l, -1);
  1394. // lua_setglobal(l, CJSON_MODNAME);
  1395. // #endif
  1396. /* Return cjson table */
  1397. return 1;
  1398. }
  1399. // int luaopen_cjson_safe(lua_State *l)
  1400. // {
  1401. // lua_cjson_safe_new(l);
  1402. // /* Return cjson.safe table */
  1403. // return 1;
  1404. // }
  1405. /* vi:ai et sw=4 ts=4:
  1406. */