module ActiveRecord::Serialization
def self.included(base)
def self.included(base) base.cattr_accessor :include_root_in_json, :instance_writer => false end
def as_json(options = nil) #:nodoc:
def as_json(options = nil) #:nodoc: hash = Serializer.new(self, options).serializable_record hash = { options[:root] || self.class.model_name.element => hash } if include_root_in_json hash end
def from_json(json)
def from_json(json) self.attributes = ActiveSupport::JSON.decode(json) self end
def from_xml(xml)
def from_xml(xml) self.attributes = Hash.from_xml(xml).values.first self end
def to_json(options = {})
{"comments": [{"body": "Don't think too hard"}],
"title": "Welcome to the weblog"},
"posts": [{"comments": [{"body": "1st post!"}, {"body": "Second!"}],
"created_at": "2006/08/01", "awesome": true,
# => {"id": 1, "name": "Konata Izumi", "age": 16,
:only => :title } })
:only => :body } },
:include => { :comments => {
konata.to_json(:include => { :posts => {
2nd level and higher order associations work as well:
{"id": 2, author_id: 1, "title": "So I was thinking"}]}
"posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"},
"created_at": "2006/08/01", "awesome": true,
# => {"id": 1, "name": "Konata Izumi", "age": 16,
konata.to_json(:include => :posts)
To include associations, use :include.
"permalink": "1-konata-izumi"}
"created_at": "2006/08/01", "awesome": true,
# => {"id": 1, "name": "Konata Izumi", "age": 16,
konata.to_json(:methods => :permalink)
To include any methods on the model, use :methods.
# => {"name": "Konata Izumi", "awesome": true}
konata.to_json(:except => [ :id, :created_at, :age ])
# => {"id": 1, "name": "Konata Izumi"}
konata.to_json(:only => [ :id, :name ])
included, and work similar to the +attributes+ method. For example:
The :only and :except options can be used to limit the attributes
"created_at": "2006/08/01", "awesome": true}
# => {"id": 1, "name": "Konata Izumi", "age": 16,
konata.to_json
konata = User.find(1)
the model's attributes. For example:
Without any +options+, the returned JSON string will include all
false.
The remainder of the examples in this section assume include_root_in_json is set to
"created_at": "2006/08/01", "awesome": true}
# => {"id": 1, "name": "Konata Izumi", "age": 16,
konata.to_json
ActiveRecord::Base.include_root_in_json = false
"created_at": "2006/08/01", "awesome": true} }
# => { "user": {"id": 1, "name": "Konata Izumi", "age": 16,
konata.to_json
ActiveRecord::Base.include_root_in_json = true
konata = User.find(1)
to_json will emit a single root node named after the object's type. For example:
true in initializers/new_rails_defaults.rb. When it is true,
top-level behavior of to_json. In a new Rails application, it is set to
The option ActiveRecord::Base.include_root_in_json controls the
available through +options+.
Returns a JSON string representing the model. Some configuration is
def to_json(options = {}) super end
def to_xml(options = {}, &block)
end
end
xml.tag!(:second_level, 'content')
xml.level_one do
xml.instruct! unless options[:skip_instruct]
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
options[:indent] ||= 2
def to_xml(options = {})
class IHaveMyOwnXML < ActiveRecord::Base
form of doing this is:
subclasses to have complete control about what's generated. The general
As noted above, you may override +to_xml+ in your ActiveRecord::Base
# ... normal attributes as shown above ...
end
end
xml.last_name "Heinemeier Hansson"
xml.first_name "David"
xml.creator do
firm.to_xml do |xml|
Alternatively, you can yield the builder object as part of the +to_xml+ call:
# ... normal attributes as shown above ...
firm.to_xml :procs => [ proc ]
proc = Proc.new { |options| options[:builder].tag!('abc', 'def') }
modified version of the options hash that was given to +to_xml+:
To call any additional Procs use :procs. The Procs are passed a
# ... normal attributes as shown above ...
firm.to_xml :methods => [ :calculated_earnings, :real_earnings ]
To include any methods on the model being called use :methods:
...
...
firm.to_xml :include => {:account => {}, :clients => {:include => :address}}
To include deeper levels of associations pass a hash like this:
firm.to_xml :include => [ :account, :clients ]
To include first level associations use :include:
topic.to_xml(:skip_instruct => true, :except => [ :id, :bonus_time, :written_on, :replies_count ])
For instance:
To not have the column type included in the XML output set :skip_types to +true+.
to +true+ will camelize all column names - this also overrides :dasherize.
can disable this setting :dasherize to +false+. Setting :camelize
+attributes+ method. The default is to dasherize all column names, but you
The :only and :except options are the same as for the
:skip_instruct, :skip_types, :dasherize and :camelize .
This behavior can be controlled with :only, :except,
instruction and all the object's attributes. For example:
By default the generated XML document will include the processing
override ActiveRecord::Base#to_xml.
available through +options+. However more complicated cases should
Builds an XML document to represent the model. Some configuration is
def to_xml(options = {}, &block) serializer = XmlSerializer.new(self, options) block_given? ? serializer.to_s(&block) : serializer.to_s end