diff options
Diffstat (limited to 'src/json.c')
| -rw-r--r-- | src/json.c | 59 |
1 files changed, 13 insertions, 46 deletions
diff --git a/src/json.c b/src/json.c index 03468e9f338..e2a4424463b 100644 --- a/src/json.c +++ b/src/json.c | |||
| @@ -215,47 +215,11 @@ json_has_suffix (const char *string, const char *suffix) | |||
| 215 | 215 | ||
| 216 | #endif | 216 | #endif |
| 217 | 217 | ||
| 218 | /* Create a multibyte Lisp string from the UTF-8 string in | 218 | /* Note that all callers of make_string_from_utf8 and build_string_from_utf8 |
| 219 | [DATA, DATA + SIZE). If the range [DATA, DATA + SIZE) does not | 219 | below either pass only value UTF-8 strings or use the functionf for |
| 220 | contain a valid UTF-8 string, the returned string will include raw | ||
| 221 | bytes. | ||
| 222 | Note that all callers below either pass only value UTF-8 strings or | ||
| 223 | use this function for formatting error messages; in the latter case | ||
| 224 | correctness isn't critical. */ | ||
| 225 | |||
| 226 | static Lisp_Object | ||
| 227 | json_make_string (const char *data, ptrdiff_t size) | ||
| 228 | { | ||
| 229 | ptrdiff_t chars, bytes; | ||
| 230 | parse_str_as_multibyte ((const unsigned char *) data, size, &chars, &bytes); | ||
| 231 | /* If DATA is a valid UTF-8 string, we can convert it to a Lisp | ||
| 232 | string directly. Otherwise, we need to decode it. */ | ||
| 233 | if (chars == size || bytes == size) | ||
| 234 | return make_specified_string (data, chars, size, true); | ||
| 235 | else | ||
| 236 | { | ||
| 237 | struct coding_system coding; | ||
| 238 | setup_coding_system (Qutf_8_unix, &coding); | ||
| 239 | coding.mode |= CODING_MODE_LAST_BLOCK; | ||
| 240 | coding.source = (const unsigned char *) data; | ||
| 241 | decode_coding_object (&coding, Qnil, 0, 0, size, size, Qt); | ||
| 242 | return coding.dst_object; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | /* Create a multibyte Lisp string from the NUL-terminated UTF-8 | ||
| 247 | string beginning at DATA. If the string is not a valid UTF-8 | ||
| 248 | string, an unspecified string is returned. Note that all callers | ||
| 249 | below either pass only value UTF-8 strings or use this function for | ||
| 250 | formatting error messages; in the latter case correctness isn't | 220 | formatting error messages; in the latter case correctness isn't |
| 251 | critical. */ | 221 | critical. */ |
| 252 | 222 | ||
| 253 | static Lisp_Object | ||
| 254 | json_build_string (const char *data) | ||
| 255 | { | ||
| 256 | return json_make_string (data, strlen (data)); | ||
| 257 | } | ||
| 258 | |||
| 259 | /* Return a unibyte string containing the sequence of UTF-8 encoding | 223 | /* Return a unibyte string containing the sequence of UTF-8 encoding |
| 260 | units of the UTF-8 representation of STRING. If STRING does not | 224 | units of the UTF-8 representation of STRING. If STRING does not |
| 261 | represent a sequence of Unicode scalar values, return a string with | 225 | represent a sequence of Unicode scalar values, return a string with |
| @@ -303,9 +267,11 @@ json_parse_error (const json_error_t *error) | |||
| 303 | symbol = Qjson_parse_error; | 267 | symbol = Qjson_parse_error; |
| 304 | #endif | 268 | #endif |
| 305 | xsignal (symbol, | 269 | xsignal (symbol, |
| 306 | list5 (json_build_string (error->text), | 270 | list5 (build_string_from_utf8 (error->text), |
| 307 | json_build_string (error->source), INT_TO_INTEGER (error->line), | 271 | build_string_from_utf8 (error->source), |
| 308 | INT_TO_INTEGER (error->column), INT_TO_INTEGER (error->position))); | 272 | INT_TO_INTEGER (error->line), |
| 273 | INT_TO_INTEGER (error->column), | ||
| 274 | INT_TO_INTEGER (error->position))); | ||
| 309 | } | 275 | } |
| 310 | 276 | ||
| 311 | static void | 277 | static void |
| @@ -648,7 +614,7 @@ usage: (json-serialize OBJECT &rest ARGS) */) | |||
| 648 | json_out_of_memory (); | 614 | json_out_of_memory (); |
| 649 | record_unwind_protect_ptr (json_free, string); | 615 | record_unwind_protect_ptr (json_free, string); |
| 650 | 616 | ||
| 651 | return unbind_to (count, json_build_string (string)); | 617 | return unbind_to (count, build_string_from_utf8 (string)); |
| 652 | } | 618 | } |
| 653 | 619 | ||
| 654 | struct json_buffer_and_size | 620 | struct json_buffer_and_size |
| @@ -855,8 +821,8 @@ json_to_lisp (json_t *json, struct json_configuration *conf) | |||
| 855 | case JSON_REAL: | 821 | case JSON_REAL: |
| 856 | return make_float (json_real_value (json)); | 822 | return make_float (json_real_value (json)); |
| 857 | case JSON_STRING: | 823 | case JSON_STRING: |
| 858 | return json_make_string (json_string_value (json), | 824 | return make_string_from_utf8 (json_string_value (json), |
| 859 | json_string_length (json)); | 825 | json_string_length (json)); |
| 860 | case JSON_ARRAY: | 826 | case JSON_ARRAY: |
| 861 | { | 827 | { |
| 862 | if (++lisp_eval_depth > max_lisp_eval_depth) | 828 | if (++lisp_eval_depth > max_lisp_eval_depth) |
| @@ -915,7 +881,7 @@ json_to_lisp (json_t *json, struct json_configuration *conf) | |||
| 915 | json_t *value; | 881 | json_t *value; |
| 916 | json_object_foreach (json, key_str, value) | 882 | json_object_foreach (json, key_str, value) |
| 917 | { | 883 | { |
| 918 | Lisp_Object key = json_build_string (key_str); | 884 | Lisp_Object key = build_string_from_utf8 (key_str); |
| 919 | EMACS_UINT hash; | 885 | EMACS_UINT hash; |
| 920 | ptrdiff_t i = hash_lookup (h, key, &hash); | 886 | ptrdiff_t i = hash_lookup (h, key, &hash); |
| 921 | /* Keys in JSON objects are unique, so the key can't | 887 | /* Keys in JSON objects are unique, so the key can't |
| @@ -932,7 +898,8 @@ json_to_lisp (json_t *json, struct json_configuration *conf) | |||
| 932 | json_t *value; | 898 | json_t *value; |
| 933 | json_object_foreach (json, key_str, value) | 899 | json_object_foreach (json, key_str, value) |
| 934 | { | 900 | { |
| 935 | Lisp_Object key = Fintern (json_build_string (key_str), Qnil); | 901 | Lisp_Object key |
| 902 | = Fintern (build_string_from_utf8 (key_str), Qnil); | ||
| 936 | result | 903 | result |
| 937 | = Fcons (Fcons (key, json_to_lisp (value, conf)), | 904 | = Fcons (Fcons (key, json_to_lisp (value, conf)), |
| 938 | result); | 905 | result); |