unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] json.el: Make it customizable what nil encodes to.
@ 2015-10-17 14:41 Taylan Ulrich Bayırlı/Kammer
  0 siblings, 0 replies; only message in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2015-10-17 14:41 UTC (permalink / raw)
  To: emacs-devel

[-- 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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-10-17 14:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-17 14:41 [PATCH] json.el: Make it customizable what nil encodes to Taylan Ulrich Bayırlı/Kammer

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).