[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] json.el: Make it customizable what nil encodes to.
From: |
Taylan Ulrich Bayırlı/Kammer |
Subject: |
[PATCH] json.el: Make it customizable what nil encodes to. |
Date: |
Sat, 17 Oct 2015 16:41:05 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) |
This is useful for an ELPA package I will be contributing:
'json-sexp-mode', which allows to transparently edit JSON files as
s-expressions.
>From b2b93379489585c74b7222da7bf10177179aac9d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taylan=20Ulrich=20Bay=C4=B1rl=C4=B1/Kammer?=
<address@hidden>
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
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH] json.el: Make it customizable what nil encodes to.,
Taylan Ulrich Bayırlı/Kammer <=