The json-mop Reference Manual

Table of Contents

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

The json-mop Reference Manual

This is the json-mop Reference Manual, generated automatically by Declt version 2.4 "Will Decker" on Wed Jun 20 12:03:04 2018 GMT+0.


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

1 Introduction

JSON-MOP

Quicklisp dist

Introduction

JSON-MOP is a small library aiming to cut down time spent moving data between CLOS and JSON objects. It depends on YASON and it should be possible to use it alongside straight calls to functions from YASON.

Quick Start

To use JSON-MOP, define your classes with the class option (:metaclass json-serializable-class). For slots that you want to appear in the JSON representation of your class, add the slot option :json-key with the string to use as the attribute name. The option :json-type defaults to :any, but you can control how each slot value is transformed to and from JSON with one of the following:

JSON type specifiers

Type | Remarks --------------|-------------------------------------------- :any | Guesses the way to encode and decode the value :string | Enforces a string value :number | Enforces a number value :hash-table | Enforces a hash table value :vector | Enforces a vector value :list | Enforces a list value :bool | Maps T and NIL with true and false <symbol> | Uses a (:metaclass json-serializable-class) class definition to direct the transformation of the value

Homogeneous sequences

In addition, the type specifier may be a list of two elements, first element is one of :list, :vector; the second is any JSON type specifier that is to be applied to the elements of the list.

NIL and null semantics

JSON null is treated as an unbound slot in CLOS. Unbound slots are ignored when encoding objects, unless *encode-unbound-slots* is bound to T, in which case they are represented as JSON null.

Slots bound to NIL with JSON types other :bool will signal an error, but this may change in the future.

Encoding and decoding JSON

Turning an object into JSON is done with the yason:encode generic function. Turning it back into an object is slightly more involved, using json-to-clos on a stream, string or hash table; a class name; and optional initargs for the class. Values decoded from the JSON will override values specified in the initargs.

Example

First, define your classes:

(defclass book ()
  ((title :initarg :title
          :json-type :string
          :json-key "title")
   (published-year :initarg :year
         :json-type :number
         :json-key "year_published")
   (fiction :initarg :fiction
            :json-type :bool
            :json-key "is_fiction"))
  (:metaclass json-serializable-class))

(defclass author ()
  ((name :initarg :name
         :json-type :string
         :json-key "name")
   (birth-year :initarg :year
               :json-type :number
               :json-key "year_birth")
   (bibliography :initarg :bibliography
                 :json-type (:list book)
                 :json-key "bibliography"))
  (:metaclass json-serializable-class))

Let's try creating an instance:

(defparameter *author*
  (make-instance 'author
                 :name "Mark Twain"
                 :year 1835
                 :bibliography
                 (list
                  (make-instance 'book
                                 :title "The Gilded Age: A Tale of Today"
                                 :year 1873
                                 :fiction t)
                  (make-instance 'book
                                 :title "Life on the Mississippi"
                                 :year 1883
                                 :fiction nil)
                  (make-instance 'book
                                 :title "Adventures of Huckleberry Finn"
                                 :year 1884
                                 :fiction t))))

To turn it into JSON, encode it:

(encode *author*)

This will print the following:

{"name":"Mark Twain","year_birth":1835,"bibliography":[{"title":"The Gilded Age: A Tale of Today","year_published":1873,"is_fiction":true},{"title":"Life on the Mississippi","year_published":1883,"is_fiction":false},{"title":"Adventures of Huckleberry Finn","year_published":1884,"is_fiction":true}]}

The same can be turned back into a CLOS object with (json-to-clos input 'author)

Licence

Copyright (c) 2015 Grim Schjetne

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


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 json-mop

Author

Grim Schjetne

License

MIT

Description

A metaclass for bridging CLOS and JSON

Dependencies
Source

json-mop.asd (file)

Component

src (module)


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

3 Modules

Modules are listed depth-first from the system components tree.


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

3.1 json-mop/src

Parent

json-mop (system)

Location

src/

Components

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

4 Files

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


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

4.1 Lisp


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

4.1.1 json-mop.asd

Location

json-mop.asd

Systems

json-mop (system)


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

4.1.2 json-mop/src/package.lisp

Parent

src (module)

Location

src/package.lisp

Packages

json-mop


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

4.1.3 json-mop/src/conditions.lisp

Dependency

package.lisp (file)

Parent

src (module)

Location

src/conditions.lisp

Exported Definitions
Internal Definitions

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

4.1.4 json-mop/src/json-mop.lisp

Dependency

conditions.lisp (file)

Parent

src (module)

Location

src/json-mop.lisp

Exported Definitions
Internal Definitions

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

4.1.5 json-mop/src/to-lisp.lisp

Dependency

json-mop.lisp (file)

Parent

src (module)

Location

src/to-lisp.lisp

Exported Definitions

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

4.1.6 json-mop/src/to-json.lisp

Dependency

to-lisp.lisp (file)

Parent

src (module)

Location

src/to-json.lisp

Exported Definitions
Internal Definitions

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

5 Packages

Packages are listed by definition order.


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

5.1 json-mop

Source

package.lisp (file)

Use List

common-lisp

Exported Definitions
Internal Definitions

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

6 Definitions

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


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

6.1 Exported definitions


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

6.1.1 Generic functions

Generic Function: json-to-clos INPUT CLASS &rest INITARGS
Package

json-mop

Source

to-lisp.lisp (file)

Methods
Method: json-to-clos (INPUT string) CLASS &rest INITARGS
Method: json-to-clos (INPUT pathname) CLASS &rest INITARGS
Method: json-to-clos (INPUT stream) CLASS &rest INITARGS
Method: json-to-clos (INPUT hash-table) CLASS &rest INITARGS
Generic Function: json-type CONDITION
Package

json-mop

Methods
Method: json-type (JSON-SERIALIZABLE-SLOT json-serializable-slot)

automatically generated reader method

Source

json-mop.lisp (file)

Method: json-type (CONDITION json-type-error)
Source

conditions.lisp (file)

Generic Function: no-values-class CONDITION
Package

json-mop

Methods
Method: no-values-class (CONDITION no-values-parsed)
Source

conditions.lisp (file)

Generic Function: no-values-hash-table CONDITION
Package

json-mop

Methods
Method: no-values-hash-table (CONDITION no-values-parsed)
Source

conditions.lisp (file)

Generic Function: slot-name CONDITION
Package

json-mop

Methods
Method: slot-name (CONDITION slot-not-serializable)
Source

conditions.lisp (file)

Generic Function: to-json-value VALUE JSON-TYPE

Turns a VALUE into a form appropriate for consumption by Yason

Package

json-mop

Source

to-json.lisp (file)

Methods
Method: to-json-value VALUE (JSON-TYPE symbol)
Method: to-json-value (VALUE sequence) (JSON-TYPE cons)

Return the homogeneous sequence VALUE

Method: to-json-value (VALUE null) (JSON-TYPE (eql bool))

Return the boolean false

Method: to-json-value VALUE (JSON-TYPE (eql bool))

Return the boolean true

Method: to-json-value (VALUE null) (JSON-TYPE (eql list))

Return the empty list VALUE

Method: to-json-value (VALUE list) (JSON-TYPE (eql list))

Return the list VALUE

Method: to-json-value (VALUE vector) (JSON-TYPE (eql vector))

Return the vector VALUE

Method: to-json-value (VALUE hash-table) (JSON-TYPE (eql hash-table))

Return the hash-table VALUE

Method: to-json-value (VALUE number) (JSON-TYPE (eql number))

Return the number VALUE

Method: to-json-value (VALUE string) (JSON-TYPE (eql string))

Return the string VALUE

Method: to-json-value (VALUE null) JSON-TYPE
Method: to-json-value VALUE (JSON-TYPE (eql any))

When the JSON type is :ANY, Pass the VALUE unchanged

Generic Function: to-lisp-value VALUE JSON-TYPE

Turns a value passed by Yason into the appropriate Lisp type as specified by JSON-TYPE

Package

json-mop

Source

to-lisp.lisp (file)

Methods
Method: to-lisp-value (VALUE hash-table) (JSON-TYPE symbol)

Return the CLOS object VALUE

Method: to-lisp-value (VALUE vector) (JSON-TYPE cons)

Return the homogeneous sequence VALUE

Method: to-lisp-value VALUE (JSON-TYPE (eql bool))

Return the boolean VALUE

Method: to-lisp-value (VALUE vector) (JSON-TYPE (eql list))

Return the list VALUE

Method: to-lisp-value (VALUE vector) (JSON-TYPE (eql vector))

Return the vector VALUE

Method: to-lisp-value (VALUE hash-table) (JSON-TYPE (eql hash-table))

Return the hash-table VALUE

Method: to-lisp-value (VALUE number) (JSON-TYPE (eql number))

Return the number VALUE

Method: to-lisp-value (VALUE string) (JSON-TYPE (eql string))

Return the string VALUE

Method: to-lisp-value VALUE (JSON-TYPE (eql any))

When the JSON type is :ANY, Pass the VALUE unchanged

Method: to-lisp-value (VALUE (eql null)) JSON-TYPE

When the value is JSON null, signal NULL-VALUE error


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

6.1.2 Conditions

Condition: json-type-error ()
Package

json-mop

Source

conditions.lisp (file)

Direct superclasses

error (condition)

Direct subclasses
Direct methods

json-type (method)

Direct slots
Slot: json-type
Initargs

:json-type

Readers

json-type (generic function)

Condition: no-values-parsed ()
Package

json-mop

Source

conditions.lisp (file)

Direct superclasses

warning (condition)

Direct methods
Direct slots
Slot: hash-table
Initargs

:hash-table

Readers

no-values-hash-table (generic function)

Slot: class-name
Initargs

:class-name

Readers

no-values-class (generic function)

Condition: null-value ()
Package

json-mop

Source

conditions.lisp (file)

Direct superclasses

json-type-error (condition)

Condition: slot-not-serializable ()
Package

json-mop

Source

conditions.lisp (file)

Direct superclasses

warning (condition)

Direct methods

slot-name (method)

Direct slots
Slot: slot-name
Initargs

:slot-name

Readers

slot-name (generic function)


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

6.1.3 Classes

Class: json-serializable ()
Package

json-mop

Source

json-mop.lisp (file)

Direct superclasses

standard-object (class)

Direct methods

encode (method)

Class: json-serializable-class ()
Package

json-mop

Source

json-mop.lisp (file)

Direct superclasses

standard-class (class)

Direct methods
  • compute-class-precedence-list (method)
  • direct-slot-definition-class (method)
  • validate-superclass (method)
  • validate-superclass (method)

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

6.2 Internal definitions


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

6.2.1 Special variables

Special Variable: *encode-unbound-slots*
Package

json-mop

Source

to-json.lisp (file)


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

6.2.2 Functions

Function: read-eval-query ()
Package

json-mop

Source

conditions.lisp (file)


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

6.2.3 Generic functions

Generic Function: json-key-name OBJECT
Package

json-mop

Methods
Method: json-key-name (SLOT standard-direct-slot-definition)
Source

json-mop.lisp (file)

Method: json-key-name (JSON-SERIALIZABLE-SLOT json-serializable-slot)

automatically generated reader method

Source

json-mop.lisp (file)


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

6.2.4 Conditions

Condition: null-in-homogeneous-sequence ()
Package

json-mop

Source

conditions.lisp (file)

Direct superclasses

json-type-error (condition)


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

6.2.5 Classes

Class: homogeneous-sequence-intermediate-class ()
Package

json-mop

Source

to-json.lisp (file)

Direct superclasses

standard-object (class)

Direct methods

encode (method)

Direct slots
Slot: values
Initargs

:values

Slot: sequence-json-type
Initargs

:sequence-json-type

Slot: element-json-type
Initargs

:element-json-type

Class: json-serializable-slot ()
Package

json-mop

Source

json-mop.lisp (file)

Direct superclasses

standard-direct-slot-definition (class)

Direct methods
Direct slots
Slot: json-key
Initargs

:json-key

Readers

json-key-name (generic function)

Slot: json-type
Initargs

:json-type

Initform

:any

Readers

json-type (generic function)


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

Appendix A Indexes


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

A.1 Concepts

Jump to:   F   J   L   M  
Index Entry  Section

F
File, Lisp, json-mop.asd: The json-mop<dot>asd file
File, Lisp, json-mop/src/conditions.lisp: The json-mop/src/conditions<dot>lisp file
File, Lisp, json-mop/src/json-mop.lisp: The json-mop/src/json-mop<dot>lisp file
File, Lisp, json-mop/src/package.lisp: The json-mop/src/package<dot>lisp file
File, Lisp, json-mop/src/to-json.lisp: The json-mop/src/to-json<dot>lisp file
File, Lisp, json-mop/src/to-lisp.lisp: The json-mop/src/to-lisp<dot>lisp file

J
json-mop.asd: The json-mop<dot>asd file
json-mop/src: The json-mop/src module
json-mop/src/conditions.lisp: The json-mop/src/conditions<dot>lisp file
json-mop/src/json-mop.lisp: The json-mop/src/json-mop<dot>lisp file
json-mop/src/package.lisp: The json-mop/src/package<dot>lisp file
json-mop/src/to-json.lisp: The json-mop/src/to-json<dot>lisp file
json-mop/src/to-lisp.lisp: The json-mop/src/to-lisp<dot>lisp file

L
Lisp File, json-mop.asd: The json-mop<dot>asd file
Lisp File, json-mop/src/conditions.lisp: The json-mop/src/conditions<dot>lisp file
Lisp File, json-mop/src/json-mop.lisp: The json-mop/src/json-mop<dot>lisp file
Lisp File, json-mop/src/package.lisp: The json-mop/src/package<dot>lisp file
Lisp File, json-mop/src/to-json.lisp: The json-mop/src/to-json<dot>lisp file
Lisp File, json-mop/src/to-lisp.lisp: The json-mop/src/to-lisp<dot>lisp file

M
Module, json-mop/src: The json-mop/src module

Jump to:   F   J   L   M  

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

A.2 Functions

Jump to:   F   G   J   M   N   R   S   T  
Index Entry  Section

F
Function, read-eval-query: Internal functions

G
Generic Function, json-key-name: Internal generic functions
Generic Function, json-to-clos: Exported generic functions
Generic Function, json-type: Exported generic functions
Generic Function, no-values-class: Exported generic functions
Generic Function, no-values-hash-table: Exported generic functions
Generic Function, slot-name: Exported generic functions
Generic Function, to-json-value: Exported generic functions
Generic Function, to-lisp-value: Exported generic functions

J
json-key-name: Internal generic functions
json-key-name: Internal generic functions
json-key-name: Internal generic functions
json-to-clos: Exported generic functions
json-to-clos: Exported generic functions
json-to-clos: Exported generic functions
json-to-clos: Exported generic functions
json-to-clos: Exported generic functions
json-type: Exported generic functions
json-type: Exported generic functions
json-type: Exported generic functions

M
Method, json-key-name: Internal generic functions
Method, json-key-name: Internal generic functions
Method, json-to-clos: Exported generic functions
Method, json-to-clos: Exported generic functions
Method, json-to-clos: Exported generic functions
Method, json-to-clos: Exported generic functions
Method, json-type: Exported generic functions
Method, json-type: Exported generic functions
Method, no-values-class: Exported generic functions
Method, no-values-hash-table: Exported generic functions
Method, slot-name: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-json-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions
Method, to-lisp-value: Exported generic functions

N
no-values-class: Exported generic functions
no-values-class: Exported generic functions
no-values-hash-table: Exported generic functions
no-values-hash-table: Exported generic functions

R
read-eval-query: Internal functions

S
slot-name: Exported generic functions
slot-name: Exported generic functions

T
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-json-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions
to-lisp-value: Exported generic functions

Jump to:   F   G   J   M   N   R   S   T  

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

A.3 Variables

Jump to:   *  
C   E   H   J   S   V  
Index Entry  Section

*
*encode-unbound-slots*: Internal special variables

C
class-name: Exported conditions

E
element-json-type: Internal classes

H
hash-table: Exported conditions

J
json-key: Internal classes
json-type: Exported conditions
json-type: Internal classes

S
sequence-json-type: Internal classes
Slot, class-name: Exported conditions
Slot, element-json-type: Internal classes
Slot, hash-table: Exported conditions
Slot, json-key: Internal classes
Slot, json-type: Exported conditions
Slot, json-type: Internal classes
Slot, sequence-json-type: Internal classes
Slot, slot-name: Exported conditions
Slot, values: Internal classes
slot-name: Exported conditions
Special Variable, *encode-unbound-slots*: Internal special variables

V
values: Internal classes

Jump to:   *  
C   E   H   J   S   V  

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

A.4 Data types

Jump to:   C   H   J   N   P   S  
Index Entry  Section

C
Class, homogeneous-sequence-intermediate-class: Internal classes
Class, json-serializable: Exported classes
Class, json-serializable-class: Exported classes
Class, json-serializable-slot: Internal classes
Condition, json-type-error: Exported conditions
Condition, no-values-parsed: Exported conditions
Condition, null-in-homogeneous-sequence: Internal conditions
Condition, null-value: Exported conditions
Condition, slot-not-serializable: Exported conditions

H
homogeneous-sequence-intermediate-class: Internal classes

J
json-mop: The json-mop system
json-mop: The json-mop package
json-serializable: Exported classes
json-serializable-class: Exported classes
json-serializable-slot: Internal classes
json-type-error: Exported conditions

N
no-values-parsed: Exported conditions
null-in-homogeneous-sequence: Internal conditions
null-value: Exported conditions

P
Package, json-mop: The json-mop package

S
slot-not-serializable: Exported conditions
System, json-mop: The json-mop system

Jump to:   C   H   J   N   P   S