aboutsummaryrefslogtreecommitdiffstats
path: root/src/coding.c
diff options
context:
space:
mode:
authorPhilipp Stephani2017-12-23 17:56:36 +0100
committerPhilipp Stephani2017-12-30 23:16:51 +0100
commita5835dfee139322de7aa071f1c87ef015acbecad (patch)
tree992b5222049f2aa2a124f8c3484bbd074eaecfd1 /src/coding.c
parent30ffc256abe7443a02b44490c518baf9a122b4c8 (diff)
downloademacs-a5835dfee139322de7aa071f1c87ef015acbecad.tar.gz
emacs-a5835dfee139322de7aa071f1c87ef015acbecad.zip
Improve error reporting when serializing non-Unicode strings to JSON
* src/coding.c (utf8_string_p): New helper function. (syms_of_coding) <utf-8-unix>: Move from json.c. * src/json.c (json_check_utf8): New helper function. (lisp_to_json_toplevel_1, lisp_to_json): Use it. To save a bit of time, check for invalid UTF-8 strings only after encountering an error, since Jansson already rejects them. * test/src/json-tests.el (json-serialize/invalid-unicode): Adapt expected error symbol.
Diffstat (limited to 'src/coding.c')
-rw-r--r--src/coding.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/coding.c b/src/coding.c
index 1705838ffad..5ea1e395f20 100644
--- a/src/coding.c
+++ b/src/coding.c
@@ -6360,6 +6360,27 @@ check_utf_8 (struct coding_system *coding)
6360} 6360}
6361 6361
6362 6362
6363/* Return whether STRING is a valid UTF-8 string. STRING must be a
6364 unibyte string. */
6365
6366bool
6367utf8_string_p (Lisp_Object string)
6368{
6369 eassert (!STRING_MULTIBYTE (string));
6370 struct coding_system coding;
6371 setup_coding_system (Qutf_8_unix, &coding);
6372 /* We initialize only the fields that check_utf_8 accesses. */
6373 coding.head_ascii = -1;
6374 coding.src_pos = 0;
6375 coding.src_pos_byte = 0;
6376 coding.src_chars = SCHARS (string);
6377 coding.src_bytes = SBYTES (string);
6378 coding.src_object = string;
6379 coding.eol_seen = EOL_SEEN_NONE;
6380 return check_utf_8 (&coding) != -1;
6381}
6382
6383
6363/* Detect how end-of-line of a text of length SRC_BYTES pointed by 6384/* Detect how end-of-line of a text of length SRC_BYTES pointed by
6364 SOURCE is encoded. If CATEGORY is one of 6385 SOURCE is encoded. If CATEGORY is one of
6365 coding_category_utf_16_XXXX, assume that CR and LF are encoded by 6386 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
@@ -10846,6 +10867,7 @@ syms_of_coding (void)
10846 DEFSYM (Qiso_2022, "iso-2022"); 10867 DEFSYM (Qiso_2022, "iso-2022");
10847 10868
10848 DEFSYM (Qutf_8, "utf-8"); 10869 DEFSYM (Qutf_8, "utf-8");
10870 DEFSYM (Qutf_8_unix, "utf-8-unix");
10849 DEFSYM (Qutf_8_emacs, "utf-8-emacs"); 10871 DEFSYM (Qutf_8_emacs, "utf-8-emacs");
10850 10872
10851#if defined (WINDOWSNT) || defined (CYGWIN) 10873#if defined (WINDOWSNT) || defined (CYGWIN)