Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activerecord / lib / active_record / serializers / json_serializer.rb
1 module ActiveRecord #:nodoc:
2 module Serialization
3 def self.included(base)
4 base.cattr_accessor :include_root_in_json, :instance_writer => false
5 base.extend ClassMethods
6 end
7
8 # Returns a JSON string representing the model. Some configuration is
9 # available through +options+.
10 #
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:
15 #
16 # konata = User.find(1)
17 # ActiveRecord::Base.include_root_in_json = true
18 # konata.to_json
19 # # => { "user": {"id": 1, "name": "Konata Izumi", "age": 16,
20 # "created_at": "2006/08/01", "awesome": true} }
21 #
22 # ActiveRecord::Base.include_root_in_json = false
23 # konata.to_json
24 # # => {"id": 1, "name": "Konata Izumi", "age": 16,
25 # "created_at": "2006/08/01", "awesome": true}
26 #
27 # The remainder of the examples in this section assume include_root_in_json is set to
28 # <tt>false</tt>.
29 #
30 # Without any +options+, the returned JSON string will include all
31 # the model's attributes. For example:
32 #
33 # konata = User.find(1)
34 # konata.to_json
35 # # => {"id": 1, "name": "Konata Izumi", "age": 16,
36 # "created_at": "2006/08/01", "awesome": true}
37 #
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:
40 #
41 # konata.to_json(:only => [ :id, :name ])
42 # # => {"id": 1, "name": "Konata Izumi"}
43 #
44 # konata.to_json(:except => [ :id, :created_at, :age ])
45 # # => {"name": "Konata Izumi", "awesome": true}
46 #
47 # To include any methods on the model, use <tt>:methods</tt>.
48 #
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"}
53 #
54 # To include associations, use <tt>:include</tt>.
55 #
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"}]}
61 #
62 # 2nd level and higher order associations work as well:
63 #
64 # konata.to_json(:include => { :posts => {
65 # :include => { :comments => {
66 # :only => :body } },
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}}"
77 else
78 JsonSerializer.new(self, options).to_s
79 end
80 end
81
82 def from_json(json)
83 self.attributes = ActiveSupport::JSON.decode(json)
84 self
85 end
86
87 class JsonSerializer < ActiveRecord::Serialization::Serializer #:nodoc:
88 def serialize
89 serializable_record.to_json
90 end
91 end
92
93 module ClassMethods
94 def json_class_name
95 @json_class_name ||= name.demodulize.underscore.inspect
96 end
97 end
98 end
99 end