aboutsummaryrefslogtreecommitdiffstats
path: root/main/lua-cjson/0003-empty-array-metadata.patch
blob: 4327c97872c3d82568a1c0d88773f860b4f00323 (plain) (blame)
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
From 42064f63d05edf0769814734ada8ab1ce3435931 Mon Sep 17 00:00:00 2001
From: Brian Maher <brian@brimworks.com>
Date: Mon, 1 Jun 2015 12:37:01 -0700
Subject: [PATCH 1/3] Allow empty array to be encoded with a metatable
 containing __is_cjson_array=true.

---
 lua_cjson.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/lua_cjson.c b/lua_cjson.c
index c14a1c5..283e3af 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -494,9 +494,25 @@ static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json)
     int max;
     int items;
 
-    max = 0;
+    max = -1;
     items = 0;
 
+    if ( lua_getmetatable(l, -1) ) {
+        int is_array, has_is_array;
+
+        lua_pushliteral(l, "__is_cjson_array");
+        lua_rawget(l, -2);
+        has_is_array = lua_isboolean(l, -1);
+        is_array = has_is_array && lua_toboolean(l, -1);
+        lua_pop(l, 2);
+
+        if ( has_is_array && ! is_array ) {
+            return -1;
+        } else {
+            max = 0;
+        }
+    }
+
     lua_pushnil(l);
     /* table, startkey */
     while (lua_next(l, -2) != 0) {
@@ -685,7 +701,7 @@ static void json_append_data(lua_State *l, json_config_t *cfg,
         current_depth++;
         json_check_encode_depth(l, cfg, current_depth, json);
         len = lua_array_length(l, cfg, json);
-        if (len > 0)
+        if (len >= 0)
             json_append_array(l, cfg, current_depth, json, len);
         else
             json_append_object(l, cfg, current_depth, json);
@@ -1204,6 +1220,16 @@ static void json_parse_array_context(lua_State *l, json_parse_t *json)
 
     /* Handle empty arrays */
     if (token.type == T_ARR_END) {
+        // Mark as array:
+        luaL_getmetatable(l, "cjson.array");
+        if ( lua_isnil(l, -1) ) {
+            lua_pop(l, 1);
+            luaL_newmetatable(l, "cjson.array");
+            lua_pushboolean(l, 1);
+            lua_setfield(l, -2, "__is_cjson_array");
+        }
+        lua_setmetatable(l, -2);
+
         json_decode_ascend(json);
         return;
     }
-- 
2.6.3


From 514c4b4761ac0552182acbf25039930ddca70070 Mon Sep 17 00:00:00 2001
From: Brian Maher <brian@brimworks.com>
Date: Thu, 3 Sep 2015 12:33:33 -0700
Subject: [PATCH 2/3] Replace __is_cjson_array with check for __name="array"
 and replace lightuserdata NULL with __name="null" meatable. This will allow
 better interchange with other modules that need to define "array" and "null"
 types. Note that you can also define custom implementations of "array" and
 "null" if you have access to the registry.

---
 lua_cjson.c | 65 ++++++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 43 insertions(+), 22 deletions(-)

diff --git a/lua_cjson.c b/lua_cjson.c
index 283e3af..2140743 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -497,20 +497,13 @@ static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json)
     max = -1;
     items = 0;
 
-    if ( lua_getmetatable(l, -1) ) {
-        int is_array, has_is_array;
-
-        lua_pushliteral(l, "__is_cjson_array");
+    if (lua_getmetatable(l, -1)) {
+        lua_pushliteral(l, "__name");
         lua_rawget(l, -2);
-        has_is_array = lua_isboolean(l, -1);
-        is_array = has_is_array && lua_toboolean(l, -1);
-        lua_pop(l, 2);
-
-        if ( has_is_array && ! is_array ) {
-            return -1;
-        } else {
+        if (lua_isstring(l, -1) && strcmp("array", lua_tostring(l, -1)) == 0) {
             max = 0;
         }
+        lua_pop(l, 2);
     }
 
     lua_pushnil(l);
@@ -698,6 +691,16 @@ static void json_append_data(lua_State *l, json_config_t *cfg,
             strbuf_append_mem(json, "false", 5);
         break;
     case LUA_TTABLE:
+        if (lua_getmetatable(l, -1)) {
+            lua_pushliteral(l, "__name");
+            lua_rawget(l, -2);
+            if (lua_isstring(l, -1) && strcmp("null", lua_tostring(l, -1)) == 0) {
+                strbuf_append_mem(json, "null", 4);
+                lua_pop(l, 2);
+                break;
+            }
+            lua_pop(l, 2);
+        }
         current_depth++;
         json_check_encode_depth(l, cfg, current_depth, json);
         len = lua_array_length(l, cfg, json);
@@ -1221,13 +1224,7 @@ static void json_parse_array_context(lua_State *l, json_parse_t *json)
     /* Handle empty arrays */
     if (token.type == T_ARR_END) {
         // Mark as array:
-        luaL_getmetatable(l, "cjson.array");
-        if ( lua_isnil(l, -1) ) {
-            lua_pop(l, 1);
-            luaL_newmetatable(l, "cjson.array");
-            lua_pushboolean(l, 1);
-            lua_setfield(l, -2, "__is_cjson_array");
-        }
+        luaL_getmetatable(l, "array");
         lua_setmetatable(l, -2);
 
         json_decode_ascend(json);
@@ -1274,9 +1271,9 @@ static void json_process_value(lua_State *l, json_parse_t *json,
         break;;
     case T_NULL:
         /* In Lua, setting "t[k] = nil" will delete k from the table.
-         * Hence a NULL pointer lightuserdata object is used instead */
-        lua_pushlightuserdata(l, NULL);
-        break;;
+         * Hence a NULL userdata object is used instead */
+        luaL_getmetatable(l, "null");
+        break;
     default:
         json_throw_parse_error(l, json, "value", token);
     }
@@ -1372,6 +1369,12 @@ static int json_protect_conversion(lua_State *l)
     return luaL_error(l, "Memory allocation error in CJSON protected call");
 }
 
+static int json_null_tostring(lua_State *l)
+{
+    lua_pushliteral(l, "null");
+    return 1;
+}
+
 /* Return cjson module table */
 static int lua_cjson_new(lua_State *l)
 {
@@ -1392,6 +1395,24 @@ static int lua_cjson_new(lua_State *l)
     /* Initialise number conversions */
     fpconv_init();
 
+    /* Initialize "null" */
+    if ( luaL_newmetatable(l, "null") ) {
+        lua_createtable(l, 0, 2);
+        lua_pushliteral(l, "null");
+        lua_setfield(l, -2, "__name");
+        lua_pushcfunction(l, json_null_tostring);
+        lua_setfield(l, -2, "__tostring");
+        lua_setmetatable(l, -2);
+    }
+    lua_pop(l, 1);
+
+    /* Initialize "array" metatable */
+    if ( luaL_newmetatable(l, "array") ) {
+        lua_pushliteral(l, "array");
+        lua_setfield(l, -2, "__name");
+    }
+    lua_pop(l, 1);
+
     /* cjson module table */
     lua_newtable(l);
 
@@ -1400,7 +1421,7 @@ static int lua_cjson_new(lua_State *l)
     luaL_setfuncs(l, reg, 1);
 
     /* Set cjson.null */
-    lua_pushlightuserdata(l, NULL);
+    luaL_getmetatable(l, "null");
     lua_setfield(l, -2, "null");
 
     /* Set module name / version fields */
-- 
2.6.3


From 438a71d35417453e2207f64f706d02f199525c7d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Tue, 8 Dec 2015 09:16:18 +0200
Subject: [PATCH 3/3] fix empty array default encoding, and add meta __name for
 objects

---
 lua_cjson.c | 52 +++++++++++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 19 deletions(-)

diff --git a/lua_cjson.c b/lua_cjson.c
index 2140743..25952ec 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -493,17 +493,20 @@ static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json)
     double k;
     int max;
     int items;
+    int is_array = 0;
+    const char *mt_name;
 
-    max = -1;
+    max = 0;
     items = 0;
 
     if (lua_getmetatable(l, -1)) {
         lua_pushliteral(l, "__name");
         lua_rawget(l, -2);
-        if (lua_isstring(l, -1) && strcmp("array", lua_tostring(l, -1)) == 0) {
-            max = 0;
-        }
+        mt_name = lua_isstring(l, -1) ? lua_tostring(l, -1) : "";
         lua_pop(l, 2);
+        if (strcmp("object", mt_name) == 0)
+            return -1;
+        is_array = strcmp("array", mt_name) == 0;
     }
 
     lua_pushnil(l);
@@ -521,6 +524,10 @@ static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json)
                 continue;
             }
         }
+        if (is_array) {
+            lua_pop(l, 1);
+            continue;
+        }
 
         /* Must not be an array (non integer key) */
         lua_pop(l, 2);
@@ -537,6 +544,10 @@ static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json)
         return -1;
     }
 
+    /* Backwards compatibility - encode untyped zero length table as object */
+    if (!is_array && max == 0)
+        return -1;
+
     return max;
 }
 
@@ -1166,6 +1177,8 @@ static void json_parse_object_context(lua_State *l, json_parse_t *json)
     json_decode_descend(l, json, 3);
 
     lua_newtable(l);
+    luaL_getmetatable(l, "cjson.object");
+    lua_setmetatable(l, -2);
 
     json_next_token(json, &token);
 
@@ -1218,15 +1231,13 @@ static void json_parse_array_context(lua_State *l, json_parse_t *json)
     json_decode_descend(l, json, 2);
 
     lua_newtable(l);
+    luaL_getmetatable(l, "cjson.array");
+    lua_setmetatable(l, -2);
 
     json_next_token(json, &token);
 
     /* Handle empty arrays */
     if (token.type == T_ARR_END) {
-        // Mark as array:
-        luaL_getmetatable(l, "array");
-        lua_setmetatable(l, -2);
-
         json_decode_ascend(json);
         return;
     }
@@ -1272,7 +1283,7 @@ static void json_process_value(lua_State *l, json_parse_t *json,
     case T_NULL:
         /* In Lua, setting "t[k] = nil" will delete k from the table.
          * Hence a NULL userdata object is used instead */
-        luaL_getmetatable(l, "null");
+        luaL_getmetatable(l, "cjson.null");
         break;
     default:
         json_throw_parse_error(l, json, "value", token);
@@ -1395,8 +1406,11 @@ static int lua_cjson_new(lua_State *l)
     /* Initialise number conversions */
     fpconv_init();
 
+    /* cjson module table */
+    lua_newtable(l);
+
     /* Initialize "null" */
-    if ( luaL_newmetatable(l, "null") ) {
+    if ( luaL_newmetatable(l, "cjson.null") ) {
         lua_createtable(l, 0, 2);
         lua_pushliteral(l, "null");
         lua_setfield(l, -2, "__name");
@@ -1404,26 +1418,26 @@ static int lua_cjson_new(lua_State *l)
         lua_setfield(l, -2, "__tostring");
         lua_setmetatable(l, -2);
     }
-    lua_pop(l, 1);
+    lua_setfield(l, -2, "null");
 
     /* Initialize "array" metatable */
-    if ( luaL_newmetatable(l, "array") ) {
+    if ( luaL_newmetatable(l, "cjson.array") ) {
         lua_pushliteral(l, "array");
         lua_setfield(l, -2, "__name");
     }
-    lua_pop(l, 1);
+    lua_setfield(l, -2, "array");
 
-    /* cjson module table */
-    lua_newtable(l);
+    /* Initialize "object" metatable */
+    if ( luaL_newmetatable(l, "cjson.object") ) {
+        lua_pushliteral(l, "object");
+        lua_setfield(l, -2, "__name");
+    }
+    lua_setfield(l, -2, "object");
 
     /* Register functions with config data as upvalue */
     json_create_config(l);
     luaL_setfuncs(l, reg, 1);
 
-    /* Set cjson.null */
-    luaL_getmetatable(l, "null");
-    lua_setfield(l, -2, "null");
-
     /* Set module name / version fields */
     lua_pushliteral(l, CJSON_MODNAME);
     lua_setfield(l, -2, "_NAME");
-- 
2.6.3