Skip to content

Commit

Permalink
Tweak ActiveRecord::Base#to_json to include a root value in the retur…
Browse files Browse the repository at this point in the history
…ned hash: {post: {title: ...}} [rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9202 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Apr 1, 2008
1 parent 6a74360 commit d450ac4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,11 @@
*SVN*

* Tweak ActiveRecord::Base#to_json to include a root value in the returned hash: {"post": {"title": ...}} [rick]

Post.find(1).to_json # => {"title": ...}
config.active_record.include_root_in_json = true
Post.find(1).to_json # => {"post": {"title": ...}}

* Add efficient #include? to AssociationCollection (for has_many/has_many :through/habtm). [stopdropandrew]

* PostgreSQL: create_ and drop_database support. #9042 [ez, pedz, nicksieger]
Expand Down
17 changes: 16 additions & 1 deletion activerecord/lib/active_record/serializers/json_serializer.rb
@@ -1,5 +1,10 @@
module ActiveRecord #:nodoc:
module Serialization
def self.included(base)
base.cattr_accessor :include_root_in_json, :instance_writer => false
base.extend ClassMethods
end

# Returns a JSON string representing the model. Some configuration is
# available through +options+.
#
Expand Down Expand Up @@ -48,7 +53,11 @@ module Serialization
# {"comments": [{"body": "Don't think too hard"}],
# "title": "So I was thinking"}]}
def to_json(options = {})
JsonSerializer.new(self, options).to_s
if include_root_in_json
"{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}"
else
JsonSerializer.new(self, options).to_s
end
end

def from_json(json)
Expand All @@ -61,5 +70,11 @@ def serialize
serializable_record.to_json
end
end

module ClassMethods
def json_class_name
@json_class_name ||= name.demodulize.underscore.inspect
end
end
end
end
25 changes: 25 additions & 0 deletions activerecord/test/cases/json_serialization_test.rb
Expand Up @@ -7,6 +7,10 @@
require 'models/comment'

class JsonSerializationTest < ActiveRecord::TestCase
class NamespacedContact < Contact
column :name, :string
end

def setup
@contact = Contact.new(
:name => 'Konata Izumi',
Expand All @@ -18,6 +22,27 @@ def setup
)
end

def test_should_demodulize_root_in_json
NamespacedContact.include_root_in_json = true
@contact = NamespacedContact.new :name => 'whatever'
json = @contact.to_json
assert_match %r{^\{"namespaced_contact": \{}, json
end

def test_should_include_root_in_json
Contact.include_root_in_json = true
json = @contact.to_json

assert_match %r{^\{"contact": \{}, json
assert_match %r{"name": "Konata Izumi"}, json
assert_match %r{"age": 16}, json
assert json.include?(%("created_at": #{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome": true}, json
assert_match %r{"preferences": \{"shows": "anime"\}}, json
ensure
Contact.include_root_in_json = false
end

def test_should_encode_all_encodable_attributes
json = @contact.to_json

Expand Down

0 comments on commit d450ac4

Please sign in to comment.