aboutsummaryrefslogtreecommitdiffstats
path: root/src/json.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c77
1 files changed, 39 insertions, 38 deletions
diff --git a/src/json.c b/src/json.c
index 2901a20811a..3f1d27ad7fb 100644
--- a/src/json.c
+++ b/src/json.c
@@ -327,13 +327,14 @@ struct json_configuration {
327 Lisp_Object false_object; 327 Lisp_Object false_object;
328}; 328};
329 329
330static json_t *lisp_to_json (Lisp_Object, struct json_configuration *conf); 330static json_t *lisp_to_json (Lisp_Object,
331 const struct json_configuration *conf);
331 332
332/* Convert a Lisp object to a toplevel JSON object (array or object). */ 333/* Convert a Lisp object to a nonscalar JSON object (array or object). */
333 334
334static json_t * 335static json_t *
335lisp_to_json_toplevel_1 (Lisp_Object lisp, 336lisp_to_json_nonscalar_1 (Lisp_Object lisp,
336 struct json_configuration *conf) 337 const struct json_configuration *conf)
337{ 338{
338 json_t *json; 339 json_t *json;
339 ptrdiff_t count; 340 ptrdiff_t count;
@@ -448,16 +449,17 @@ lisp_to_json_toplevel_1 (Lisp_Object lisp,
448 return json; 449 return json;
449} 450}
450 451
451/* Convert LISP to a toplevel JSON object (array or object). Signal 452/* Convert LISP to a nonscalar JSON object (array or object). Signal
452 an error of type `wrong-type-argument' if LISP is not a vector, 453 an error of type `wrong-type-argument' if LISP is not a vector,
453 hashtable, alist, or plist. */ 454 hashtable, alist, or plist. */
454 455
455static json_t * 456static json_t *
456lisp_to_json_toplevel (Lisp_Object lisp, struct json_configuration *conf) 457lisp_to_json_nonscalar (Lisp_Object lisp,
458 const struct json_configuration *conf)
457{ 459{
458 if (++lisp_eval_depth > max_lisp_eval_depth) 460 if (++lisp_eval_depth > max_lisp_eval_depth)
459 xsignal0 (Qjson_object_too_deep); 461 xsignal0 (Qjson_object_too_deep);
460 json_t *json = lisp_to_json_toplevel_1 (lisp, conf); 462 json_t *json = lisp_to_json_nonscalar_1 (lisp, conf);
461 --lisp_eval_depth; 463 --lisp_eval_depth;
462 return json; 464 return json;
463} 465}
@@ -467,7 +469,7 @@ lisp_to_json_toplevel (Lisp_Object lisp, struct json_configuration *conf)
467 JSON object. */ 469 JSON object. */
468 470
469static json_t * 471static json_t *
470lisp_to_json (Lisp_Object lisp, struct json_configuration *conf) 472lisp_to_json (Lisp_Object lisp, const struct json_configuration *conf)
471{ 473{
472 if (EQ (lisp, conf->null_object)) 474 if (EQ (lisp, conf->null_object))
473 return json_check (json_null ()); 475 return json_check (json_null ());
@@ -499,7 +501,7 @@ lisp_to_json (Lisp_Object lisp, struct json_configuration *conf)
499 } 501 }
500 502
501 /* LISP now must be a vector, hashtable, alist, or plist. */ 503 /* LISP now must be a vector, hashtable, alist, or plist. */
502 return lisp_to_json_toplevel (lisp, conf); 504 return lisp_to_json_nonscalar (lisp, conf);
503} 505}
504 506
505static void 507static void
@@ -557,15 +559,15 @@ DEFUN ("json-serialize", Fjson_serialize, Sjson_serialize, 1, MANY,
557 NULL, 559 NULL,
558 doc: /* Return the JSON representation of OBJECT as a string. 560 doc: /* Return the JSON representation of OBJECT as a string.
559 561
560OBJECT must be a vector, hashtable, alist, or plist and its elements 562OBJECT must be t, a number, string, vector, hashtable, alist, plist,
561can recursively contain the Lisp equivalents to the JSON null and 563or the Lisp equivalents to the JSON null and false values, and its
562false values, t, numbers, strings, or other vectors hashtables, alists 564elements must recursively consist of the same kinds of values. t will
563or plists. t will be converted to the JSON true value. Vectors will 565be converted to the JSON true value. Vectors will be converted to
564be converted to JSON arrays, whereas hashtables, alists and plists are 566JSON arrays, whereas hashtables, alists and plists are converted to
565converted to JSON objects. Hashtable keys must be strings without 567JSON objects. Hashtable keys must be strings without embedded null
566embedded null characters and must be unique within each object. Alist 568characters and must be unique within each object. Alist and plist
567and plist keys must be symbols; if a key is duplicate, the first 569keys must be symbols; if a key is duplicate, the first instance is
568instance is used. 570used.
569 571
570The Lisp equivalents to the JSON null and false values are 572The Lisp equivalents to the JSON null and false values are
571configurable in the arguments ARGS, a list of keyword/argument pairs: 573configurable in the arguments ARGS, a list of keyword/argument pairs:
@@ -603,12 +605,10 @@ usage: (json-serialize OBJECT &rest ARGS) */)
603 {json_object_hashtable, json_array_array, QCnull, QCfalse}; 605 {json_object_hashtable, json_array_array, QCnull, QCfalse};
604 json_parse_args (nargs - 1, args + 1, &conf, false); 606 json_parse_args (nargs - 1, args + 1, &conf, false);
605 607
606 json_t *json = lisp_to_json_toplevel (args[0], &conf); 608 json_t *json = lisp_to_json (args[0], &conf);
607 record_unwind_protect_ptr (json_release_object, json); 609 record_unwind_protect_ptr (json_release_object, json);
608 610
609 /* If desired, we might want to add the following flags: 611 char *string = json_dumps (json, JSON_COMPACT | JSON_ENCODE_ANY);
610 JSON_DECODE_ANY, JSON_ALLOW_NUL. */
611 char *string = json_dumps (json, JSON_COMPACT);
612 if (string == NULL) 612 if (string == NULL)
613 json_out_of_memory (); 613 json_out_of_memory ();
614 record_unwind_protect_ptr (json_free, string); 614 record_unwind_protect_ptr (json_free, string);
@@ -723,12 +723,10 @@ usage: (json-insert OBJECT &rest ARGS) */)
723 move_gap_both (PT, PT_BYTE); 723 move_gap_both (PT, PT_BYTE);
724 struct json_insert_data data; 724 struct json_insert_data data;
725 data.inserted_bytes = 0; 725 data.inserted_bytes = 0;
726 /* If desired, we might want to add the following flags: 726 /* Could have used json_dumpb, but that became available only in
727 JSON_DECODE_ANY, JSON_ALLOW_NUL. */ 727 Jansson 2.10, whereas we want to support 2.7 and upward. */
728 int status 728 int status = json_dump_callback (json, json_insert_callback, &data,
729 /* Could have used json_dumpb, but that became available only in 729 JSON_COMPACT | JSON_ENCODE_ANY);
730 Jansson 2.10, whereas we want to support 2.7 and upward. */
731 = json_dump_callback (json, json_insert_callback, &data, JSON_COMPACT);
732 if (status == -1) 730 if (status == -1)
733 { 731 {
734 if (CONSP (data.error)) 732 if (CONSP (data.error))
@@ -791,7 +789,7 @@ usage: (json-insert OBJECT &rest ARGS) */)
791/* Convert a JSON object to a Lisp object. */ 789/* Convert a JSON object to a Lisp object. */
792 790
793static Lisp_Object ARG_NONNULL ((1)) 791static Lisp_Object ARG_NONNULL ((1))
794json_to_lisp (json_t *json, struct json_configuration *conf) 792json_to_lisp (json_t *json, const struct json_configuration *conf)
795{ 793{
796 switch (json_typeof (json)) 794 switch (json_typeof (json))
797 { 795 {
@@ -932,12 +930,12 @@ DEFUN ("json-parse-string", Fjson_parse_string, Sjson_parse_string, 1, MANY,
932 NULL, 930 NULL,
933 doc: /* Parse the JSON STRING into a Lisp object. 931 doc: /* Parse the JSON STRING into a Lisp object.
934This is essentially the reverse operation of `json-serialize', which 932This is essentially the reverse operation of `json-serialize', which
935see. The returned object will be a vector, list, hashtable, alist, or 933see. The returned object will be the JSON null value, the JSON false
936plist. Its elements will be the JSON null value, the JSON false 934value, t, a number, a string, a vector, a list, a hashtable, an alist,
937value, t, numbers, strings, or further vectors, hashtables, alists, or 935or a plist. Its elements will be further objects of these types. If
938plists. If there are duplicate keys in an object, all but the last 936there are duplicate keys in an object, all but the last one are
939one are ignored. If STRING doesn't contain a valid JSON object, this 937ignored. If STRING doesn't contain a valid JSON object, this function
940function signals an error of type `json-parse-error'. 938signals an error of type `json-parse-error'.
941 939
942The arguments ARGS are a list of keyword/argument pairs: 940The arguments ARGS are a list of keyword/argument pairs:
943 941
@@ -982,7 +980,8 @@ usage: (json-parse-string STRING &rest ARGS) */)
982 json_parse_args (nargs - 1, args + 1, &conf, true); 980 json_parse_args (nargs - 1, args + 1, &conf, true);
983 981
984 json_error_t error; 982 json_error_t error;
985 json_t *object = json_loads (SSDATA (encoded), 0, &error); 983 json_t *object
984 = json_loads (SSDATA (encoded), JSON_DECODE_ANY, &error);
986 if (object == NULL) 985 if (object == NULL)
987 json_parse_error (&error); 986 json_parse_error (&error);
988 987
@@ -1078,8 +1077,10 @@ usage: (json-parse-buffer &rest args) */)
1078 ptrdiff_t point = PT_BYTE; 1077 ptrdiff_t point = PT_BYTE;
1079 struct json_read_buffer_data data = {.point = point}; 1078 struct json_read_buffer_data data = {.point = point};
1080 json_error_t error; 1079 json_error_t error;
1081 json_t *object = json_load_callback (json_read_buffer_callback, &data, 1080 json_t *object
1082 JSON_DISABLE_EOF_CHECK, &error); 1081 = json_load_callback (json_read_buffer_callback, &data,
1082 JSON_DECODE_ANY | JSON_DISABLE_EOF_CHECK,
1083 &error);
1083 1084
1084 if (object == NULL) 1085 if (object == NULL)
1085 json_parse_error (&error); 1086 json_parse_error (&error);