Module: Sequel::Plugins::JsonSerializer
- Defined in:
- lib/sequel/plugins/json_serializer.rb
Overview
The json_serializer plugin handles serializing entire Sequel::Model objects to JSON, as well as support for deserializing JSON directly into Sequel::Model objects. It requires the json library, and can work with either the pure ruby version or the C extension.
Basic Example:
album = Album[1]
album.to_json
# => '{"json_class"=>"Album","id"=>1,"name"=>"RF","artist_id"=>2}'
In addition, you can provide options to control the JSON output:
album.to_json(:only=>:name)
album.to_json(:except=>[:id, :artist_id])
# => '{"json_class"="Album","name"=>"RF"}'
album.to_json(:include=>:artist)
# => '{"json_class":"Album","id":1,"name":"RF","artist_id":2,
# "artist":{"json_class":"Artist","id":2,"name":"YJM"}}'
You can use a hash value with :include
to pass options to associations:
album.to_json(:include=>{:artist=>{:only=>:name}})
# => '{"json_class":"Album","id":1,"name":"RF","artist_id":2,
# "artist":{"json_class":"Artist","name":"YJM"}}'
You can specify the :root
option to nest the JSON under the name of the model:
album.to_json(:root => true)
# => '{"album":{"id":1,"name":"RF","artist_id":2}}'
Additionally, to_json
also exists as a class and dataset method, both of which return all objects in the dataset:
Album.to_json
Album.filter(:artist_id=>1).to_json(:include=>:tags)
If you have an existing array of model instances you want to convert to JSON, you can call the class to_json method with the :array option:
Album.to_json(:array=>[Album[1], Album[2]])
In addition to creating JSON, this plugin also enables Sequel::Model classes to create instances directly from JSON using the from_json class method:
json = album.to_json
album = Album.from_json(json)
The array_from_json class method exists to parse arrays of model instances from json:
json = Album.filter(:artist_id=>1).to_json
albums = Album.array_from_json(json)
These does not necessarily round trip, since doing so would let users create model objects with arbitrary values. By default, from_json will call set with the values in the hash. If you want to specify the allowed fields, you can use the :fields option, which will call set_fields with the given fields:
Album.from_json(album.to_json, :fields=>%w'id name')
If you want to update an existing instance, you can use the from_json instance method:
album.from_json(json)
Both of these allow creation of cached associated objects, if you provide the :associations option:
album.from_json(json, :associations=>:artist)
You can even provide options when setting up the associated objects:
album.from_json(json, :associations=>{:artist=>{:fields=>%w'id name', :associations=>:tags}})
Note that active_support/json makes incompatible changes to the to_json API, and breaks some aspects of the json_serializer plugin. You can undo the damage done by active_support/json by doing:
class Array
def to_json( = {})
JSON.generate(self)
end
end
class Hash
def to_json( = {})
JSON.generate(self)
end
end
Note that this will probably cause active_support/json to no longer work correctly in some cases.
Usage:
# Add JSON output capability to all model subclass instances (called before loading subclasses)
Sequel::Model.plugin :json_serializer
# Add JSON output capability to Album class instances
Album.plugin :json_serializer
Defined Under Namespace
Modules: ClassMethods, DatasetMethods, InstanceMethods Classes: Literal
Class Method Summary collapse
-
.configure(model, opts = {}) ⇒ Object
Set up the column readers to do deserialization and the column writers to save the value in deserialized_values.
Class Method Details
.configure(model, opts = {}) ⇒ Object
Set up the column readers to do deserialization and the column writers to save the value in deserialized_values.
114 115 116 117 118 |
# File 'lib/sequel/plugins/json_serializer.rb', line 114 def self.configure(model, opts={}) model.instance_eval do @json_serializer_opts = (@json_serializer_opts || {}).merge(opts) end end |