From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Simen =?UTF-8?Q?Heggest=C3=B8yl?= Newsgroups: gmane.emacs.bugs Subject: bug#21488: json-pretty-print-buffer works,but... Date: Sat, 03 Oct 2015 21:32:22 +0200 Message-ID: <87twq7hho9.fsf@gmail.com> References: <55F840B1.87A9B6.02747@m50-133.163.com> <87r3lcf4qm.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1443900813 10705 80.91.229.3 (3 Oct 2015 19:33:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 3 Oct 2015 19:33:33 +0000 (UTC) Cc: Junqi Cai , 21488@debbugs.gnu.org To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Oct 03 21:33:15 2015 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZiSYS-0007aG-TE for geb-bug-gnu-emacs@m.gmane.org; Sat, 03 Oct 2015 21:33:13 +0200 Original-Received: from localhost ([::1]:39781 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZiSYR-0004Hy-VM for geb-bug-gnu-emacs@m.gmane.org; Sat, 03 Oct 2015 15:33:11 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:36144) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZiSYO-0004Hi-4x for bug-gnu-emacs@gnu.org; Sat, 03 Oct 2015 15:33:09 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZiSYJ-0007oJ-4V for bug-gnu-emacs@gnu.org; Sat, 03 Oct 2015 15:33:08 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:36719) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZiSYJ-0007oF-0G for bug-gnu-emacs@gnu.org; Sat, 03 Oct 2015 15:33:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1ZiSYI-0003Kv-LK for bug-gnu-emacs@gnu.org; Sat, 03 Oct 2015 15:33:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Simen =?UTF-8?Q?Heggest=C3=B8yl?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 03 Oct 2015 19:33:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 21488 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 21488-submit@debbugs.gnu.org id=B21488.144390075112738 (code B ref 21488); Sat, 03 Oct 2015 19:33:02 +0000 Original-Received: (at 21488) by debbugs.gnu.org; 3 Oct 2015 19:32:31 +0000 Original-Received: from localhost ([127.0.0.1]:53923 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1ZiSXm-0003JM-8H for submit@debbugs.gnu.org; Sat, 03 Oct 2015 15:32:30 -0400 Original-Received: from mail-la0-f52.google.com ([209.85.215.52]:34021) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1ZiSXi-0003J5-MG for 21488@debbugs.gnu.org; Sat, 03 Oct 2015 15:32:27 -0400 Original-Received: by labzv5 with SMTP id zv5so107773235lab.1 for <21488@debbugs.gnu.org>; Sat, 03 Oct 2015 12:32:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=nqnDXLYMa+QrY1sEPoQop03vh+OI75T2gLSNYQ8f+2U=; b=1E21C6Xp1Ewcj5DWo38s0iY1crMBUilEc7M5aPCNnId/RDhi2q8NmGZGGMnA2S/Sog 8BTdyCvApiBEivj9GO1jqrjsEn7YArROuR7u4InCDkG4NW1KlQehsSgDdSQ7FRVfWm4n 8li3ApjbgX8o5JG8Dq0lr+5WRgzOBUmSEXibOWvK2Rya5qSIeUL9MSE79CipMGfZySnU a+hdHUH0uHVk07Q7KkrdWj3tPffNPdKxLs15zIRuTXlnkv+1vwzcAgUMCg2C54kxM6/+ kBy7h8ILzOFrh7DRNTibzEJ2m0zf9eSirte0wb8q+NV0+mT5WxtkGRw4VwUTdsgg7+lt 2S8g== X-Received: by 10.112.199.100 with SMTP id jj4mr8431228lbc.122.1443900744589; Sat, 03 Oct 2015 12:32:24 -0700 (PDT) Original-Received: from x240 (cm-84.210.143.4.getinternet.no. [84.210.143.4]) by smtp.gmail.com with ESMTPSA id as6sm2622560lbc.16.2015.10.03.12.32.23 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 03 Oct 2015 12:32:23 -0700 (PDT) In-Reply-To: (Stefan Monnier's message of "Sat, 03 Oct 2015 13:54:55 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x 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-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:107276 Archived-At: Stefan Monnier writes: > Don't we also need to reverse (tho differently) if json-object-type > is `plist'? > > > Stefan Yes, updated: >From cbe22b3bab6770512c43c2214ed11447d2a5b120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= Date: Sat, 3 Oct 2015 11:31:16 +0200 Subject: [PATCH] Maintain ordering of JSON object keys by default * lisp/json.el (json-object-type): Mention order handling in doc-string. (json--plist-reverse): New utility function. (json-read-object): Maintain ordering for alists and plists. (json-pretty-print): Ensure that ordering is maintained. * test/automated/json-tests.el (test-json-plist-reverse): New test for `json--plist-reverse'. (json-read-simple-alist): Update test to accommodate for changes in `json-read-object'. * etc/NEWS: Document the new behavior of the pretty printing functions. --- etc/NEWS | 5 +++++ lisp/json.el | 21 +++++++++++++++++++-- test/automated/json-tests.el | 13 ++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 26c478e..dbe0de3 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -314,6 +314,11 @@ standards. * Changes in Specialized Modes and Packages in Emacs 25.1 +** JSON +--- +*** `json-pretty-print' and `json-pretty-print-buffer' now maintain +the ordering of object keys by default. + ** You can recompute the VC state of a file buffer with `M-x vc-refresh-state' ** Prog mode has some support for multi-mode indentation. See `prog-indentation-context' and `prog-widen'. diff --git a/lisp/json.el b/lisp/json.el index daa0c94..e2c7cc7 100644 --- a/lisp/json.el +++ b/lisp/json.el @@ -57,7 +57,8 @@ (defvar json-object-type 'alist "Type to convert JSON objects to. Must be one of `alist', `plist', or `hash-table'. Consider let-binding -this around your call to `json-read' instead of `setq'ing it.") +this around your call to `json-read' instead of `setq'ing it. Ordering +is maintained for `alist' and `plist', but not for `hash-table'.") (defvar json-array-type 'vector "Type to convert JSON arrays to. @@ -136,6 +137,17 @@ without indentation.") 'not-plist))) (null list)) +(defun json--plist-reverse (plist) + "Return a copy of PLIST in reverse order. +Unlike `reverse', this keeps the property-value pairs intact." + (let (res) + (while plist + (let ((prop (pop plist)) + (val (pop plist))) + (push val res) + (push prop res))) + res)) + (defmacro json--with-indentation (body) `(let ((json--encoding-current-indentation (if json-encoding-pretty-print @@ -400,7 +412,10 @@ Please see the documentation of `json-object-type' and `json-key-type'." (signal 'json-object-format (list "," (json-peek)))))) ;; Skip over the "}" (json-advance) - elements)) + (pcase json-object-type + (`alist (nreverse elements)) + (`plist (json--plist-reverse elements)) + (_ elements)))) ;; Hash table encoding @@ -602,6 +617,8 @@ Advances point just past JSON object." (interactive "r") (atomic-change-group (let ((json-encoding-pretty-print t) + ;; Ensure that ordering is maintained + (json-object-type 'alist) (txt (delete-and-extract-region begin end))) (insert (json-encode (json-read-from-string txt)))))) diff --git a/test/automated/json-tests.el b/test/automated/json-tests.el index fd89b7a..d1b7a2f 100644 --- a/test/automated/json-tests.el +++ b/test/automated/json-tests.el @@ -22,15 +22,22 @@ (require 'ert) (require 'json) +(ert-deftest test-json-plist-reverse () + (should (equal (json--plist-reverse '()) '())) + (should (equal (json--plist-reverse '(:a 1)) '(:a 1))) + (should (equal (json--plist-reverse '(:a 1 :b 2 :c 3)) + '(:c 3 :b 2 :a 1)))) + (ert-deftest json-encode-simple-alist () (should (equal (json-encode '((a . 1) (b . 2))) "{\"a\":1,\"b\":2}"))) (ert-deftest json-read-simple-alist () - (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}") - '((b . 2) - (a . 1))))) + (let ((json-object-type 'alist)) + (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}") + '((a . 1) + (b . 2)))))) (ert-deftest json-encode-string-with-special-chars () (should (equal (json-encode-string "a\n\fb") -- 2.5.3