Note: The major difference between this function and the internal cjson json_encode function is that we don't use the config object created by cjson. Instead we initialize and create a default config here to avoid passing the config as a user data object. Note: We also have a boolean which decides upon the error handling
| 1631 | // a user data object. |
| 1632 | // Note: We also have a boolean which decides upon the error handling |
| 1633 | int lua_cjson_decode(lua_State *l, const char* json_string, size_t json_len, |
| 1634 | int options_index, int protected_mode, char* errbuf, |
| 1635 | size_t errbuf_len) |
| 1636 | { |
| 1637 | json_config_t cfg; |
| 1638 | json_parse_t json; |
| 1639 | json_token_t token; |
| 1640 | |
| 1641 | json_initialize_config(l, options_index, &cfg, 0, protected_mode, errbuf, errbuf_len); |
| 1642 | json.cfg = &cfg; |
| 1643 | json.data = json_string; |
| 1644 | json.data_end = json_string + json_len; |
| 1645 | json.current_depth = 0; |
| 1646 | json.ptr = json.data; |
| 1647 | |
| 1648 | /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) |
| 1649 | * |
| 1650 | * CJSON can support any simple data type, hence only the first |
| 1651 | * character is guaranteed to be ASCII (at worst: '"'). This is |
| 1652 | * still enough to detect whether the wrong encoding is in use. */ |
| 1653 | if (json_len >= 2 && (!json.data[0] || !json.data[1])) |
| 1654 | { |
| 1655 | return DefoldError(&cfg, "JSON parser does not support UTF-16 or UTF-32"); |
| 1656 | } |
| 1657 | |
| 1658 | /* Ensure the temporary buffer can hold the entire string. |
| 1659 | * This means we no longer need to do length checks since the decoded |
| 1660 | * string must be smaller than the entire json string */ |
| 1661 | json.tmp = strbuf_new(json_len); |
| 1662 | |
| 1663 | json_next_token(&json, &token); |
| 1664 | if(!json_process_value(l, &json, &token)) |
| 1665 | return 0; |
| 1666 | |
| 1667 | /* Ensure there is no more input left */ |
| 1668 | json_next_token(&json, &token); |
| 1669 | |
| 1670 | if (token.type != T_END) |
| 1671 | return json_throw_parse_error(l, &json, "the end", &token); |
| 1672 | |
| 1673 | strbuf_free(json.tmp); |
| 1674 | |
| 1675 | return 1; |
| 1676 | } |
| 1677 | |
| 1678 | #if 0 // Defold: unused |
| 1679 | /* ===== INITIALISATION ===== */ |
no test coverage detected