Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the define-json-expander Reference Manual, generated automatically by Declt version 4.0 beta 2 "William Riker" on Wed Jun 15 04:25:06 2022 GMT+0.
Next: Systems, Previous: The define-json-expander Reference Manual, Up: The define-json-expander Reference Manual [Contents][Index]
Small 'library' for writing simple interfaces between JSON files and CLOS. The `define-json-expander' macro creates both a CLOS-class and a decoder function named DECODE-* where * = name (this can be changed with accessor-prefix) for simple use. define-json-expander is supposed to be used in tandem with cl-json as its decode-functions expects cl-json list-decoded JSON output as input.
This library is written by Johan Sjölén and is licensed under the MIT license.
define-json-expander has the same syntax as defclass with two new slot-options: :json-prop :json-decoder. :json-prop expects a keyword that represents a property name. :json-decoder expects a function of one argument whose output will be used as the value for the slot. The input to the :json-decoder function is the value of the json property.
All accessors (if not already defined) are on the form *-OF
If no :json-prop is defined then the slot-name as a keyword will be taken automatically.
All unknown/unusued data will be put in the REST slot of the resulting object.
The following JSON is taken from json.org/example.html
JSON
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
Code
(define-json-expander menu ()
((menu :json-decoder #'decode-menu-slots)))
(define-json-expander menu-slots ()
((id)
(value)
(popup :json-decoder #'decode-menuitem)))
(define-json-expander menuitem ()
((menuitem :json-decoder (lambda (x) (loop for obj in x collect (decode-button obj))))))
(define-json-expander button ()
((value) (onclick)))
`
Next: Files, Previous: Introduction, Up: The define-json-expander Reference Manual [Contents][Index]
The main system appears first, followed by any subsystem dependency.
Small facility for converting between JSON and CLOS
Johan Sjölén
MIT
Next: Packages, Previous: Systems, Up: The define-json-expander Reference Manual [Contents][Index]
Files are sorted by type and then listed depth-first from the systems components trees.
Next: define-json-expander/package.lisp, Previous: Lisp, Up: Lisp [Contents][Index]
define-json-expander (system).
Next: define-json-expander/define-json-expander.lisp, Previous: define-json-expander/define-json-expander.asd, Up: Lisp [Contents][Index]
define-json-expander (system).
Previous: define-json-expander/package.lisp, Up: Lisp [Contents][Index]
package.lisp (file).
define-json-expander (system).
Next: Definitions, Previous: Files, Up: The define-json-expander Reference Manual [Contents][Index]
Packages are listed by definition order.
common-lisp.
Next: Indexes, Previous: Packages, Up: The define-json-expander Reference Manual [Contents][Index]
Definitions are sorted by export status, category, package, and then by lexicographic order.
Next: Internals, Previous: Definitions, Up: Definitions [Contents][Index]
Next: Macros, Previous: Public Interface, Up: Public Interface [Contents][Index]
Previous: Special variables, Up: Public Interface [Contents][Index]
Previous: Public Interface, Up: Definitions [Contents][Index]
Next: Ordinary functions, Previous: Internals, Up: Internals [Contents][Index]
Generates a decoder function with the name decode-‘name’
The ‘slot/json-list’ is of the form (slot-name json-property-name &optional decoder-function) where
slot-name refers to the slot’s initarg
decoder-function refers to the function which will be called upon a structure if so provided
The decoder automatically takes all unused JSON-properties and puts them into the rest slot
Example definition and expansion:
(define-4chan-decoder post
(:no :no)
(:name :name)
(:e-mail :email)
(:body :com)
(:date :time))
(DEFUN DECODE-POST (POST)
"Takes a JSON document in list form and decodes it into a CLOS POST object"
(FLET ((P (KEY)
(CDR (ASSOC KEY POST))))
(MAKE-INSTANCE ’POST :NO (P :NO) :NAME (P :NAME) :E-MAIL (P :EMAIL) :BODY
(P :COM) :DATE (P :TIME) :REST
(REMOVE-IF
(LAMBDA (ELT)
(MEMBER (CAR ELT)
’((:NO :NO) (:NAME :NAME) (:E-MAIL :EMAIL)
(:BODY :COM) (:DATE :TIME))
:KEY #’CADR :TEST #’EQ))
POST))))
Returns two values, the first being the ‘direct-slot’ with the ‘option’ and its value stripped and the second being a list consisting of the name of the slot and the found prop and value. If such a prop isn’t found then the second element is NIL.
Cleans several options from several slots.
Flatten a tree by n levels of subtrees. n = 1 doesn’t flatten at all.
Previous: Definitions, Up: The define-json-expander Reference Manual [Contents][Index]
Jump to: | C D F G M |
---|
Jump to: | C D F G M |
---|
Next: Data types, Previous: Functions, Up: Indexes [Contents][Index]
Jump to: | *
S |
---|
Index Entry | Section | ||
---|---|---|---|
| |||
* | |||
*accessor-prefix* : | Public special variables | ||
| |||
S | |||
Special Variable, *accessor-prefix* : | Public special variables | ||
|
Jump to: | *
S |
---|
Jump to: | D F P S |
---|
Jump to: | D F P S |
---|