diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-08-10 19:39:52 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-08-10 19:39:52 +0930 |
commit | 386fdad6820fefa77355ed330f3072a5e2a4bd3e (patch) | |
tree | 2b72bae1be543dc1116eb512d50f1fa5731ed44e | |
parent | e5ee37f77f029f4df68a0fc92fc7a79c9f8af210 (diff) | |
download | lua-cjson-386fdad6820fefa77355ed330f3072a5e2a4bd3e.tar.gz lua-cjson-386fdad6820fefa77355ed330f3072a5e2a4bd3e.tar.bz2 lua-cjson-386fdad6820fefa77355ed330f3072a5e2a4bd3e.zip |
Fix detection of objects with numeric string keys
lua_array_length() recognised some objects with numeric string
keys as arrays since it was incorrectly using lua_isnumber().
When an object was incorrectly recognised as an array,
json_append_array() would not find any entries and generate a
result like:
[null,null,...]
Reported by: Zhang "agentzh" Yichun <agentzh@gmail.com>
-rw-r--r-- | lua_cjson.c | 2 | ||||
-rwxr-xr-x | tests/test.lua | 5 |
2 files changed, 4 insertions, 3 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index 4b1915a..b46e915 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
@@ -501,7 +501,7 @@ static int lua_array_length(lua_State *l, json_config_t *cfg) | |||
501 | /* table, startkey */ | 501 | /* table, startkey */ |
502 | while (lua_next(l, -2) != 0) { | 502 | while (lua_next(l, -2) != 0) { |
503 | /* table, key, value */ | 503 | /* table, key, value */ |
504 | if (lua_isnumber(l, -2) && | 504 | if (lua_type(l, -2) == LUA_TNUMBER && |
505 | (k = lua_tonumber(l, -2))) { | 505 | (k = lua_tonumber(l, -2))) { |
506 | /* Integer >= 1 ? */ | 506 | /* Integer >= 1 ? */ |
507 | if (floor(k) == k && k >= 1) { | 507 | if (floor(k) == k && k >= 1) { |
diff --git a/tests/test.lua b/tests/test.lua index 7a9b0ff..7a75243 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -118,13 +118,14 @@ local encode_table_tests = { | |||
118 | end, | 118 | end, |
119 | { json.encode, { { [3] = "sparse test" } }, | 119 | { json.encode, { { [3] = "sparse test" } }, |
120 | true, { '[null,null,"sparse test"]' } }, | 120 | true, { '[null,null,"sparse test"]' } }, |
121 | |||
122 | { json.encode, { { [1] = "one", [4] = "sparse test" } }, | 121 | { json.encode, { { [1] = "one", [4] = "sparse test" } }, |
123 | true, { '["one",null,null,"sparse test"]' } }, | 122 | true, { '["one",null,null,"sparse test"]' } }, |
124 | |||
125 | { json.encode, { { [1] = "one", [5] = "sparse test" } }, | 123 | { json.encode, { { [1] = "one", [5] = "sparse test" } }, |
126 | true, { '{"1":"one","5":"sparse test"}' } }, | 124 | true, { '{"1":"one","5":"sparse test"}' } }, |
127 | 125 | ||
126 | { json.encode, { { ["2"] = "numeric string key test" } }, | ||
127 | true, { '{"2":"numeric string key test"}' } }, | ||
128 | |||
128 | { json.encode, { nested5 }, true, { '[[[[["nested"]]]]]' } }, | 129 | { json.encode, { nested5 }, true, { '[[[[["nested"]]]]]' } }, |
129 | { json.encode, { { nested5 } }, | 130 | { json.encode, { { nested5 } }, |
130 | false, { "Cannot serialise, excessive nesting (6)" } }, | 131 | false, { "Cannot serialise, excessive nesting (6)" } }, |