The define-json-expander Reference Manual

Table of Contents

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 2.3 "Robert April" on Tue Jan 09 14:27:31 2018 GMT+0.


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

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

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

2 Systems

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


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

2.1 define-json-expander

Author

Johan Sjölén

License

MIT

Description

Small facility for converting between JSON and CLOS

Source

define-json-expander.asd (file)

Components

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

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


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.1 define-json-expander.asd

Location

define-json-expander.asd

Systems

define-json-expander (system)


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.2 define-json-expander/package.lisp

Parent

define-json-expander (system)

Location

package.lisp

Packages

define-json-expander


Previous: , Up: Lisp files   [Contents][Index]

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

Dependency

package.lisp (file)

Parent

define-json-expander (system)

Location

define-json-expander.lisp

Exported Definitions
Internal Definitions

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

4 Packages

Packages are listed by definition order.


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

4.1 define-json-expander

Source

package.lisp (file)

Use List

common-lisp

Exported Definitions
Internal Definitions

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

5 Definitions

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


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

5.1 Exported definitions


Next: , Previous: , Up: Exported definitions   [Contents][Index]

5.1.1 Special variables

Special Variable: *accessor-prefix*
Package

define-json-expander

Source

define-json-expander.lisp (file)


Previous: , Up: Exported definitions   [Contents][Index]

5.1.2 Macros

Macro: define-json-expander NAME DIRECT-SUPERCLASSES DIRECT-SLOTS &rest OPTIONS
Package

define-json-expander

Source

define-json-expander.lisp (file)


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

5.2 Internal definitions


Next: , Previous: , Up: Internal definitions   [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 (file)


Previous: , Up: Internal definitions   [Contents][Index]

5.2.2 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 (file)

Function: clean-options SLOTS &rest OPTIONS

Cleans several options from several slots.

Package

define-json-expander

Source

define-json-expander.lisp (file)

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

Function: get-prop SYMBOL LIST
Package

define-json-expander

Source

define-json-expander.lisp (file)

Function: group SOURCE N
Package

define-json-expander

Source

define-json-expander.lisp (file)


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

Appendix A Indexes


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

A.1 Concepts

Jump to:   D   F   L  
Index Entry  Section

D
define-json-expander.asd: The define-json-expander<dot>asd file
define-json-expander/define-json-expander.lisp: The define-json-expander/define-json-expander<dot>lisp file
define-json-expander/package.lisp: The define-json-expander/package<dot>lisp file

F
File, Lisp, define-json-expander.asd: The define-json-expander<dot>asd file
File, Lisp, define-json-expander/define-json-expander.lisp: The define-json-expander/define-json-expander<dot>lisp file
File, Lisp, define-json-expander/package.lisp: The define-json-expander/package<dot>lisp file

L
Lisp File, define-json-expander.asd: The define-json-expander<dot>asd file
Lisp File, define-json-expander/define-json-expander.lisp: The define-json-expander/define-json-expander<dot>lisp file
Lisp File, define-json-expander/package.lisp: The define-json-expander/package<dot>lisp file

Jump to:   D   F   L  

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

A.2 Functions

Jump to:   C   D   F   G   M  
Index Entry  Section

C
clean-option: Internal functions
clean-options: Internal functions

D
define-json-decoder: Internal macros
define-json-expander: Exported macros

F
flatten-n-times: Internal functions
Function, clean-option: Internal functions
Function, clean-options: Internal functions
Function, flatten-n-times: Internal functions
Function, get-prop: Internal functions
Function, group: Internal functions

G
get-prop: Internal functions
group: Internal functions

M
Macro, define-json-decoder: Internal macros
Macro, define-json-expander: Exported macros

Jump to:   C   D   F   G   M  

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

A.3 Variables

Jump to:   *  
S  
Index Entry  Section

*
*accessor-prefix*: Exported special variables

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

Jump to:   *  
S  

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

A.4 Data types

Jump to:   D   P   S  
Index Entry  Section

D
define-json-expander: The define-json-expander system
define-json-expander: The define-json-expander package

P
Package, define-json-expander: The define-json-expander package

S
System, define-json-expander: The define-json-expander system

Jump to:   D   P   S