aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
authorCody Fauser <codyfauser@gmail.com>2008-10-07 12:05:41 -0400
committerPratik Naik <pratiknaik@gmail.com>2008-10-07 17:48:02 +0100
commitf2c10f27565318b5efbdc57fb608b2afe9ae445d (patch)
tree584cc6362342480d2c6b576d86bc9a539f8d3043 /activeresource
parentdce6ade4cdc2833b53bd600ef10f9bce83c7102d (diff)
downloadrails-f2c10f27565318b5efbdc57fb608b2afe9ae445d.tar.gz
rails-f2c10f27565318b5efbdc57fb608b2afe9ae445d.tar.bz2
rails-f2c10f27565318b5efbdc57fb608b2afe9ae445d.zip
Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json methods. [#1011 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/CHANGELOG2
-rw-r--r--activeresource/lib/active_resource/base.rb36
-rw-r--r--activeresource/lib/active_resource/formats/json_format.rb2
-rw-r--r--activeresource/test/format_test.rb17
4 files changed, 56 insertions, 1 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG
index 012f9028a0..e93b3ec15b 100644
--- a/activeresource/CHANGELOG
+++ b/activeresource/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*
+* Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json. #1011 [Rasik Pandey, Cody Fauser]
+
* Add ActiveResource::Base.find(:last). [#754 state:resolved] (Adrian Mugnolo)
* Fixed problems with the logger used if the logging string included %'s [#840 state:resolved] (Jamis Buck)
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 74d8128c0e..bb284803d8 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -854,6 +854,42 @@ module ActiveResource
#
# my_group.to_xml(:skip_instruct => true)
# # => <subsidiary_group> [...] </subsidiary_group>
+ def to_xml(options={})
+ attributes.to_xml({:root => self.class.element_name}.merge(options))
+ end
+
+ # Returns a JSON string representing the model. Some configuration is
+ # available through +options+.
+ #
+ # ==== Options
+ # The +options+ are passed to the +to_json+ method on each
+ # attribute, so the same options as the +to_json+ methods in
+ # Active Support.
+ #
+ # * <tt>:only</tt> - Only include the specified attribute or list of
+ # attributes in the serialized output. Attribute names must be specified
+ # as strings.
+ # * <tt>:except</tt> - Do not include the specified attribute or list of
+ # attributes in the serialized output. Attribute names must be specified
+ # as strings.
+ #
+ # ==== Examples
+ # person = Person.new(:first_name => "Jim", :last_name => "Smith")
+ # person.to_json
+ # # => {"first_name": "Jim", "last_name": "Smith"}
+ #
+ # person.to_json(:only => ["first_name"])
+ # # => {"first_name": "Jim"}
+ #
+ # person.to_json(:except => ["first_name"])
+ # # => {"last_name": "Smith"}
+ def to_json(options={})
+ attributes.to_json(options)
+ end
+
+ # Returns the serialized string representation of the resource in the configured
+ # serialization format specified in ActiveResource::Base.format. The options
+ # applicable depend on the configured encoding format.
def encode(options={})
case self.class.format
when ActiveResource::Formats[:xml]
diff --git a/activeresource/lib/active_resource/formats/json_format.rb b/activeresource/lib/active_resource/formats/json_format.rb
index 9e269d4ded..1d88fc5f16 100644
--- a/activeresource/lib/active_resource/formats/json_format.rb
+++ b/activeresource/lib/active_resource/formats/json_format.rb
@@ -12,7 +12,7 @@ module ActiveResource
end
def encode(hash, options={})
- hash.to_json
+ hash.to_json(options)
end
def decode(json)
diff --git a/activeresource/test/format_test.rb b/activeresource/test/format_test.rb
index 365576a092..a564ee01b5 100644
--- a/activeresource/test/format_test.rb
+++ b/activeresource/test/format_test.rb
@@ -1,5 +1,6 @@
require 'abstract_unit'
require "fixtures/person"
+require "fixtures/street_address"
class FormatTest < Test::Unit::TestCase
def setup
@@ -83,6 +84,22 @@ class FormatTest < Test::Unit::TestCase
assert_equal ActiveResource::Formats[:json], resource.connection.format
end
+ def test_serialization_of_nested_resource
+ address = { :street => '12345 Street' }
+ person = { :name=> 'Rus', :address => address}
+
+ [:json, :xml].each do |format|
+ encoded_person = ActiveResource::Formats[format].encode(person)
+ assert_match /12345 Street/, encoded_person
+ remote_person = Person.new(person.update({:address => StreetAddress.new(address)}))
+ assert_kind_of StreetAddress, remote_person.address
+ using_format(Person, format) do
+ ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, encoded_person, 201, {'Location' => "/people/5.#{format}"}
+ remote_person.save
+ end
+ end
+ end
+
private
def using_format(klass, mime_type_reference)
previous_format = klass.format