bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#24252: 25.1; json.el doesn't distinguish null and empty object


From: Yoichi Nakayama
Subject: bug#24252: 25.1; json.el doesn't distinguish null and empty object
Date: Sat, 20 Aug 2016 15:12:33 +0900

> Maybe the fix could be in json-pretty-print.

I agree that json-pretty-print should be responsible to the issue.
But I think

(let ((json-null 'NULL)) (json-encode (json-read-from-string "{}")))

is also a bug.
How about the following patch?

>From 24a11fc81ea283c7f999bbcf87ea0f2c01c1c24e Mon Sep 17 00:00:00 2001
From: Yoichi Nakayama <yoichi.nakayama@gmail.com>
Date: Sat, 20 Aug 2016 15:00:28 +0900
Subject: [PATCH] Distinguish empty json object and null

* lisp/json.el (json-encode-list, json-encode): Handle empty object
correctly when json-null is not nil.
(json-pretty-print): Bind json-null to distinguish empty object and null.
---
 lisp/json.el | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lisp/json.el b/lisp/json.el
index fdac8d9..a439f77 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -588,7 +588,7 @@ Please see the documentation of `json-object-type'
and `json-key-type'."
   "Return a JSON representation of LIST.
 Tries to DWIM: simple lists become JSON arrays, while alists and plists
 become JSON objects."
-  (cond ((null list)         "null")
+  (cond ((eq json-null list) "null")
         ((json-alist-p list) (json-encode-alist list))
         ((json-plist-p list) (json-encode-plist list))
         ((listp list)        (json-encode-array list))
@@ -700,12 +700,12 @@ Advances point just past JSON object."
         ((stringp object)      (json-encode-string object))
         ((keywordp object)     (json-encode-string
                                 (substring (symbol-name object) 1)))
-        ((symbolp object)      (json-encode-string
-                                (symbol-name object)))
         ((numberp object)      (json-encode-number object))
         ((arrayp object)       (json-encode-array object))
         ((hash-table-p object) (json-encode-hash-table object))
         ((listp object)        (json-encode-list object))
+        ((symbolp object)      (json-encode-string
+                                (symbol-name object)))
         (t                     (signal 'json-error (list object)))))

 ;; Pretty printing
@@ -722,6 +722,8 @@ Advances point just past JSON object."
     (let ((json-encoding-pretty-print t)
           ;; Ensure that ordering is maintained
           (json-object-type 'alist)
+          ;; Distinguish empty object and null
+          (json-null :json-null)
           (txt (delete-and-extract-region begin end)))
       (insert (json-encode (json-read-from-string txt))))))

-- 
2.8.1





reply via email to

[Prev in Thread] Current Thread [Next in Thread]