Skip to content
Browse files

Add ActiveResource::Base.include_root_in_json, like Active Record, to…

… serialize instances as hash of model name -> attributes hash rather than the bare attributes hash. [#2584 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent 9e262de commit aa401bd75a055a0bb12094128b01c8ce5fc2abe8 Joe Martinez committed with jeremy Apr 26, 2010
Showing with 26 additions and 0 deletions.
  1. +2 −0 actionpack/CHANGELOG
  2. +9 −0 activeresource/lib/active_resource/base.rb
  3. +15 −0 activeresource/test/base_test.rb
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*2.3.6 (unreleased)*
+* JSON: set Base.include_root_in_json = true to include a root value in the JSON: {"post": {"title": ...}}. Mirrors the Active Record option. #2584 [Matthew Moore, Joe Martinez, Elad Meidar, Santiago Pastorino]
+
* Ruby 1.9: ERB template encoding using a magic comment at the top of the file. [Jeremy Kemper]
<%# encoding: utf-8 %>
View
9 activeresource/lib/active_resource/base.rb
@@ -227,6 +227,9 @@ class Base
# The logger for diagnosing and tracing Active Resource calls.
cattr_accessor :logger
+ # Controls the top-level behavior of JSON serialization
+ cattr_accessor :include_root_in_json, :instance_writer => false
+
class << self
# Gets the URI of the REST resources to map for this class. The site variable is required for
# Active Resource's mapping to work.
@@ -971,6 +974,12 @@ def encode(options={})
case self.class.format
when ActiveResource::Formats[:xml]
self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options))
+ when ActiveResource::Formats::JsonFormat
+ if ActiveResource::Base.include_root_in_json
+ self.class.format.encode({self.class.element_name => attributes}, options)
+ else
+ self.class.format.encode(attributes, options)
+ end
else
self.class.format.encode(attributes, options)
end
View
15 activeresource/test/base_test.rb
@@ -4,6 +4,7 @@
require "fixtures/street_address"
require "fixtures/beast"
require "fixtures/proxy"
+require 'active_support/json'
class BaseTest < Test::Unit::TestCase
def setup
@@ -13,6 +14,7 @@ def setup
@addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address')
@default_request_headers = { 'Content-Type' => 'application/xml' }
@rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person")
+ @joe = {'person' => { :id => 6, :name => 'Joe' }}.to_json
@people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people')
@people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people')
@addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses')
@@ -64,6 +66,7 @@ def setup
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, @matz
mock.get "/people/2.xml", {}, @david
+ mock.get "/people/6.json", {}, @joe
mock.get "/people/5.xml", {}, @marty
mock.get "/people/Greg.xml", {}, @greg
mock.get "/people/4.xml", {'key' => 'value'}, nil, 404
@@ -1005,6 +1008,18 @@ def test_to_xml
assert xml.include?('<id type="integer">1</id>')
end
+ def test_to_json_including_root
+ Person.include_root_in_json = true
+ Person.format = :json
+ joe = Person.find(6)
+ json = joe.encode
+ assert_match '{"person":{"person":{', json
+ assert_match '"name":"Joe"', json
+ assert_match '"id":6', json
+ ensure
+ Person.format = :xml
+ end
+
def test_to_param_quacks_like_active_record
new_person = Person.new
assert_nil new_person.to_param

0 comments on commit aa401bd

Please sign in to comment.
Something went wrong with that request. Please try again.