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