MCPcopy Create free account
hub / github.com/9chu/LuaSTGPlus / json_decode

Function json_decode

LuaSTGPlus/LuaExtensions/cjson4lua/lua_cjson.c:1256–1294  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1254}
1255
1256static int json_decode(lua_State *l)
1257{
1258 json_parse_t json;
1259 json_token_t token;
1260 size_t json_len;
1261
1262 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
1263
1264 json.cfg = json_fetch_config(l);
1265 json.data = luaL_checklstring(l, 1, &json_len);
1266 json.current_depth = 0;
1267 json.ptr = json.data;
1268
1269 /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3)
1270 *
1271 * CJSON can support any simple data type, hence only the first
1272 * character is guaranteed to be ASCII (at worst: '"'). This is
1273 * still enough to detect whether the wrong encoding is in use. */
1274 if (json_len >= 2 && (!json.data[0] || !json.data[1]))
1275 luaL_error(l, "JSON parser does not support UTF-16 or UTF-32");
1276
1277 /* Ensure the temporary buffer can hold the entire string.
1278 * This means we no longer need to do length checks since the decoded
1279 * string must be smaller than the entire json string */
1280 json.tmp = strbuf_new(json_len);
1281
1282 json_next_token(&json, &token);
1283 json_process_value(l, &json, &token);
1284
1285 /* Ensure there is no more input left */
1286 json_next_token(&json, &token);
1287
1288 if (token.type != T_END)
1289 json_throw_parse_error(l, &json, "the end", &token);
1290
1291 strbuf_free(json.tmp);
1292
1293 return 1;
1294}
1295
1296/* ===== INITIALISATION ===== */
1297

Callers

nothing calls this directly

Calls 6

json_fetch_configFunction · 0.85
strbuf_newFunction · 0.85
json_next_tokenFunction · 0.85
json_process_valueFunction · 0.85
json_throw_parse_errorFunction · 0.85
strbuf_freeFunction · 0.85

Tested by

no test coverage detected