aboutsummaryrefslogtreecommitdiffstats
path: root/src/json.c
diff options
context:
space:
mode:
authorPhilipp Stephani2019-04-28 12:28:27 +0200
committerPhilipp Stephani2019-04-28 12:28:27 +0200
commit75ee20364c5ed4c175b13debaa53a2ba14168999 (patch)
tree3bdff98355f169ddf8fe20cc7c60aa13ba682139 /src/json.c
parentdbe81e16583adcae664871206694573209540286 (diff)
downloademacs-75ee20364c5ed4c175b13debaa53a2ba14168999.tar.gz
emacs-75ee20364c5ed4c175b13debaa53a2ba14168999.zip
Refactoring: move UTF-8 decoding functions into coding.h.
json_make_string and json_build_string are generally useful and not JSON-specific. Move them to coding.[ch]. * src/coding.h (build_utf8_string): Move from json.c. * src/coding.c (make_utf8_string): Move from json.c. * src/json.c (json_make_string, json_build_string): Move to coding.[ch]. Split out JSON-specific comment. (json_parse_error, Fjson_serialize, json_to_lisp): Fix callers. * src/emacs-module.c (module_make_function, module_make_string): Use new functions. (module_decode, module_decode_copy): Remove.
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c52
1 files changed, 8 insertions, 44 deletions
diff --git a/src/json.c b/src/json.c
index 03468e9f338..cc98914423b 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_utf8_string and build_utf8_string
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
226static Lisp_Object
227json_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
253static Lisp_Object
254json_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,8 +267,8 @@ 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_utf8_string (error->text),
307 json_build_string (error->source), INT_TO_INTEGER (error->line), 271 build_utf8_string (error->source), INT_TO_INTEGER (error->line),
308 INT_TO_INTEGER (error->column), INT_TO_INTEGER (error->position))); 272 INT_TO_INTEGER (error->column), INT_TO_INTEGER (error->position)));
309} 273}
310 274
@@ -648,7 +612,7 @@ usage: (json-serialize OBJECT &rest ARGS) */)
648 json_out_of_memory (); 612 json_out_of_memory ();
649 record_unwind_protect_ptr (json_free, string); 613 record_unwind_protect_ptr (json_free, string);
650 614
651 return unbind_to (count, json_build_string (string)); 615 return unbind_to (count, build_utf8_string (string));
652} 616}
653 617
654struct json_buffer_and_size 618struct json_buffer_and_size
@@ -855,7 +819,7 @@ json_to_lisp (json_t *json, struct json_configuration *conf)
855 case JSON_REAL: 819 case JSON_REAL:
856 return make_float (json_real_value (json)); 820 return make_float (json_real_value (json));
857 case JSON_STRING: 821 case JSON_STRING:
858 return json_make_string (json_string_value (json), 822 return make_utf8_string (json_string_value (json),
859 json_string_length (json)); 823 json_string_length (json));
860 case JSON_ARRAY: 824 case JSON_ARRAY:
861 { 825 {
@@ -915,7 +879,7 @@ json_to_lisp (json_t *json, struct json_configuration *conf)
915 json_t *value; 879 json_t *value;
916 json_object_foreach (json, key_str, value) 880 json_object_foreach (json, key_str, value)
917 { 881 {
918 Lisp_Object key = json_build_string (key_str); 882 Lisp_Object key = build_utf8_string (key_str);
919 EMACS_UINT hash; 883 EMACS_UINT hash;
920 ptrdiff_t i = hash_lookup (h, key, &hash); 884 ptrdiff_t i = hash_lookup (h, key, &hash);
921 /* Keys in JSON objects are unique, so the key can't 885 /* Keys in JSON objects are unique, so the key can't
@@ -932,7 +896,7 @@ json_to_lisp (json_t *json, struct json_configuration *conf)
932 json_t *value; 896 json_t *value;
933 json_object_foreach (json, key_str, value) 897 json_object_foreach (json, key_str, value)
934 { 898 {
935 Lisp_Object key = Fintern (json_build_string (key_str), Qnil); 899 Lisp_Object key = Fintern (build_utf8_string (key_str), Qnil);
936 result 900 result
937 = Fcons (Fcons (key, json_to_lisp (value, conf)), 901 = Fcons (Fcons (key, json_to_lisp (value, conf)),
938 result); 902 result);