From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich =?utf-8?Q?Bay=C4=B1rl=C4=B1?= =?utf-8?Q?=2FKammer?=) Newsgroups: gmane.emacs.devel Subject: [PATCH] json.el: Make it customizable what nil encodes to. Date: Sat, 17 Oct 2015 16:41:05 +0200 Message-ID: <87eggty2v2.fsf@T420.taylan> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1445092893 11907 80.91.229.3 (17 Oct 2015 14:41:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 17 Oct 2015 14:41:33 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Oct 17 16:41:33 2015 Return-path: Envelope-to: ged-emacs-devel@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 1ZnSfq-0005ML-MB for ged-emacs-devel@m.gmane.org; Sat, 17 Oct 2015 16:41:30 +0200 Original-Received: from localhost ([::1]:58502 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnSfq-0003zV-0m for ged-emacs-devel@m.gmane.org; Sat, 17 Oct 2015 10:41:30 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56192) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnSfV-0003z3-8V for emacs-devel@gnu.org; Sat, 17 Oct 2015 10:41:10 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZnSfU-0004RU-CN for emacs-devel@gnu.org; Sat, 17 Oct 2015 10:41:09 -0400 Original-Received: from mail-wi0-x231.google.com ([2a00:1450:400c:c05::231]:33807) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnSfU-0004RO-6I for emacs-devel@gnu.org; Sat, 17 Oct 2015 10:41:08 -0400 Original-Received: by wicgb1 with SMTP id gb1so42892093wic.1 for ; Sat, 17 Oct 2015 07:41:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-type; bh=i2bYQM+CQpDkLeDcdK6uLxOUU1gk57bUaypIPIQ4Sf0=; b=gZTGfSodCeKxpsTJjInbBSc7krMi+v0h1yzjp3X7NZ/4I7xnZF0pPlVaGrkaLuTnxt KIXUVg9sSc8XomGVZyc27LGAXuS1x4SGTGgpMUFuQ6kYFKG5qa9Ct/+nLMkvoIJjs1oB 26eurq81aQlAP19dUmamPbx5wexQQpauMsiTcbPaieVbBI28MINTNFxfV5pfouLh2dHW O70csbKiYfqd66s0WS9j7SIPhtuXQZ+oXjBTTNdRGaVgHHC7WSAaS/nQN2X2hA3kIy0k 3cKApCRal2j4NGqdLhPaCE6D9BmB1HDv0lMfpkqfewjAwVzqv+rk/Q7YDmotV/vg3ZFI Qaqg== X-Received: by 10.195.13.164 with SMTP id ez4mr22780497wjd.112.1445092867660; Sat, 17 Oct 2015 07:41:07 -0700 (PDT) Original-Received: from T420.taylan ([2a02:908:c32:4740:221:ccff:fe66:68f0]) by smtp.gmail.com with ESMTPSA id ly4sm28679911wjb.4.2015.10.17.07.41.06 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Oct 2015 07:41:06 -0700 (PDT) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c05::231 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:191832 Archived-At: --=-=-= Content-Type: text/plain This is useful for an ELPA package I will be contributing: 'json-sexp-mode', which allows to transparently edit JSON files as s-expressions. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-json.el-Make-it-customizable-what-nil-encodes-to.patch >From b2b93379489585c74b7222da7bf10177179aac9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Taylan=20Ulrich=20Bay=C4=B1rl=C4=B1/Kammer?= Date: Sat, 17 Oct 2015 16:36:26 +0200 Subject: [PATCH] json.el: Make it customizable what nil encodes to. --- lisp/json.el | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lisp/json.el b/lisp/json.el index e2c7cc7..d84e1ac 100644 --- a/lisp/json.el +++ b/lisp/json.el @@ -93,6 +93,18 @@ If this has the same value as `json-false', 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-nil nil + "How `nil' should be turned into a JSON value. +Possible values and their result: + + nil -> null + array -> [] + object -> {} + string -> \"nil\" + +Note that this is only used when encoding, and not affected by +`json-object-type' or `json-array-type' in any way.") + (defvar json-encoding-separator "," "Value to use as an element separator when encoding.") @@ -265,6 +277,15 @@ representation will be parsed correctly." "Return a JSON representation of NUMBER." (format "%s" number)) +;;; Nil + +(defun json-encode-nil () + "Return the JSON representation of `nil'. See `json-nil'." + (cond ((eq json-nil nil) "null") + ((eq json-nil 'array) "[]") + ((eq json-nil 'object) "{}") + ((eq json-nil 'string) (symbol-name nil)))) + ;;; Strings (defvar json-special-chars @@ -594,6 +615,8 @@ 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)) + ;; Don't forget that `nil' is a symbol too, so do this early. + ((null object) (json-encode-nil)) ((stringp object) (json-encode-string object)) ((keywordp object) (json-encode-string (substring (symbol-name object) 1))) -- 2.5.0 --=-=-=--