From 68948b36c11803e28d10b73705ab87675fa459d0 Mon Sep 17 00:00:00 2001 From: Joe Martinez Date: Tue, 30 Jun 2009 16:01:02 -0400 Subject: [PATCH] create option to include_root_in_json for ActiveResource --- activeresource/lib/active_resource/base.rb | 9 +++++++++ activeresource/test/base_test.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+), 0 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index f919f91..fd13291 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -218,6 +218,9 @@ module ActiveResource # 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. @@ -907,6 +910,12 @@ module ActiveResource 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 diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 82d3b2a..8f0e45f 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -4,12 +4,14 @@ require "fixtures/customer" require "fixtures/street_address" require "fixtures/beast" require 'active_support/core_ext/hash/conversions' +require 'active_support/json' 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') + @joe = {'person' => { :id => 6, :name => 'Joe' }}.to_json @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") @@ -64,6 +66,7 @@ class BaseTest < Test::Unit::TestCase 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 @@ -859,6 +862,15 @@ class BaseTest < Test::Unit::TestCase assert xml.include?('1') 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 -- 1.6.0.5