-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_jansson.cpp
executable file
·547 lines (460 loc) · 12 KB
/
lua_jansson.cpp
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
#include "jansson\jansson.h"
// Metatable identifier in the Lua registry
static const char* json_metaname = "jansson.json";
// JSON null value
static json_t** jsonlua_null = NULL;
/***************************************************
Private utility methods
***************************************************/
// Calculates the largest whole-number index of a table.
static lua_Integer table_maxn(lua_State* L, int stack_idx)
{
// L: ...
luaL_checkstack(L, 2, "Nested too deep!");
lua_Integer max = 0, num = 0;
lua_Number dbl = 0;
lua_pushnil(L); // first key
if (stack_idx < 0) --stack_idx; // adjust table index
// L: ... nil
while (lua_next(L, stack_idx) != 0)
{
// L: ... key, value
lua_pop(L, 1); // don't need value
// L: ... key
if (lua_type(L, -1) == LUA_TNUMBER)
{
dbl = lua_tonumber(L, -1);
num = lua_tointeger(L, -1);
if (num == dbl && num > max)
max = num;
}
}
// L: ...
return max;
}
// Returns 1 if it's a JSON array, 2 if it's a JSON object,
// 3 if it's invalid, and 0 for uncertain (empty table {})
static unsigned char lua_table_json_type(lua_State* L)
{
// L: ... table
luaL_checkstack(L, 2, "Nested too deep!");
unsigned int numArrayEntries = 0;
unsigned char type = 0;
int ltype;
lua_pushnil(L); // first key
// L: ... table, nil
while (lua_next(L, -2) != 0)
{
// L: .. table, key, value
lua_pop(L, 1); // don't need the value
// L: .. table, key
ltype = lua_type(L, -1);
if (type != 2 && ltype == LUA_TNUMBER)
{
if (type != 1) type = 1;
}
else if (type != 1 && ltype == LUA_TSTRING)
{
if (type != 2) type = 2;
}
else
{
type = 3;
break;
}
}
// L: ... table
return type;
}
// Pushes a JSON object to the Lua stack
static void push_json_udata(lua_State* L, json_t* obj)
{
if (json_is_null(obj))
{
lua_pushlightuserdata(L, jsonlua_null);
lua_gettable(L, LUA_REGISTRYINDEX);
}
else
{
json_t** ud = (json_t**)lua_newuserdata(L, sizeof(json_t*));
luaL_getmetatable(L, json_metaname);
lua_setmetatable(L, -2);
*ud = obj;
}
}
/***************************************************
JSON encoding implementation
***************************************************/
static json_t* encode_lua_data(lua_State* L);
static json_t* encode_lua_table_array(lua_State* L)
{
// L: ... table
luaL_checkstack(L, 2, "Nested too deep!");
json_t* obj = json_array();
if (!obj)
{
lua_pushliteral(L, "Unknown JSON error while encoding.");
lua_error(L);
}
// Push it to the stack in case the Lua stack overflows while inserting members.
push_json_udata(L, obj);
// L: ... table, udata
json_t* member = NULL;
for (lua_Integer i = 1; i <= table_maxn(L, -2); ++i)
{
lua_pushnumber(L, i);
// L: ... table, udata, index
lua_gettable(L, -3);
// L: ... table, udata, value
member = encode_lua_data(L);
if (!member || json_array_append_new(obj, member) != 0)
{
lua_pushliteral(L, "Unknown JSON error while encoding.");
lua_error(L);
}
lua_pop(L, 1);
// L: ... table, udata
}
json_incref(obj); // add a reference for when it's GC'd
lua_pop(L, 1);
// L: ... table
return obj;
}
static json_t* encode_lua_table_object(lua_State* L)
{
// L: ... table
luaL_checkstack(L, 2, "Nested too deep!");
json_t* obj = json_object();
if (!obj)
{
lua_pushliteral(L, "Unable to create json_object for unknown reason.");
lua_error(L);
}
// Push it to the stack in case the Lua stack overflows while inserting members.
push_json_udata(L, obj);
// L: ... table, udata
json_t* member = NULL;
lua_pushnil(L); // first key
// L: ... table, udata, nil
while (lua_next(L, -3) != 0)
{
// L: ... table, udata, key, value
if (lua_type(L, -2) == LUA_TSTRING)
{
member = encode_lua_data(L);
if (!member || json_object_set_new(obj, lua_tostring(L, -2), member) != 0)
{
lua_pushliteral(L, "Unknown JSON error while encoding.");
lua_error(L);
}
}
lua_pop(L, 1);
// L: ... table, udata, key
}
// L: ... table, udata
json_incref(obj); // add a reference for when it's GC'd
lua_pop(L, 1);
// L: ... table
return obj;
}
static json_t* encode_lua_table(lua_State* L)
{
// L: ... table
json_t* obj = NULL;
switch (lua_table_json_type(L))
{
case 0:
luaL_error(L, "Ambiguity: empty table could be either array or object.");
break;
case 1:
obj = encode_lua_table_array(L);
break;
case 2:
obj = encode_lua_table_object(L);
break;
case 3:
luaL_error(L, "Unable to convert mixed table!");
break;
}
return obj;
}
static json_t* encode_lua_json_udata(lua_State* L)
{
json_t* obj = *((json_t**)lua_touserdata(L, -1));
// increment the reference count
json_incref(obj);
// return the same object
return obj;
}
static json_t* encode_lua_number(lua_State* L)
{
// L: ... number
lua_Number dbl = lua_tonumber(L, -1);
lua_Integer num = lua_tointeger(L, -1);
if (num == dbl)
return json_integer(num);
else
return json_real(dbl);
}
static json_t* encode_lua_data(lua_State* L)
{
// L: ... item
int ltype = lua_type(L, -1);
switch (ltype)
{
case LUA_TUSERDATA:
{
luaL_getmetatable(L, json_metaname);
lua_getmetatable(L, -2);
bool is_json = lua_rawequal(L, -1, -2);
lua_pop(L, 2);
if (is_json)
return encode_lua_json_udata(L);
break;
}
case LUA_TTABLE:
return encode_lua_table(L);
case LUA_TNUMBER:
return encode_lua_number(L);
case LUA_TSTRING:
return json_string(lua_tostring(L, -1));
case LUA_TBOOLEAN:
return (lua_toboolean(L, -1) ? json_true() : json_false());
case LUA_TNIL:
return json_null();
}
luaL_error(L, "Invalid data type: %s", lua_typename(L, ltype));
return NULL; // never reached
}
/***************************************************
JSON decoding implementation
***************************************************/
static void decode_json_data(lua_State* L, json_t* obj);
static void decode_json_object(lua_State* L, json_t* obj)
{
luaL_checkstack(L, 3, "Nested too deep!");
lua_newtable(L);
void* itr = json_object_iter(obj);
const char* key = NULL;
json_t* val = NULL;
while (itr)
{
key = json_object_iter_key(itr);
val = json_object_iter_value(itr);
lua_pushstring(L, key);
decode_json_data(L, val);
lua_rawset(L, -3);
itr = json_object_iter_next(obj, itr);
}
}
static void decode_json_array(lua_State* L, json_t* obj)
{
luaL_checkstack(L, 3, "Nested too deep!");
lua_newtable(L);
for (unsigned int i = 0; i < json_array_size(obj); ++i)
{
lua_pushinteger(L, i+1);
decode_json_data(L, json_array_get(obj, i));
lua_rawset(L, -3);
}
}
static void decode_json_data(lua_State* L, json_t* obj)
{
switch (json_typeof(obj))
{
case JSON_OBJECT:
decode_json_object(L, obj);
break;
case JSON_ARRAY:
decode_json_array(L, obj);
break;
case JSON_STRING:
lua_pushstring(L, json_string_value(obj));
break;
case JSON_REAL:
lua_pushnumber(L, json_real_value(obj));
break;
case JSON_INTEGER:
lua_pushinteger(L, json_integer_value(obj));
break;
case JSON_TRUE:
lua_pushboolean(L, 1);
break;
case JSON_FALSE:
lua_pushboolean(L, 0);
break;
case JSON_NULL:
push_json_udata(L, json_null());
break;
}
}
/***************************************************
JSON library methods
***************************************************/
// Converts a Lua datum into a JSON string
static int Ljson_encode(lua_State* L)
{
lua_settop(L, 2);
// L: item, flags
lua_Integer flags = 0;
if (!lua_isnoneornil(L, 2))
flags = luaL_checkinteger(L, 2);
lua_pop(L, 1);
json_t* obj = encode_lua_data(L);
char* json = json_dumps(obj, flags);
if (!json)
{
lua_pushnil(L);
lua_pushliteral(L, "Unable to encode JSON for an unknown reason.");
return 2;
}
else
{
lua_pushstring(L, json);
free(json);
return 1;
}
}
// Converts a JSON string into a Lua datum
static int Ljson_decode(lua_State* L)
{
lua_settop(L, 1);
// L: json
json_t* obj = NULL;
if (lua_isuserdata(L, 1) && !lua_islightuserdata(L, 1))
{
obj = *((json_t**)luaL_checkudata(L, 1, json_metaname));
}
else
{
json_error_t error;
obj = json_loads(luaL_checkstring(L, 1), &error);
if (!obj)
{
lua_pushnil(L);
lua_pushstring(L, error.text);
lua_pushinteger(L, error.line);
return 3;
}
}
decode_json_data(L, obj);
return 1;
}
// Converts a Lua table as a JSON array into a compiled JSON chunk
static int Ljson_as_array(lua_State* L)
{
lua_settop(L, 1);
luaL_argcheck(L, lua_type(L, 1) == LUA_TTABLE, 1, "expected table");
json_t* obj = encode_lua_table_array(L);
lua_pop(L, 1);
push_json_udata(L, obj);
return 1;
}
// Converts a Lua table as a JSON object into a compiled JSON chunk
static int Ljson_as_object(lua_State* L)
{
lua_settop(L, 1);
luaL_argcheck(L, lua_type(L, 1) == LUA_TTABLE, 1, "expected table");
json_t* obj = encode_lua_table_object(L);
lua_pop(L, 1);
push_json_udata(L, obj);
return 1;
}
// Calculates indent level flag
static int Ljson_indent_flag(lua_State* L)
{
lua_settop(L, 1);
int indent = luaL_checkinteger(L, 1);
lua_pushinteger(L, JSON_INDENT(indent));
return 1;
}
/***************************************************
JSON object meta table and methods
***************************************************/
// Cleans up the JSON chunk once it's no longer accessible
static int Ljson_gc_(lua_State* L)
{
json_t* obj = *((json_t**)luaL_checkudata(L, 1, json_metaname));
lua_pop(L, 1);
json_decref(obj);
return 0;
}
static int Ljson_tostring_(lua_State* L)
{
json_t** obj = (json_t**)luaL_checkudata(L, 1, json_metaname);
lua_pop(L, 1);
switch (json_typeof(*obj))
{
case JSON_OBJECT:
lua_pushfstring(L, "json: object (%p)", *obj);
break;
case JSON_ARRAY:
lua_pushfstring(L, "json: array (%p)", *obj);
break;
case JSON_NULL:
lua_pushliteral(L, "json: null");
break;
default:
lua_pushfstring(L, "json: unknown (%p)", *obj);
break;
}
return 1;
}
static const luaL_Reg json_meta[] = {
{"__gc", &Ljson_gc_},
{"__tostring", &Ljson_tostring_},
{NULL, NULL}
};
/***************************************************
Library setup data
***************************************************/
static const luaL_Reg json_lib[] = {
{"encode", &Ljson_encode},
{"decode", &Ljson_decode},
{"array", &Ljson_as_array},
{"object", &Ljson_as_object},
{NULL, NULL}
};
LUALIB_API int luaopen_jansson(lua_State *L)
{
// Create JSON object metatable
luaL_newmetatable(L, json_metaname); /* create new metatable */
lua_pushliteral(L, "__index");
lua_pushvalue(L, -2); /* push metatable */
lua_rawset(L, -3); /* metatable.__index = metatable */
luaL_register(L, NULL, json_meta);
lua_pop(L, 1);
// register 'json' library
luaL_register(L, "json", json_lib);
// add "null" JSON item
lua_pushliteral(L, "null");
// json, "null"
jsonlua_null = (json_t**)lua_newuserdata(L, sizeof(json_t*));
luaL_getmetatable(L, json_metaname);
lua_setmetatable(L, -2);
*jsonlua_null = json_null();
// json, "null", null
// stick it in the registry
lua_pushlightuserdata(L, jsonlua_null);
// json, "null", null, light
lua_pushvalue(L, -2);
// json, "null", null, light, null
lua_settable(L, LUA_REGISTRYINDEX);
// json, "null", null
lua_rawset(L, -3);
// json
// Add encoding flags
lua_pushliteral(L, "INDENT");
lua_pushcfunction(L, &Ljson_indent_flag);
lua_settable(L, -3);
lua_pushliteral(L, "COMPACT");
lua_pushinteger(L, JSON_COMPACT);
lua_settable(L, -3);
lua_pushliteral(L, "ENSURE_ASCII");
lua_pushinteger(L, JSON_ENSURE_ASCII);
lua_settable(L, -3);
lua_pushliteral(L, "SORT_KEYS");
lua_pushinteger(L, JSON_SORT_KEYS);
lua_settable(L, -3);
return 1;
}