From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Yoichi Nakayama Newsgroups: gmane.emacs.bugs Subject: bug#24252: 25.1; json.el doesn't distinguish null and empty object Date: Wed, 17 Aug 2016 23:54:02 +0900 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1471448185 970 195.159.176.226 (17 Aug 2016 15:36:25 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 17 Aug 2016 15:36:25 +0000 (UTC) To: 24252@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Aug 17 17:36:21 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ba2tA-0008V1-Uc for geb-bug-gnu-emacs@m.gmane.org; Wed, 17 Aug 2016 17:36:21 +0200 Original-Received: from localhost ([::1]:48095 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ba2t8-00011k-38 for geb-bug-gnu-emacs@m.gmane.org; Wed, 17 Aug 2016 11:36:18 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52057) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ba2sw-000103-Bf for bug-gnu-emacs@gnu.org; Wed, 17 Aug 2016 11:36:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ba2ss-0003Xa-9Z for bug-gnu-emacs@gnu.org; Wed, 17 Aug 2016 11:36:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:34479) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ba2ss-0003XV-60 for bug-gnu-emacs@gnu.org; Wed, 17 Aug 2016 11:36:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ba2ss-0003qa-2K for bug-gnu-emacs@gnu.org; Wed, 17 Aug 2016 11:36:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Yoichi Nakayama Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 17 Aug 2016 15:36:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 24252 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.147144811014719 (code B ref -1); Wed, 17 Aug 2016 15:36:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 17 Aug 2016 15:35:10 +0000 Original-Received: from localhost ([127.0.0.1]:60424 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ba2s2-0003pL-Jb for submit@debbugs.gnu.org; Wed, 17 Aug 2016 11:35:10 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:51318) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ba2FS-0002mu-6z for submit@debbugs.gnu.org; Wed, 17 Aug 2016 10:55:18 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ba2FJ-0003O3-7l for submit@debbugs.gnu.org; Wed, 17 Aug 2016 10:55:13 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:48699) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ba2FJ-0003Mf-5M for submit@debbugs.gnu.org; Wed, 17 Aug 2016 10:55:09 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42253) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ba2FG-0003M0-Li for bug-gnu-emacs@gnu.org; Wed, 17 Aug 2016 10:55:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ba2FB-0003JM-FP for bug-gnu-emacs@gnu.org; Wed, 17 Aug 2016 10:55:06 -0400 Original-Received: from mail2.asahi-net.or.jp ([202.224.39.198]:29154) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ba2FA-0003J8-Tk for bug-gnu-emacs@gnu.org; Wed, 17 Aug 2016 10:55:01 -0400 Original-Received: from gizaemon.local (ab154117.dynamic.ppp.asahi-net.or.jp [183.76.154.117]) by mail2.asahi-net.or.jp (Postfix) with ESMTP id A7A93C7FCC for ; Wed, 17 Aug 2016 23:54:58 +0900 (JST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (barebone) [generic] [fuzzy] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Mailman-Approved-At: Wed, 17 Aug 2016 11:35:09 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:122321 Archived-At: 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 >From 6b0ec686dab49be2309ed2dd349e31695b7cc6f2 Mon Sep 17 00:00:00 2001 From: Yoichi Nakayama 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