[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master 3455192: JSON serialization: reject duplicate keys
From: |
Philipp Stephani |
Subject: |
[Emacs-diffs] master 3455192: JSON serialization: reject duplicate keys in hashtables |
Date: |
Sun, 24 Dec 2017 07:53:12 -0500 (EST) |
branch: master
commit 3455192777459a08a38b0adb311a76202e29f48d
Author: Philipp Stephani <address@hidden>
Commit: Philipp Stephani <address@hidden>
JSON serialization: reject duplicate keys in hashtables
* src/json.c (lisp_to_json_toplevel_1): Reject duplicate keys in
hashtables.
* test/src/json-tests.el (json-serialize/object-with-duplicate-keys):
Add unit tests.
---
src/json.c | 7 ++++++-
test/src/json-tests.el | 8 ++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/src/json.c b/src/json.c
index 689f6ac..c1daba1 100644
--- a/src/json.c
+++ b/src/json.c
@@ -352,7 +352,12 @@ lisp_to_json_toplevel_1 (Lisp_Object lisp, json_t **json)
/* We can't specify the length, so the string must be
null-terminated. */
check_string_without_embedded_nulls (key);
- int status = json_object_set_new (*json, SSDATA (key),
+ const char *key_str = SSDATA (key);
+ /* Reject duplicate keys. These are possible if the hash
+ table test is not `equal'. */
+ if (json_object_get (*json, key_str) != NULL)
+ wrong_type_argument (Qjson_value_p, lisp);
+ int status = json_object_set_new (*json, key_str,
lisp_to_json (HASH_VALUE (h,
i)));
if (status == -1)
/* FIXME: A failure here might also indicate that the
diff --git a/test/src/json-tests.el b/test/src/json-tests.el
index 9884e9a..5d9f6b3 100644
--- a/test/src/json-tests.el
+++ b/test/src/json-tests.el
@@ -52,6 +52,14 @@
(should (equal (json-serialize table)
"{\"abc\":[1,2,true],\"def\":null}"))))
+(ert-deftest json-serialize/object-with-duplicate-keys ()
+ (skip-unless (fboundp 'json-serialize))
+ (let ((table (make-hash-table :test #'eq)))
+ (puthash (copy-sequence "abc") [1 2 t] table)
+ (puthash (copy-sequence "abc") :null table)
+ (should (equal (hash-table-count table) 2))
+ (should-error (json-serialize table) :type 'wrong-type-argument)))
+
(ert-deftest json-parse-string/object ()
(skip-unless (fboundp 'json-parse-string))
(let ((input
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] master 3455192: JSON serialization: reject duplicate keys in hashtables,
Philipp Stephani <=