Skip to content
Browse files

create option to include_root_in_json for ActiveResource [#2584 state…

…:committed]
  • Loading branch information...
1 parent c6746ff commit 72f89b5d971b48a133c4c0af56fbeda35d738dae @spastorino spastorino committed Apr 5, 2010
Showing with 28 additions and 6 deletions.
  1. +9 −0 activeresource/lib/active_resource/base.rb
  2. +19 −6 activeresource/test/cases/base_test.rb
View
9 activeresource/lib/active_resource/base.rb
@@ -251,6 +251,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
# Creates a schema for this resource - setting the attributes that are
# known prior to fetching an instance from the remote system.
@@ -1240,6 +1243,12 @@ def encode(options={})
case self.class.format
when ActiveResource::Formats::XmlFormat
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
25 activeresource/test/cases/base_test.rb
@@ -4,20 +4,22 @@
require "fixtures/street_address"
require "fixtures/beast"
require "fixtures/proxy"
+require 'active_support/json'
require 'active_support/core_ext/hash/conversions'
require 'mocha'
class BaseTest < Test::Unit::TestCase
def setup
- @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
- @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
- @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person')
- @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address')
@default_request_headers = { 'Content-Type' => 'application/xml' }
- @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person")
+ @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
+ @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
+ @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person')
+ @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address')
+ @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', :country => 'Australia' }].to_xml(:root => 'addresses')
+ @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses')
# - deep nested resource -
# - Luis (Customer)
@@ -66,6 +68,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
@@ -1012,6 +1015,16 @@ 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
+ Person.format = :xml
+ assert_equal json, '{"person":{"person":{"name":"Joe","id":6}}}'
+ end
+
def test_to_param_quacks_like_active_record
new_person = Person.new
assert_nil new_person.to_param

0 comments on commit 72f89b5

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