PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/src/yason.lisp

http://github.com/sykopomp/chillax
Lisp | 45 lines | 40 code | 5 blank | 0 comment | 0 complexity | 666e7ec7267d3154db6c62413de5e708 MD5 | raw file
 1(defpackage #:chillax.yason
 2  (:use :cl :chillax.core)
 3  (:export :yason-server))
 4(in-package :chillax.yason)
 5
 6(defgeneric parse-json-arrays-as-vectors-p (server))
 7(defgeneric parse-json-booleans-as-symbols-p (server))
 8(defgeneric parse-object-as-alist-p (server))
 9(defgeneric parse-object-key-fun (server))
10
11(defclass yason-server (standard-server)
12  ((array-as-vector-p
13    :initarg :array-as-vector-p
14    :reader parse-json-arrays-as-vectors-p)
15   (boolean-as-symbol-p
16    :initarg :boolean-as-symbol-p
17    :reader parse-json-booleans-as-symbols-p)
18   (object-as-alist-p
19    :initarg :object-as-alist-p
20    :reader parse-object-as-alist-p)
21   (parse-object-key-fun
22    :initarg :parse-object-key-fun
23    :reader parse-object-key-fun))
24  (:documentation
25   "YASON-SERVERs use Yason's JSON parser/encoder to automatically translate content going to/coming
26   from the associated CouchDB server.")
27  (:default-initargs
28      :array-as-vector-p nil
29    :boolean-as-symbol-p nil
30    :object-as-alist-p nil
31    :parse-object-key-fun #'identity))
32
33(defmethod data->json ((server yason-server) data &key)
34  (with-output-to-string (s)
35    (if (parse-object-as-alist-p server)
36        (yason:encode-alist data s)
37        (yason:encode data s))))
38
39(defmethod json->data ((server yason-server) json &key)
40  (yason:parse json
41               :object-key-fn (parse-object-key-fun server)
42               :object-as (if (parse-object-as-alist-p server) :alist :hash-table)
43               :json-booleans-as-symbols (parse-json-booleans-as-symbols-p server)
44               :json-arrays-as-vectors (parse-json-arrays-as-vectors-p server)))
45