unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#24252: 25.1; json.el doesn't distinguish null and empty object
@ 2016-08-17 14:54 Yoichi Nakayama
  2016-08-19  2:06 ` Dmitry Gutov
  2018-05-19  6:52 ` Damien Cassou
  0 siblings, 2 replies; 20+ messages in thread
From: Yoichi Nakayama @ 2016-08-17 14:54 UTC (permalink / raw)
  To: 24252

Hello,

When json-pretty-print applied to "{}", it is
unexpectedly converted to "null".
This is caused by internal representations of null
and empty object are the same:
  (json-read-from-string "{}") ; => nil
  (json-read-from-string "null") ; => nil

;; Based on the fact that
;;   (let ((json-object-type 'hash-table))
;;     (json-read-from-string "{}"))
;; is non-nil, there was a workaround in the past.
;; The current json-pretty-print bind it to alist
;; to keep ordering of elements, so the technique
;; no longer works.

Following patch make json.el to treat empty object
and null differently and solve the issue.

--
Yoichi Nakayama
\f
From 6b0ec686dab49be2309ed2dd349e31695b7cc6f2 Mon Sep 17 00:00:00 2001
From: Yoichi Nakayama <yoichi.nakayama@gmail.com>
Date: Wed, 17 Aug 2016 01:18:56 +0900
Subject: [PATCH] Distinguish empty json object and null

* lisp/json.el (json-empty-object): New variable.
(json-new-object, json-add-to-object, json-read-object, json-encode):
Use it.
---
 lisp/json.el | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/lisp/json.el b/lisp/json.el
index fdac8d9..ec2c06a 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -89,6 +89,12 @@ If this has the same value as `json-null', you might not be able to tell
 the difference between `false' and `null'.  Consider let-binding this
 around your call to `json-read' instead of `setq'ing it.")
 
+(defvar json-empty-object :json-empty-object
+  "Value to use when reading JSON `{}'.
+If this has the same value as `json-null', you might not be able to tell
+the difference between `{}' and `null'.  Consider let-binding this
+around your call to `json-read' instead of `setq'ing it.")
+
 (defvar json-null nil
   "Value to use when reading JSON `null'.
 If this has the same value as `json-false', you might not be able to
@@ -442,7 +448,7 @@ Please see the documentation of `json-object-type'."
   (cond ((eq json-object-type 'hash-table)
          (make-hash-table :test 'equal))
         (t
-         ())))
+         json-empty-object)))
 
 (defun json-add-to-object (object key value)
   "Add a new KEY -> VALUE association to OBJECT.
@@ -454,7 +460,9 @@ Please see the documentation of `json-object-type' and `json-key-type'."
              (cdr (assq json-object-type '((hash-table . string)
                                            (alist . symbol)
                                            (plist . keyword))))
-           json-key-type)))
+           json-key-type))
+        (object (cond ((eq object json-empty-object) ())
+                      (t object))))
     (setq key
           (cond ((eq json-key-type 'string)
                  key)
@@ -501,10 +509,12 @@ Please see the documentation of `json-object-type' and `json-key-type'."
           (signal 'json-object-format (list "," (json-peek))))))
     ;; Skip over the "}"
     (json-advance)
-    (pcase json-object-type
-      (`alist (nreverse elements))
-      (`plist (json--plist-reverse elements))
-      (_ elements))))
+    (cond ((eq elements json-empty-object) elements)
+          (t
+           (pcase json-object-type
+             (`alist (nreverse elements))
+             (`plist (json--plist-reverse elements))
+             (_ elements))))))
 
 ;; Hash table encoding
 
@@ -697,6 +707,7 @@ Advances point just past JSON object."
   "Return a JSON representation of OBJECT as a string."
   (cond ((memq object (list t json-null json-false))
          (json-encode-keyword object))
+        ((eq object json-empty-object) "{}")
         ((stringp object)      (json-encode-string object))
         ((keywordp object)     (json-encode-string
                                 (substring (symbol-name object) 1)))
-- 
2.8.1






^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2018-06-14  9:04 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-17 14:54 bug#24252: 25.1; json.el doesn't distinguish null and empty object Yoichi Nakayama
2016-08-19  2:06 ` Dmitry Gutov
2016-08-19 23:45   ` Yoichi Nakayama
2016-08-20  0:52     ` Dmitry Gutov
2016-08-20  6:12       ` Yoichi Nakayama
2016-08-21  1:30         ` Yoichi Nakayama
2016-08-21  3:42         ` Dmitry Gutov
2016-08-21 12:11           ` Yoichi Nakayama
2016-08-21 13:32             ` Yoichi Nakayama
2016-08-21 15:06               ` Yoichi Nakayama
2016-08-27  0:05                 ` Dmitry Gutov
2018-05-17 14:39                 ` Damien Cassou
2018-05-19  6:52 ` Damien Cassou
2018-05-28 15:21   ` Nicolas Petton
2018-06-11 13:36     ` Damien Cassou
2018-06-12 17:14       ` Eli Zaretskii
2018-06-13  7:13         ` Damien Cassou
2018-06-13 13:05           ` Nicolas Petton
2018-06-13 16:55             ` Eli Zaretskii
2018-06-14  9:04               ` Nicolas Petton

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).