diff options
author | Mark Pulford <mark@kyne.com.au> | 2012-01-16 12:00:46 +0400 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2012-03-04 12:24:34 +0400 |
commit | a7f3bec63026fcbffacf8f895c22c7623fce0d40 (patch) | |
tree | 695f50bdbddd0e6c28f947cdfeb0feac7b4b4668 /lua_cjson.c | |
parent | cb272bab61784d8d69dee20d27d7b6a9ca8df11a (diff) |
Merge lua_json_decode() into json_decode()
Also ensure cjson.decode() only receives a single argument.
Diffstat (limited to 'lua_cjson.c')
-rw-r--r-- | lua_cjson.c | 42 |
1 files changed, 14 insertions, 28 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index 5f7d6fb..9ec69cf 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -731,18 +731,14 @@ static void json_append_data(lua_State *l, json_config_t *cfg, static int json_encode(lua_State *l) { - json_config_t *cfg; + json_config_t *cfg = json_fetch_config(l); strbuf_t local_encode_buf; strbuf_t *encode_buf; char *json; int len; - /* Can't use json_verify_arg_count() since we need to ensure - * there is only 1 argument */ luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); - cfg = json_fetch_config(l); - if (!cfg->encode_keep_buffer) { /* Use private buffer */ encode_buf = &local_encode_buf; @@ -1282,17 +1278,27 @@ static void json_process_value(lua_State *l, json_parse_t *json, } } -/* json_text must be null terminated string */ -static void lua_json_decode(lua_State *l, const char *json_text, int json_len) +static int json_decode(lua_State *l) { json_parse_t json; json_token_t token; + size_t json_len; + + luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); json.cfg = json_fetch_config(l); + json.data = luaL_checklstring(l, 1, &json_len); json.current_depth = 0; - json.data = json_text; json.ptr = json.data; + /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) + * + * CJSON can support any simple data type, hence only the first + * character is guaranteed to be ASCII (at worst: '"'). This is + * still enough to detect whether the wrong encoding is in use. */ + if (json_len >= 2 && (!json.data[0] || !json.data[1])) + luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); + /* Ensure the temporary buffer can hold the entire string. * This means we no longer need to do length checks since the decoded * string must be smaller than the entire json string */ @@ -1308,26 +1314,6 @@ static void lua_json_decode(lua_State *l, const char *json_text, int json_len) json_throw_parse_error(l, &json, "the end", &token); strbuf_free(json.tmp); -} - -static int json_decode(lua_State *l) -{ - const char *json; - size_t len; - - json_verify_arg_count(l, 1); - - json = luaL_checklstring(l, 1, &len); - - /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) - * - * CJSON can support any simple data type, hence only the first - * character is guaranteed to be ASCII (at worst: '"'). This is - * still enough to detect whether the wrong encoding is in use. */ - if (len >= 2 && (!json[0] || !json[1])) - luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); - - lua_json_decode(l, json, len); return 1; } |