From: taylanbayirli@gmail.com (Taylan Ulrich Bayırlı/Kammer)
To: emacs-devel@gnu.org
Subject: [PATCH] json.el: Make it customizable what nil encodes to.
Date: Sat, 17 Oct 2015 16:41:05 +0200 [thread overview]
Message-ID: <87eggty2v2.fsf@T420.taylan> (raw)
[-- Attachment #1: Type: text/plain, Size: 142 bytes --]
This is useful for an ELPA package I will be contributing:
'json-sexp-mode', which allows to transparently edit JSON files as
s-expressions.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-json.el-Make-it-customizable-what-nil-encodes-to.patch --]
[-- Type: text/x-diff, Size: 2070 bytes --]
From b2b93379489585c74b7222da7bf10177179aac9d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taylan=20Ulrich=20Bay=C4=B1rl=C4=B1/Kammer?=
<taylanbayirli@gmail.com>
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
reply other threads:[~2015-10-17 14:41 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87eggty2v2.fsf@T420.taylan \
--to=taylanbayirli@gmail.com \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.