Lines Matching refs:json
448 static void json_encode_exception(lua_State *l, json_config_t *cfg, strbuf_t *json, int lindex, in json_encode_exception() argument
452 strbuf_free(json); in json_encode_exception()
463 static void json_append_string(lua_State *l, strbuf_t *json, int lindex) in json_append_string() argument
476 strbuf_ensure_empty_length(json, len * 6 + 2); in json_append_string()
478 strbuf_append_char_unsafe(json, '\"'); in json_append_string()
482 strbuf_append_string(json, escstr); in json_append_string()
484 strbuf_append_char_unsafe(json, str[i]); in json_append_string()
486 strbuf_append_char_unsafe(json, '\"'); in json_append_string()
493 static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json) in lua_array_length() argument
528 json_encode_exception(l, cfg, json, -1, "excessively sparse array"); in lua_array_length()
537 int current_depth, strbuf_t *json) in json_check_encode_depth() argument
553 strbuf_free(json); in json_check_encode_depth()
560 int current_depth, strbuf_t *json);
567 strbuf_t *json, int array_length) in json_append_array() argument
571 strbuf_append_char(json, '['); in json_append_array()
576 strbuf_append_char(json, ','); in json_append_array()
581 json_append_data(l, cfg, current_depth, json); in json_append_array()
585 strbuf_append_char(json, ']'); in json_append_array()
589 strbuf_t *json, int lindex) in json_append_number() argument
597 json_encode_exception(l, cfg, json, lindex, "must not be NaN or Inf"); in json_append_number()
602 strbuf_append_mem(json, "nan", 3); in json_append_number()
608 strbuf_append_mem(json, "null", 4); in json_append_number()
613 strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); in json_append_number()
614 len = fpconv_g_fmt(strbuf_empty_ptr(json), num, cfg->encode_number_precision); in json_append_number()
615 strbuf_extend_length(json, len); in json_append_number()
619 int current_depth, strbuf_t *json) in json_append_object() argument
624 strbuf_append_char(json, '{'); in json_append_object()
631 strbuf_append_char(json, ','); in json_append_object()
638 strbuf_append_char(json, '"'); in json_append_object()
639 json_append_number(l, cfg, json, -2); in json_append_object()
640 strbuf_append_mem(json, "\":", 2); in json_append_object()
642 json_append_string(l, json, -2); in json_append_object()
643 strbuf_append_char(json, ':'); in json_append_object()
645 json_encode_exception(l, cfg, json, -2, in json_append_object()
651 json_append_data(l, cfg, current_depth, json); in json_append_object()
656 strbuf_append_char(json, '}'); in json_append_object()
661 int current_depth, strbuf_t *json) in json_append_data() argument
667 json_append_string(l, json, -1); in json_append_data()
670 json_append_number(l, cfg, json, -1); in json_append_data()
674 strbuf_append_mem(json, "true", 4); in json_append_data()
676 strbuf_append_mem(json, "false", 5); in json_append_data()
680 json_check_encode_depth(l, cfg, current_depth, json); in json_append_data()
681 len = lua_array_length(l, cfg, json); in json_append_data()
683 json_append_array(l, cfg, current_depth, json, len); in json_append_data()
685 json_append_object(l, cfg, current_depth, json); in json_append_data()
688 strbuf_append_mem(json, "null", 4); in json_append_data()
692 strbuf_append_mem(json, "null", 4); in json_append_data()
698 json_encode_exception(l, cfg, json, -1, "type not supported"); in json_append_data()
708 char *json; in json_encode() local
724 json = strbuf_string(encode_buf, &len); in json_encode()
726 lua_pushlstring(l, json, len); in json_encode()
736 static void json_process_value(lua_State *l, json_parse_t *json,
819 static int json_append_unicode_escape(json_parse_t *json) in json_append_unicode_escape() argument
828 codepoint = decode_hex4(json->ptr + 2); in json_append_unicode_escape()
844 if (*(json->ptr + escape_len) != '\\' || in json_append_unicode_escape()
845 *(json->ptr + escape_len + 1) != 'u') { in json_append_unicode_escape()
850 surrogate_low = decode_hex4(json->ptr + 2 + escape_len); in json_append_unicode_escape()
871 strbuf_append_mem_unsafe(json->tmp, utf8, len); in json_append_unicode_escape()
872 json->ptr += escape_len; in json_append_unicode_escape()
877 static void json_set_token_error(json_token_t *token, json_parse_t *json, in json_set_token_error() argument
881 token->index = json->ptr - json->data; in json_set_token_error()
885 static void json_next_string_token(json_parse_t *json, json_token_t *token) in json_next_string_token() argument
887 char *escape2char = json->cfg->escape2char; in json_next_string_token()
891 assert(*json->ptr == '"'); in json_next_string_token()
894 json->ptr++; in json_next_string_token()
900 strbuf_reset(json->tmp); in json_next_string_token()
902 while ((ch = *json->ptr) != '"') { in json_next_string_token()
905 json_set_token_error(token, json, "unexpected end of string"); in json_next_string_token()
912 ch = *(json->ptr + 1); in json_next_string_token()
917 if (json_append_unicode_escape(json) == 0) in json_next_string_token()
920 json_set_token_error(token, json, in json_next_string_token()
925 json_set_token_error(token, json, "invalid escape code"); in json_next_string_token()
930 json->ptr++; in json_next_string_token()
934 strbuf_append_char_unsafe(json->tmp, ch); in json_next_string_token()
935 json->ptr++; in json_next_string_token()
937 json->ptr++; /* Eat final quote (") */ in json_next_string_token()
939 strbuf_ensure_null(json->tmp); in json_next_string_token()
942 token->value.string = strbuf_string(json->tmp, &token->string_len); in json_next_string_token()
960 static int json_is_invalid_number(json_parse_t *json) in json_is_invalid_number() argument
962 const char *p = json->ptr; in json_is_invalid_number()
996 static void json_next_number_token(json_parse_t *json, json_token_t *token) in json_next_number_token() argument
1001 token->value.number = fpconv_strtod(json->ptr, &endptr); in json_next_number_token()
1002 if (json->ptr == endptr) in json_next_number_token()
1003 json_set_token_error(token, json, "invalid number"); in json_next_number_token()
1005 json->ptr = endptr; /* Skip the processed number */ in json_next_number_token()
1014 static void json_next_token(json_parse_t *json, json_token_t *token) in json_next_token() argument
1016 const json_token_type_t *ch2token = json->cfg->ch2token; in json_next_token()
1021 ch = (unsigned char)*(json->ptr); in json_next_token()
1025 json->ptr++; in json_next_token()
1030 token->index = json->ptr - json->data; in json_next_token()
1034 json_set_token_error(token, json, "invalid token"); in json_next_token()
1044 json->ptr++; in json_next_token()
1055 json_next_string_token(json, token); in json_next_token()
1058 if (!json->cfg->decode_invalid_numbers && json_is_invalid_number(json)) { in json_next_token()
1059 json_set_token_error(token, json, "invalid number"); in json_next_token()
1062 json_next_number_token(json, token); in json_next_token()
1064 } else if (!strncmp(json->ptr, "true", 4)) { in json_next_token()
1067 json->ptr += 4; in json_next_token()
1069 } else if (!strncmp(json->ptr, "false", 5)) { in json_next_token()
1072 json->ptr += 5; in json_next_token()
1074 } else if (!strncmp(json->ptr, "null", 4)) { in json_next_token()
1076 json->ptr += 4; in json_next_token()
1078 } else if (json->cfg->decode_invalid_numbers && in json_next_token()
1079 json_is_invalid_number(json)) { in json_next_token()
1085 json_next_number_token(json, token); in json_next_token()
1090 json_set_token_error(token, json, "invalid token"); in json_next_token()
1099 static void json_throw_parse_error(lua_State *l, json_parse_t *json, in json_throw_parse_error() argument
1104 strbuf_free(json->tmp); in json_throw_parse_error()
1116 static inline void json_decode_ascend(json_parse_t *json) in json_decode_ascend() argument
1118 json->current_depth--; in json_decode_ascend()
1121 static void json_decode_descend(lua_State *l, json_parse_t *json, int slots) in json_decode_descend() argument
1123 json->current_depth++; in json_decode_descend()
1125 if (json->current_depth <= json->cfg->decode_max_depth && in json_decode_descend()
1130 strbuf_free(json->tmp); in json_decode_descend()
1132 json->current_depth, json->ptr - json->data); in json_decode_descend()
1135 static void json_parse_object_context(lua_State *l, json_parse_t *json) in json_parse_object_context() argument
1141 json_decode_descend(l, json, 3); in json_parse_object_context()
1145 json_next_token(json, &token); in json_parse_object_context()
1149 json_decode_ascend(json); in json_parse_object_context()
1155 json_throw_parse_error(l, json, "object key string", &token); in json_parse_object_context()
1160 json_next_token(json, &token); in json_parse_object_context()
1162 json_throw_parse_error(l, json, "colon", &token); in json_parse_object_context()
1165 json_next_token(json, &token); in json_parse_object_context()
1166 json_process_value(l, json, &token); in json_parse_object_context()
1171 json_next_token(json, &token); in json_parse_object_context()
1174 json_decode_ascend(json); in json_parse_object_context()
1179 json_throw_parse_error(l, json, "comma or object end", &token); in json_parse_object_context()
1181 json_next_token(json, &token); in json_parse_object_context()
1186 static void json_parse_array_context(lua_State *l, json_parse_t *json) in json_parse_array_context() argument
1193 json_decode_descend(l, json, 2); in json_parse_array_context()
1197 json_next_token(json, &token); in json_parse_array_context()
1201 json_decode_ascend(json); in json_parse_array_context()
1206 json_process_value(l, json, &token); in json_parse_array_context()
1209 json_next_token(json, &token); in json_parse_array_context()
1212 json_decode_ascend(json); in json_parse_array_context()
1217 json_throw_parse_error(l, json, "comma or array end", &token); in json_parse_array_context()
1219 json_next_token(json, &token); in json_parse_array_context()
1224 static void json_process_value(lua_State *l, json_parse_t *json, in json_process_value() argument
1238 json_parse_object_context(l, json); in json_process_value()
1241 json_parse_array_context(l, json); in json_process_value()
1249 json_throw_parse_error(l, json, "value", token); in json_process_value()
1255 json_parse_t json; in json_decode() local
1261 json.cfg = json_fetch_config(l); in json_decode()
1262 json.data = luaL_checklstring(l, 1, &json_len); in json_decode()
1263 json.current_depth = 0; in json_decode()
1264 json.ptr = json.data; in json_decode()
1271 if (json_len >= 2 && (!json.data[0] || !json.data[1])) in json_decode()
1277 json.tmp = strbuf_new(json_len); in json_decode()
1279 json_next_token(&json, &token); in json_decode()
1280 json_process_value(l, &json, &token); in json_decode()
1283 json_next_token(&json, &token); in json_decode()
1286 json_throw_parse_error(l, &json, "the end", &token); in json_decode()
1288 strbuf_free(json.tmp); in json_decode()