The define-json-expander Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

The define-json-expander Reference Manual

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.

Table of Contents


1 Introduction

define-json-expander

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.

Usage

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.

Example usage

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

2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 define-json-expander

Small facility for converting between JSON and CLOS

Author

Johan Sjölén

License

MIT

Source

define-json-expander.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


3.1.1 define-json-expander/define-json-expander.asd

Source

define-json-expander.asd.

Parent Component

define-json-expander (system).

ASDF Systems

define-json-expander.


3.1.2 define-json-expander/package.lisp

Source

define-json-expander.asd.

Parent Component

define-json-expander (system).

Packages

define-json-expander.


3.1.3 define-json-expander/define-json-expander.lisp

Dependency

package.lisp (file).

Source

define-json-expander.asd.

Parent Component

define-json-expander (system).

Public Interface
Internals

4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 define-json-expander

Source

package.lisp.

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Public Interface


Next: , Previous: , Up: Public Interface   [Contents][Index]

5.1.1 Special variables

Special Variable: *accessor-prefix*
Package

define-json-expander.

Source

define-json-expander.lisp.


5.1.2 Macros

Macro: define-json-expander (name direct-superclasses direct-slots &rest options)
Package

define-json-expander.

Source

define-json-expander.lisp.


5.2 Internals


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.1 Macros

Macro: define-json-decoder (name &rest slot/json-list)

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

Package

define-json-expander.

Source

define-json-expander.lisp.


Previous: , Up: Internals   [Contents][Index]

5.2.2 Ordinary functions

Function: clean-option (direct-slot option)

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.

Package

define-json-expander.

Source

define-json-expander.lisp.

Function: clean-options (slots &rest options)

Cleans several options from several slots.

Package

define-json-expander.

Source

define-json-expander.lisp.

Function: flatten-n-times (tree &optional n)

Flatten a tree by n levels of subtrees. n = 1 doesn’t flatten at all.

Package

define-json-expander.

Source

define-json-expander.lisp.

Function: get-prop (symbol list)
Package

define-json-expander.

Source

define-json-expander.lisp.

Function: group (source n)
Package

define-json-expander.

Source

define-json-expander.lisp.


Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts


Next: , Previous: , Up: Indexes   [Contents][Index]

A.3 Variables

Jump to:   *  
S  
Index Entry  Section

*
*accessor-prefix*: Public special variables

S
Special Variable, *accessor-prefix*: Public special variables

Jump to:   *  
S