diff options
| author | Philipp Stephani | 2017-12-13 23:35:07 +0100 |
|---|---|---|
| committer | Philipp Stephani | 2017-12-19 18:20:55 +0100 |
| commit | db4f12e93f466832a5e5e1d512aff87ea90ef197 (patch) | |
| tree | 585a95c4cfb8cf765e8df7dc0fe623ed786b6444 /doc | |
| parent | 16813e6faa32b1741685ee429132251846d253a3 (diff) | |
| download | emacs-db4f12e93f466832a5e5e1d512aff87ea90ef197.tar.gz emacs-db4f12e93f466832a5e5e1d512aff87ea90ef197.zip | |
Allow JSON parser functions to return alists
* src/json.c (Fjson_parse_string, Fjson_parse_buffer): Give these
functions a keyword argument to specify the return type for JSON
objects.
(json_to_lisp): Convert objects to alists if requested.
(json_parse_object_type): New helper function to parse keyword
arguments.
* test/src/json-tests.el (json-parse-string/object): Add a unit test.
* doc/lispref/text.texi (Parsing JSON): Document new functionality.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispref/text.texi | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index 5b288d9750e..9592702ef1c 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi | |||
| @@ -4965,14 +4965,13 @@ represented using Lisp vectors. | |||
| 4965 | 4965 | ||
| 4966 | @item | 4966 | @item |
| 4967 | JSON has only one map type, the object. JSON objects are represented | 4967 | JSON has only one map type, the object. JSON objects are represented |
| 4968 | using Lisp hashtables. | 4968 | using Lisp hashtables or alists. |
| 4969 | 4969 | ||
| 4970 | @end itemize | 4970 | @end itemize |
| 4971 | 4971 | ||
| 4972 | @noindent | 4972 | @noindent |
| 4973 | Note that @code{nil} doesn't represent any JSON values: this is to | 4973 | Note that @code{nil} represents the empty JSON object, @code{@{@}}, |
| 4974 | avoid confusion, because @code{nil} could either represent | 4974 | not @code{null}, @code{false}, or an empty array, all of which are |
| 4975 | @code{null}, @code{false}, or an empty array, all of which are | ||
| 4976 | different JSON values. | 4975 | different JSON values. |
| 4977 | 4976 | ||
| 4978 | If some Lisp object can't be represented in JSON, the serialization | 4977 | If some Lisp object can't be represented in JSON, the serialization |
| @@ -4995,8 +4994,13 @@ The parsing functions will signal the following errors: | |||
| 4995 | 4994 | ||
| 4996 | Only top-level values (arrays and objects) can be serialized to | 4995 | Only top-level values (arrays and objects) can be serialized to |
| 4997 | JSON. The subobjects within these top-level values can be of any | 4996 | JSON. The subobjects within these top-level values can be of any |
| 4998 | type. Likewise, the parsing functions will only return vectors and | 4997 | type. Likewise, the parsing functions will only return vectors, |
| 4999 | hashtables. | 4998 | hashtables, and alists. |
| 4999 | |||
| 5000 | The parsing functions accept keyword arguments. Currently only one | ||
| 5001 | keyword argument, @code{:object-type}, is recognized; its value can be | ||
| 5002 | either @code{hash-table} to parse JSON objects as hashtables with | ||
| 5003 | string keys (the default) or @code{alist} to parse them as alists. | ||
| 5000 | 5004 | ||
| 5001 | @defun json-serialize object | 5005 | @defun json-serialize object |
| 5002 | This function returns a new Lisp string which contains the JSON | 5006 | This function returns a new Lisp string which contains the JSON |
| @@ -5008,12 +5012,12 @@ This function inserts the JSON representation of @var{object} into the | |||
| 5008 | current buffer before point. | 5012 | current buffer before point. |
| 5009 | @end defun | 5013 | @end defun |
| 5010 | 5014 | ||
| 5011 | @defun json-parse-string string | 5015 | @defun json-parse-string string &key (object-type @code{hash-table}) |
| 5012 | This function parses the JSON value in @var{string}, which must be a | 5016 | This function parses the JSON value in @var{string}, which must be a |
| 5013 | Lisp string. | 5017 | Lisp string. |
| 5014 | @end defun | 5018 | @end defun |
| 5015 | 5019 | ||
| 5016 | @defun json-parse-buffer | 5020 | @defun json-parse-buffer &key (object-type @code{hash-table}) |
| 5017 | This function reads the next JSON value from the current buffer, | 5021 | This function reads the next JSON value from the current buffer, |
| 5018 | starting at point. It moves point to the position immediately after | 5022 | starting at point. It moves point to the position immediately after |
| 5019 | the value if a value could be read and converted to Lisp; otherwise it | 5023 | the value if a value could be read and converted to Lisp; otherwise it |