module Sequel::Plugins::JsonSerializer

  1. lib/sequel/plugins/json_serializer.rb

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]
# => '{"id"=>1,"name"=>"RF","artist_id"=>2}'

In addition, you can provide options to control the JSON output:

album.to_json(:except=>[:id, :artist_id])
# => '{"json_class"="Album","name"=>"RF"}'

# => '{"id":1,"name":"RF","artist_id":2,
#      "artist":{"id":2,"name":"YJM"}}'

You can use a hash value with :include to pass options to associations:

# => '{"id":1,"name":"RF","artist_id":2,
#      "artist":{"name":"YJM"}}'

You can specify a name for a given association by passing :name to the :include hash

album.to_json(:include=>{, :singer)=>{:only=>:name}})
# => '{"id":1,"name":"RF","artist_id":2,
#      "singer":{"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}}'

You can specify JSON serialization options to use later:

album.json_serializer_opts(: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:


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.where(: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:


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(options = {})

class Hash
  def to_json(options = {})

Note that this will probably cause active_support/json to no longer work correctly in some cases.


# 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


Public Class

  1. configure

Public Class methods

configure (model, opts={})

Set up the column readers to do deserialization and the column writers to save the value in deserialized_values.

[show source]
# File lib/sequel/plugins/json_serializer.rb, line 128
def self.configure(model, opts={})
  model.instance_eval do
    @json_serializer_opts = (@json_serializer_opts || {}).merge(opts)