Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the json-mop Reference Manual, generated automatically by Declt version 3.0 "Montgomery Scott" on Mon Apr 19 16:32:31 2021 GMT+0.
• Introduction | What json-mop is all about | |
• Systems | The systems documentation | |
• Modules | The modules documentation | |
• Files | The files documentation | |
• Packages | The packages documentation | |
• Definitions | The symbols documentation | |
• Indexes | Concepts, functions, variables and data types |
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.
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:
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
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.
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.
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.
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)
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: Modules, Previous: Introduction, Up: Top [Contents][Index]
The main system appears first, followed by any subsystem dependency.
• The json-mop system |
Grim Schjetne
MIT
A metaclass for bridging CLOS and JSON
json-mop.asd (file)
src (module)
Modules are listed depth-first from the system components tree.
• The json-mop/src module |
json-mop (system)
src/
Files are sorted by type and then listed depth-first from the systems components trees.
• Lisp files |
Next: The json-mop/src/package․lisp file, Previous: Lisp files, Up: Lisp files [Contents][Index]
json-mop.asd
json-mop (system)
Next: The json-mop/src/conditions․lisp file, Previous: The json-mop․asd file, Up: Lisp files [Contents][Index]
Next: The json-mop/src/json-mop․lisp file, Previous: The json-mop/src/package․lisp file, Up: Lisp files [Contents][Index]
package.lisp (file)
src (module)
src/conditions.lisp
Next: The json-mop/src/to-lisp․lisp file, Previous: The json-mop/src/conditions․lisp file, Up: Lisp files [Contents][Index]
conditions.lisp (file)
src (module)
src/json-mop.lisp
Next: The json-mop/src/to-json․lisp file, Previous: The json-mop/src/json-mop․lisp file, Up: Lisp files [Contents][Index]
json-mop.lisp (file)
src (module)
src/to-lisp.lisp
Previous: The json-mop/src/to-lisp․lisp file, Up: Lisp files [Contents][Index]
to-lisp.lisp (file)
src (module)
src/to-json.lisp
Next: Definitions, Previous: Files, Up: Top [Contents][Index]
Packages are listed by definition order.
• The json-mop package |
package.lisp (file)
common-lisp
Definitions are sorted by export status, category, package, and then by lexicographic order.
• Exported definitions | ||
• Internal definitions |
Next: Internal definitions, Previous: Definitions, Up: Definitions [Contents][Index]
• Exported generic functions | ||
• Exported conditions | ||
• Exported classes |
Next: Exported conditions, Previous: Exported definitions, Up: Exported definitions [Contents][Index]
to-lisp.lisp (file)
automatically generated reader method
json-mop.lisp (file)
conditions.lisp (file)
conditions.lisp (file)
conditions.lisp (file)
conditions.lisp (file)
Turns a VALUE into a form appropriate for consumption by Yason
to-json.lisp (file)
Return the homogeneous sequence VALUE
Return the boolean false
Return the boolean true
Return the empty list VALUE
Return the list VALUE
Return the vector VALUE
Return the hash-table VALUE
Return the number VALUE
Return the string VALUE
When the JSON type is :ANY, Pass the VALUE unchanged
Turns a value passed by Yason into the appropriate Lisp type as specified by JSON-TYPE
to-lisp.lisp (file)
Return the CLOS object VALUE
Return the homogeneous sequence VALUE
Return the boolean VALUE
Return the list VALUE
Return the vector VALUE
Return the hash-table VALUE
Return the number VALUE
Return the string VALUE
When the JSON type is :ANY, Pass the VALUE unchanged
When the value is JSON null, signal NULL-VALUE error
Next: Exported classes, Previous: Exported generic functions, Up: Exported definitions [Contents][Index]
conditions.lisp (file)
error (condition)
json-type (method)
:json-type
json-type (generic function)
conditions.lisp (file)
warning (condition)
:hash-table
no-values-hash-table (generic function)
:class-name
no-values-class (generic function)
conditions.lisp (file)
json-type-error (condition)
conditions.lisp (file)
warning (condition)
slot-name (method)
:slot-name
slot-name (generic function)
Previous: Exported conditions, Up: Exported definitions [Contents][Index]
json-mop.lisp (file)
standard-object (class)
encode (method)
json-mop.lisp (file)
standard-class (class)
Previous: Exported definitions, Up: Definitions [Contents][Index]
• Internal special variables | ||
• Internal functions | ||
• Internal generic functions | ||
• Internal conditions | ||
• Internal classes |
Next: Internal functions, Previous: Internal definitions, Up: Internal definitions [Contents][Index]
to-json.lisp (file)
Next: Internal generic functions, Previous: Internal special variables, Up: Internal definitions [Contents][Index]
conditions.lisp (file)
Next: Internal conditions, Previous: Internal functions, Up: Internal definitions [Contents][Index]
json-mop.lisp (file)
automatically generated reader method
json-mop.lisp (file)
Next: Internal classes, Previous: Internal generic functions, Up: Internal definitions [Contents][Index]
conditions.lisp (file)
json-type-error (condition)
Previous: Internal conditions, Up: Internal definitions [Contents][Index]
to-json.lisp (file)
standard-object (class)
encode (method)
:values
:sequence-json-type
:element-json-type
json-mop.lisp (file)
standard-direct-slot-definition (class)
:json-key
json-key-name (generic function)
:json-type
:any
json-type (generic function)
Previous: Definitions, Up: Top [Contents][Index]
• Concept index | ||
• Function index | ||
• Variable index | ||
• Data type index |
Next: Function index, Previous: Indexes, Up: Indexes [Contents][Index]
Jump to: | F J L M |
---|
Jump to: | F J L M |
---|
Next: Variable index, Previous: Concept index, Up: Indexes [Contents][Index]
Jump to: | F G J M N R S T |
---|
Jump to: | F G J M N R S T |
---|
Next: Data type index, Previous: Function index, Up: Indexes [Contents][Index]
Jump to: | *
C E H J S V |
---|
Jump to: | *
C E H J S V |
---|
Previous: Variable index, Up: Indexes [Contents][Index]
Jump to: | C H J N P S |
---|
Jump to: | C H J N P S |
---|