1fd65ed006d551ec692590ec0ec60a91601e4b2d
1 module ActiveRecord
#:nodoc:
3 def self.included(base
)
4 base
.cattr_accessor
:include_root_in_json, :instance_writer => false
5 base
.extend ClassMethods
8 # Returns a JSON string representing the model. Some configuration is
9 # available through +options+.
11 # The option <tt>ActiveRecord::Base.include_root_in_json</tt> controls the
12 # top-level behavior of to_json. In a new Rails application, it is set to
13 # <tt>true</tt> in initializers/new_rails_defaults.rb. When it is <tt>true</tt>,
14 # to_json will emit a single root node named after the object's type. For example:
16 # konata = User.find(1)
17 # ActiveRecord::Base.include_root_in_json = true
19 # # => { "user": {"id": 1, "name": "Konata Izumi", "age": 16,
20 # "created_at": "2006/08/01", "awesome": true} }
22 # ActiveRecord::Base.include_root_in_json = false
24 # # => {"id": 1, "name": "Konata Izumi", "age": 16,
25 # "created_at": "2006/08/01", "awesome": true}
27 # The remainder of the examples in this section assume include_root_in_json is set to
30 # Without any +options+, the returned JSON string will include all
31 # the model's attributes. For example:
33 # konata = User.find(1)
35 # # => {"id": 1, "name": "Konata Izumi", "age": 16,
36 # "created_at": "2006/08/01", "awesome": true}
38 # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the attributes
39 # included, and work similar to the +attributes+ method. For example:
41 # konata.to_json(:only => [ :id, :name ])
42 # # => {"id": 1, "name": "Konata Izumi"}
44 # konata.to_json(:except => [ :id, :created_at, :age ])
45 # # => {"name": "Konata Izumi", "awesome": true}
47 # To include any methods on the model, use <tt>:methods</tt>.
49 # konata.to_json(:methods => :permalink)
50 # # => {"id": 1, "name": "Konata Izumi", "age": 16,
51 # "created_at": "2006/08/01", "awesome": true,
52 # "permalink": "1-konata-izumi"}
54 # To include associations, use <tt>:include</tt>.
56 # konata.to_json(:include => :posts)
57 # # => {"id": 1, "name": "Konata Izumi", "age": 16,
58 # "created_at": "2006/08/01", "awesome": true,
59 # "posts": [{"id": 1, "author_id": 1, "title": "Welcome to the weblog"},
60 # {"id": 2, author_id: 1, "title": "So I was thinking"}]}
62 # 2nd level and higher order associations work as well:
64 # konata.to_json(:include => { :posts => {
65 # :include => { :comments => {
67 # :only => :title } })
68 # # => {"id": 1, "name": "Konata Izumi", "age": 16,
69 # "created_at": "2006/08/01", "awesome": true,
70 # "posts": [{"comments": [{"body": "1st post!"}, {"body": "Second!"}],
71 # "title": "Welcome to the weblog"},
72 # {"comments": [{"body": "Don't think too hard"}],
73 # "title": "So I was thinking"}]}
74 def to_json(options
= {})
75 if include_root_in_json
76 "{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}"
78 JsonSerializer
.new(self, options
).to_s
83 self.attributes
= ActiveSupport
::JSON.decode(json
)
87 class JsonSerializer
< ActiveRecord
::Serialization::Serializer #:nodoc:
89 serializable_record
.to_json
95 @json_class_name ||= name
.demodulize
.underscore
.inspect