unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] json: Add support for encoding structs
@ 2017-04-23 10:16 Vibhav Pant
  2017-04-24 13:25 ` Ted Zlatanov
  2017-04-24 13:45 ` Clément Pit-Claudel
  0 siblings, 2 replies; 16+ messages in thread
From: Vibhav Pant @ 2017-04-23 10:16 UTC (permalink / raw)
  To: emacs-devel@gnu.org

The following patch to json.el adds support for encoding record types
declared via cl-defstruct. It also allows the user to change the json
key for a struct field by using the `:json' keyword while declaring
a slot.

An example of the new json-encode:
ELISP> (cl-defstruct foo (f1 "foo") (f2 "bar" :json "field2"))
foo
ELISP> (json-encode (make-foo))
"{\"f1\":\"foo\",\"field2\":\"bar\"}"


---
diff --git a/lisp/json.el b/lisp/json.el
index 049c9b1951..cc09b46b7c 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -53,6 +53,7 @@
 ;;; Code:

 (require 'map)
+(require 'cl-lib)

 ;; Parameters

@@ -549,6 +550,33 @@ json-encode-hash-table
                 ""
               json--encoding-current-indentation))))

+;; Struct encoding
+(defun json-encode-struct (struct)
+  "Return a JSON representation of STRUCT."
+  (let* ((struct-type (type-of struct))
+         (slots-info (cdr (cl-struct-slot-info struct-type))))
+    (format "{%s%s}"
+            (json-join
+             (json--with-indentation
+              (mapcar #'(lambda (slot)
+                          (let* ((slot-name (car slot))
+                                 (opts (cddr slot))
+                                 (key (or (plist-get opts :json) slot-name)))
+                            (format (if json-encoding-pretty-print
+                                        "%s%s: %s"
+                                      "%s%s:%s")
+                                    json--encoding-current-indentation
+                                    (json-encode-key key)
+                                    (json-encode
+                                     (cl-struct-slot-value
struct-type slot-name
+                                                           struct)))))
+                      slots-info))
+             json-encoding-separator)
+            (if (or (not json-encoding-pretty-print)
+                    json-encoding-lisp-style-closings)
+                ""
+              json--encoding-current-indentation))))
+
 ;; List encoding (including alists and plists)

 (defun json-encode-alist (alist)
@@ -721,6 +749,7 @@ json-encode
         ((arrayp object)       (json-encode-array object))
         ((hash-table-p object) (json-encode-hash-table object))
         ((listp object)        (json-encode-list object))
+        ((cl-struct-p object)  (json-encode-struct object))
         (t                     (signal 'json-error (list object)))))

 ;; Pretty printing


-- 
Vibhav Pant
vibhavp@gmail.com



^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2017-04-27 12:31 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-23 10:16 [PATCH] json: Add support for encoding structs Vibhav Pant
2017-04-24 13:25 ` Ted Zlatanov
2017-04-24 17:54   ` Vibhav Pant
2017-04-24 19:17     ` Ted Zlatanov
2017-04-24 21:00       ` Ted Zlatanov
2017-04-24 21:43       ` Stefan Monnier
2017-04-25 14:26         ` Ted Zlatanov
2017-04-25 16:26           ` Vibhav Pant
2017-04-25 18:12             ` Ted Zlatanov
2017-04-26 12:13           ` Stefan Monnier
2017-04-27  6:50             ` Vibhav Pant
2017-04-27 12:31               ` Stefan Monnier
2017-04-25 12:27       ` Vibhav Pant
2017-04-24 22:52     ` Davis Herring
2017-04-25 12:40       ` Vibhav Pant
2017-04-24 13:45 ` Clément Pit-Claudel

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).