diff options
| author | Yuuki Harano | 2021-02-20 18:00:36 +0900 |
|---|---|---|
| committer | Yuuki Harano | 2021-02-20 18:00:36 +0900 |
| commit | be2e47362b0f933dbc8e300e3d168296b7e2aac4 (patch) | |
| tree | 3b14e2e66d33566b81cb1d6e8bb04551d95dd2b9 /src/json.c | |
| parent | 949d3e50ec4ea7723bf14b93b66ad0b72f96f163 (diff) | |
| parent | c85c8e7d42ae2a5fc95fa7b14257389d8383b34d (diff) | |
| download | emacs-be2e47362b0f933dbc8e300e3d168296b7e2aac4.tar.gz emacs-be2e47362b0f933dbc8e300e3d168296b7e2aac4.zip | |
Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs into feature/pgtk
Diffstat (limited to 'src/json.c')
| -rw-r--r-- | src/json.c | 77 |
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 | ||
| 330 | static json_t *lisp_to_json (Lisp_Object, struct json_configuration *conf); | 330 | static 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 | ||
| 334 | static json_t * | 335 | static json_t * |
| 335 | lisp_to_json_toplevel_1 (Lisp_Object lisp, | 336 | lisp_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 | ||
| 455 | static json_t * | 456 | static json_t * |
| 456 | lisp_to_json_toplevel (Lisp_Object lisp, struct json_configuration *conf) | 457 | lisp_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 | ||
| 469 | static json_t * | 471 | static json_t * |
| 470 | lisp_to_json (Lisp_Object lisp, struct json_configuration *conf) | 472 | lisp_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 | ||
| 505 | static void | 507 | static 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 | ||
| 560 | OBJECT must be a vector, hashtable, alist, or plist and its elements | 562 | OBJECT must be t, a number, string, vector, hashtable, alist, plist, |
| 561 | can recursively contain the Lisp equivalents to the JSON null and | 563 | or the Lisp equivalents to the JSON null and false values, and its |
| 562 | false values, t, numbers, strings, or other vectors hashtables, alists | 564 | elements must recursively consist of the same kinds of values. t will |
| 563 | or plists. t will be converted to the JSON true value. Vectors will | 565 | be converted to the JSON true value. Vectors will be converted to |
| 564 | be converted to JSON arrays, whereas hashtables, alists and plists are | 566 | JSON arrays, whereas hashtables, alists and plists are converted to |
| 565 | converted to JSON objects. Hashtable keys must be strings without | 567 | JSON objects. Hashtable keys must be strings without embedded null |
| 566 | embedded null characters and must be unique within each object. Alist | 568 | characters and must be unique within each object. Alist and plist |
| 567 | and plist keys must be symbols; if a key is duplicate, the first | 569 | keys must be symbols; if a key is duplicate, the first instance is |
| 568 | instance is used. | 570 | used. |
| 569 | 571 | ||
| 570 | The Lisp equivalents to the JSON null and false values are | 572 | The Lisp equivalents to the JSON null and false values are |
| 571 | configurable in the arguments ARGS, a list of keyword/argument pairs: | 573 | configurable 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 | ||
| 793 | static Lisp_Object ARG_NONNULL ((1)) | 791 | static Lisp_Object ARG_NONNULL ((1)) |
| 794 | json_to_lisp (json_t *json, struct json_configuration *conf) | 792 | json_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. |
| 934 | This is essentially the reverse operation of `json-serialize', which | 932 | This is essentially the reverse operation of `json-serialize', which |
| 935 | see. The returned object will be a vector, list, hashtable, alist, or | 933 | see. The returned object will be the JSON null value, the JSON false |
| 936 | plist. Its elements will be the JSON null value, the JSON false | 934 | value, t, a number, a string, a vector, a list, a hashtable, an alist, |
| 937 | value, t, numbers, strings, or further vectors, hashtables, alists, or | 935 | or a plist. Its elements will be further objects of these types. If |
| 938 | plists. If there are duplicate keys in an object, all but the last | 936 | there are duplicate keys in an object, all but the last one are |
| 939 | one are ignored. If STRING doesn't contain a valid JSON object, this | 937 | ignored. If STRING doesn't contain a valid JSON object, this function |
| 940 | function signals an error of type `json-parse-error'. | 938 | signals an error of type `json-parse-error'. |
| 941 | 939 | ||
| 942 | The arguments ARGS are a list of keyword/argument pairs: | 940 | The 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); |