-
Notifications
You must be signed in to change notification settings - Fork 8
/
lparson.c
766 lines (668 loc) · 19.2 KB
/
lparson.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h> // strtoll
#include <string.h> // strcmp
#include "lparson.h"
#include "parson.h"
#define MAX_KEY_LEN 256
#define MAX_STACK_DEEP 1024
#define ARRAY_OPT "__opt"
/* ========================== lua 5.1 ======================================= */
#ifndef LUA_MAXINTEGER
#define LUA_MAXINTEGER (2147483647)
#endif
#ifndef LUA_MININTEGER
#define LUA_MININTEGER (-2147483647 - 1)
#endif
/* this function was copy from src of lua5.3.1 */
void luaL_setfuncs_ex(lua_State *L, const luaL_Reg *l, int nup)
{
luaL_checkstack(L, nup, "too many upvalues");
for (; l->name != NULL; l++)
{ /* fill the table with given functions */
int i;
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -nup);
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_setfield(L, -(nup + 2), l->name);
}
lua_pop(L, nup); /* remove upvalues */
}
#ifndef luaL_newlibtable
#define luaL_newlibtable(L, l) \
lua_createtable(L, 0, sizeof(l) / sizeof((l)[0]) - 1)
#endif
#ifndef luaL_newlib
#define luaL_newlib(L, l) \
(luaL_newlibtable(L, l), luaL_setfuncs_ex(L, l, 0))
#endif
/* ========================== lua 5.1 ======================================= */
typedef const char *lpe_t; // lua_parson_error_t
struct lpo // lua parson option
{
double opt;
lpe_t error;
};
static JSON_Value *encode_lua_value(lua_State *L, int index, struct lpo *option);
static int decode_parson_value(lua_State *L, const JSON_Value *js_val,
struct lpo *option);
static inline void lua_parson_error(lua_State *L, struct lpo *option)
{
luaL_error(L, option->error);
}
/**
* check a lua table is object or array
* @return object = 0, max_index = 1 if using opt
* array = 1, max_index is max array index
*/
static int check_type(lua_State *L, int index, lua_Integer *max_index, double opt)
{
lua_Integer key = 0;
int key_count = 0;
double index_opt = 0.;
double percent_opt = 0.;
lua_Integer max_key = 0;
#if LUA_VERSION_NUM < 503
double num_key = 0.;
#endif
if (luaL_getmetafield(L, index, ARRAY_OPT))
{
if (LUA_TNUMBER == lua_type(L, -1)) opt = lua_tonumber(L, -1);
lua_pop(L, 1); /* pop metafield value */
}
// encode as object, using opt
if (0. == opt)
{
*max_index = 1;
return 0;
}
if (opt > 1) percent_opt = modf(opt, &index_opt);
// try to find out the max key
lua_pushnil(L);
while (lua_next(L, index) != 0)
{
/* stack status:table, key, value */
#if LUA_VERSION_NUM >= 503 // lua 5.3 has int64
if (!lua_isinteger(L, -2))
{
goto NO_MAX_KEY;
}
key = lua_tointeger(L, -2);
if (key < 1)
{
goto NO_MAX_KEY;
}
#else
if (lua_type(L, -2) != LUA_TNUMBER)
{
goto NO_MAX_KEY;
}
num_key = lua_tonumber(L, -2);
key = floor(num_key);
/* array index must be interger and >= 1 */
if (key < 1 || key != num_key)
{
goto NO_MAX_KEY;
}
#endif
key_count++;
if (key > max_key) max_key = key;
lua_pop(L, 1);
}
if (opt != 1.0)
{
// empty table encode as object
if (0 == max_key) return 0;
// max_key larger than opt integer part and then item count fail
// to fill the percent of opt fractional part, encode as object
if (opt > 0. && max_key > index_opt
&& ((double)key_count) / max_key < percent_opt)
{
*max_index = 1;
return 0;
}
}
*max_index = max_key;
return 1;
NO_MAX_KEY:
lua_pop(L, 2);
if (1. == opt)
{
// force encode as array, no max key found
*max_index = -1;
return 1;
}
else
{
// none integer key found, encode as object
if (opt >= 0) *max_index = 1;
return 0;
}
}
static inline JSON_Value *encode_value(lua_State *L, int index, struct lpo *option)
{
JSON_Value *val = (JSON_Value *)0;
int ty = lua_type(L, index);
switch (ty)
{
case LUA_TNIL:
{
val = json_value_init_null();
}
break;
case LUA_TBOOLEAN:
{
val = json_value_init_boolean(lua_toboolean(L, -1));
}
break;
case LUA_TSTRING:
{
val = json_value_init_string(lua_tostring(L, -1));
}
break;
case LUA_TNUMBER:
{
val = json_value_init_number(lua_tonumber(L, -1));
}
break;
case LUA_TTABLE:
{
val = encode_lua_value(L, lua_gettop(L), option);
}
break;
default:
{
option->error = "table value must be nil,string,number,table,boolean";
}
}
return val;
}
static JSON_Value *encode_invalid_key_array(lua_State *L, int index,
struct lpo *option)
{
JSON_Status st = 0;
JSON_Value *val = json_value_init_array();
JSON_Array *root_array = json_value_get_array(val);
int stack_top = lua_gettop(L);
lua_pushnil(L);
while (lua_next(L, index) != 0)
{
JSON_Value *array_val = (JSON_Value *)0;
array_val = encode_value(L, stack_top + 2, option);
if (!array_val)
{
lua_pop(L, 2);
json_value_free(val);
return 0;
}
st = json_array_append_value(root_array, array_val);
if (JSONSuccess != st)
{
lua_pop(L, 2);
json_value_free(val);
return 0;
}
lua_pop(L, 1);
}
assert(val);
return val;
}
static JSON_Value *encode_array(lua_State *L, int index, int max_index,
struct lpo *option)
{
int cur_key = 0;
JSON_Status st = 0;
JSON_Value *val = json_value_init_array();
JSON_Array *root_array = json_value_get_array(val);
int stack_top = lua_gettop(L);
for (cur_key = 1; cur_key <= max_index; cur_key++)
{
JSON_Value *array_val = (JSON_Value *)0;
lua_rawgeti(L, -1, cur_key);
array_val = encode_value(L, stack_top + 1, option);
if (!array_val)
{
lua_pop(L, 1);
json_value_free(val);
return 0;
}
st = json_array_append_value(root_array, array_val);
if (JSONSuccess != st)
{
lua_pop(L, 1);
json_value_free(val);
return 0;
}
lua_pop(L, 1);
}
assert(val);
return val;
}
static inline JSON_Value *encode_object(lua_State *L, int index, int use_cvt,
struct lpo *option)
{
JSON_Value *root_val = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_val);
int converted = 0;
lua_pushnil(L);
while (lua_next(L, index) != 0)
{
JSON_Status st = 0;
char key[MAX_KEY_LEN];
int ty = lua_type(L, -2);
const char *pkey = (char *)0;
JSON_Value *obj_val = (JSON_Value *)0;
switch (ty)
{
case LUA_TNUMBER:
{
converted = 1;
#if LUA_VERSION_NUM >= 503 // lua 5.3 has int64
if (lua_isinteger(L, -2))
snprintf(key, MAX_KEY_LEN, "%lld", lua_tointeger(L, -2));
else
snprintf(key, MAX_KEY_LEN, "%f", lua_tonumber(L, -2));
#else
double val = lua_tonumber(L, -2);
if (floor(val) == val) /* interger key */
snprintf(key, MAX_KEY_LEN, "%.0f", val);
else
snprintf(key, MAX_KEY_LEN, "%f", val);
#endif
pkey = key;
}
break;
case LUA_TSTRING:
{
size_t len = 0;
pkey = lua_tolstring(L, -2, &len);
if (len <= 0 || len > MAX_KEY_LEN)
{
option->error = "table string key too long";
lua_pop(L, 2);
json_value_free(root_val);
return 0;
}
}
break;
default:
{
/* LUA_TNIL LUA_TBOOLEAN LUA_TTABLE LUA_TFUNCTION, LUA_TUSERDATA,
* LUA_TTHREAD, LUA_TLIGHTUSERDATA can't not be key
*/
option->error = "table key must be a number or string";
lua_pop(L, 2);
json_value_free(root_val);
return 0;
}
}
assert(pkey);
obj_val = encode_value(L, lua_gettop(L), option);
if (!obj_val)
{
lua_pop(L, 2);
json_value_free(root_val);
return 0;
}
st = json_object_set_value(root_object, pkey, obj_val);
if (JSONSuccess != st)
{
option->error = "json_object_set_value fail";
lua_pop(L, 2);
json_value_free(root_val);
return 0;
}
lua_pop(L, 1);
}
// append `__array_opt` to object if using opt
if (converted && use_cvt >= 0)
{
json_object_set_value(root_object,
ARRAY_OPT, json_value_init_number(0));
}
return root_val;
}
static JSON_Value *encode_lua_value(lua_State *L, int index, struct lpo *option)
{
lua_Integer max_index = -1;
if (lua_gettop(L) > MAX_STACK_DEEP)
{
option->error = "lua parson encode too deep";
return (JSON_Value *)0;
}
if (!lua_checkstack(L, 2))
{
option->error = "lua parson stack overflow";
return (JSON_Value *)0;
}
int type = check_type(L, index, &max_index, option->opt);
if (0 == type)
{
return encode_object(L, index, (int)max_index, option);
}
return max_index > 0 ? encode_array(L, index, (int)max_index, option)
: encode_invalid_key_array(L, index, option);
}
/**
* encode a lua table to json string. a lua error will throw if any error occur
* @param tbl a lua table to be encode
* @param pretty boolean, format json string to pretty human readable or not
* @param opt number, option to set the table as array or object
* @return json string
*/
static int encode(lua_State *L)
{
int pretty = 0;
char *str;
JSON_Value *val;
struct lpo option;
option.error = (lpe_t)0;
if (lua_type(L, 1) != LUA_TTABLE)
{
option.error = "lua_parson encode,argument #1 table expect";
lua_parson_error(L, &option);
return 0;
}
pretty = lua_toboolean(L, 2);
option.opt = luaL_optnumber(L, 3, -1);
lua_settop(L, 1); /* remove other parameters,only table in stack now */
val = encode_lua_value(L, 1, &option);
if (!val)
{
lua_parson_error(L, &option);
return 0;
}
str = pretty ? json_serialize_to_string_pretty(val)
: json_serialize_to_string(val);
lua_pushstring(L, str);
json_free_serialized_string(str);
json_value_free(val);
return 1;
}
/**
* encode a lua table to file. a lua error will throw if any error occur
* @param tbl a lua table to be encode
* @param file a file path that json string will be written in
* @param pretty boolean, format json string to pretty human readable or not
* @param opt number, option to set the table as array or object
* @return boolean
*/
static int encode_to_file(lua_State *L)
{
int pretty = 0;
JSON_Value *val;
JSON_Status st = 0;
const char *path = 0;
struct lpo option;
option.error = (lpe_t)0;
if (lua_type(L, 1) != LUA_TTABLE)
{
option.error = "lua_parson encode,argument #1 table expect";
lua_parson_error(L, &option);
return 0;
}
path = luaL_checkstring(L, 2);
pretty = lua_toboolean(L, 3);
option.opt = luaL_optnumber(L, 4, -1);
lua_settop(L, 1); /* remove path,pretty,only table in stack now */
val = encode_lua_value(L, 1, &option);
if (!val)
{
lua_parson_error(L, &option);
return 0;
}
st = pretty ? json_serialize_to_file_pretty(val, path)
: json_serialize_to_file(val, path);
json_value_free(val);
lua_pushboolean(L, JSONSuccess == st ? 1 : 0);
return 1;
}
static int decode_parson_array(lua_State *L, const JSON_Value *js_val,
struct lpo *option)
{
size_t index = 0;
JSON_Array *array = json_value_get_array(js_val);
size_t count = json_array_get_count(array);
lua_createtable(L, (int)count, 0);
for (index = 0; index < count; index++)
{
JSON_Value *temp_value = json_array_get_value(array, index);
int stack_num = decode_parson_value(L, temp_value, option);
if (stack_num < 0)
{
lua_pop(L, 1); /* error, pop the table */
return -1;
}
else if (stack_num > 0)
{
assert(1 == stack_num);
/* lua table index start from 1 */
lua_rawseti(L, -2, index + 1);
}
/* stack_num == 0,value is nil */
}
return 1;
}
static int decode_parson_object(lua_State *L, const JSON_Value *js_val,
struct lpo *option)
{
int is_convert = 0;
size_t index = 0;
JSON_Object *object = json_value_get_object(js_val);
size_t count = json_object_get_count(object);
if (option->opt != 1 && option->opt >= 0)
{
if (json_object_get_value(object, ARRAY_OPT))
{
count--;
is_convert = 1;
}
}
lua_createtable(L, 0, (int)count);
for (index = 0; index < count; index++)
{
int stack_num = -1;
const char *key = json_object_get_name(object, index);
if (is_convert && 0 == strcmp(key, ARRAY_OPT))
{
continue;
}
JSON_Value *temp_value = json_object_get_value(object, key);
if (is_convert)
{
char *end_ptr = NULL;
long long int ikey = strtoll(key, &end_ptr, 0);
// *end_ptr != '\0' if convert error or key is float
// if can not convert to integer, push the string key
if (*end_ptr != '\0')
{
double d = strtod(key, &end_ptr);
if (*end_ptr != '\0')
lua_pushstring(L, key);
else
lua_pushnumber(L, d);
}
else
{
#if LUA_VERSION_NUM >= 503 // int64 support
lua_pushinteger(L, ikey);
#else
lua_pushnumber(L, ikey);
#endif
}
}
else
{
lua_pushstring(L, key);
}
stack_num = decode_parson_value(L, temp_value, option);
if (stack_num < 0)
{
lua_pop(L, 2); /* error, pop the key and table */
return -1;
}
else if (stack_num > 0)
{
assert(1 == stack_num);
lua_rawset(L, -3);
}
else
{
lua_pop(L, 1); /* null, pop the key */
}
}
return 1;
}
static int decode_parson_value(lua_State *L, const JSON_Value *js_val,
struct lpo *option)
{
int push_num = 1;
int stack_top = lua_gettop(L);
if (stack_top > MAX_STACK_DEEP)
{
option->error = "lua parson decode too deep";
return -1;
}
if (!lua_checkstack(L, 3))
{
option->error = "lua parson stack overflow";
return -1;
}
switch (json_value_get_type(js_val))
{
case JSONArray:
{
if (decode_parson_array(L, js_val, option) < 0) return -1;
}
break;
case JSONObject:
{
if (decode_parson_object(L, js_val, option) < 0) return -1;
}
break;
case JSONString:
{
const char *val = json_value_get_string(js_val);
lua_pushstring(L, val);
}
break;
case JSONBoolean:
{
int val = json_value_get_boolean(js_val);
lua_pushboolean(L, val);
}
break;
case JSONNumber:
{
double val = json_value_get_number(js_val);
/* if using lua5.1,interger is int32 */
if (floor(val) == val && (val <= LUA_MAXINTEGER && val >= LUA_MININTEGER))
lua_pushinteger(L, (lua_Integer)val);
else
lua_pushnumber(L, val);
}
break;
case JSONNull:
{
push_num = 0;
}
break;
case JSONError:
{
option->error = "parson get error type";
return -1;
}
break;
default:
{
option->error = "unhandle json value type";
return -1;
}
}
assert(0 == push_num || 1 == push_num);
return push_num;
}
/**
* decode a json string to a lua table.a lua error will throw if any error occur
* @param a json string
* @param comment boolean, is the str containt comments
* @param opt number, enable integer key convertion if set
* @return a lua table
*/
static int decode(lua_State *L)
{
int return_code = 0;
JSON_Value *val = (JSON_Value *)0;
const char *str = luaL_checkstring(L, 1);
int comment = lua_toboolean(L, 2);
struct lpo option;
option.error = (lpe_t)0;
option.opt = luaL_optnumber(L, 3, -1);
assert(str);
val = comment ? json_parse_string_with_comments(str) : json_parse_string(str);
if (!val)
{
option.error = "invalid json string";
lua_parson_error(L, &option);
return 0;
}
return_code = decode_parson_value(L, val, &option);
json_value_free(val);
if (return_code < 0)
{
lua_parson_error(L, &option);
return 0;
}
/* decode string "null",0 == return_code */
assert(0 == return_code || 1 == return_code);
return return_code;
}
/**
* decode a file content to a lua table.a lua error will throw if any error
* occur
* @param a json file path
* @param comment boolean, is the file content containt comments
* @param opt number, enable integer key convertion if set
* @return a lua table
*/
static int decode_from_file(lua_State *L)
{
int return_code = 0;
JSON_Value *val = (JSON_Value *)0;
const char *path = luaL_checkstring(L, 1);
int comment = lua_toboolean(L, 2);
struct lpo option;
option.error = (lpe_t)0;
option.opt = luaL_optnumber(L, 3, -1);
assert(path);
val = comment ? json_parse_file_with_comments(path) : json_parse_file(path);
if (!val)
{
option.error = "invalid json string";
lua_parson_error(L, &option);
return 0;
}
return_code = decode_parson_value(L, val, &option);
json_value_free(val);
if (return_code < 0)
{
lua_parson_error(L, &option);
return 0;
}
/* decode string "null",0 == return_code */
assert(0 == return_code || 1 == return_code);
return return_code;
}
/* ====================LIBRARY INITIALISATION FUNCTION======================= */
static const luaL_Reg lua_parson_lib[] = {{"encode", encode},
{"decode", decode},
{"encode_to_file", encode_to_file},
{"decode_from_file", decode_from_file},
{NULL, NULL}};
int luaopen_lua_parson(lua_State *L)
{
luaL_checkversion(L);
luaL_newlib(L, lua_parson_lib);
return 1;
}