public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json methods. [#1011 
state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
codyfauser (author)
Tue Oct 07 09:05:41 -0700 2008
lifo (committer)
Tue Oct 07 09:48:02 -0700 2008
commit  f2c10f27565318b5efbdc57fb608b2afe9ae445d
tree    584cc6362342480d2c6b576d86bc9a539f8d3043
parent  dce6ade4cdc2833b53bd600ef10f9bce83c7102d
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *Edge*
0
 
0
+* Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json. #1011 [Rasik Pandey, Cody Fauser]
0
+
0
 * Add ActiveResource::Base.find(:last). [#754 state:resolved] (Adrian Mugnolo)
0
 
0
 * Fixed problems with the logger used if the logging string included %'s [#840 state:resolved] (Jamis Buck)
...
854
855
856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
857
858
859
...
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
0
@@ -854,6 +854,42 @@ module ActiveResource
0
     #
0
     #   my_group.to_xml(:skip_instruct => true)
0
     #   # => <subsidiary_group> [...] </subsidiary_group>
0
+    def to_xml(options={})
0
+      attributes.to_xml({:root => self.class.element_name}.merge(options))
0
+    end
0
+
0
+    # Returns a JSON string representing the model. Some configuration is
0
+    # available through +options+.
0
+    #
0
+    # ==== Options
0
+    # The +options+ are passed to the +to_json+ method on each
0
+    # attribute, so the same options as the +to_json+ methods in
0
+    # Active Support.
0
+    #
0
+    # * <tt>:only</tt> - Only include the specified attribute or list of
0
+    #   attributes in the serialized output. Attribute names must be specified
0
+    #   as strings.
0
+    # * <tt>:except</tt> - Do not include the specified attribute or list of
0
+    #   attributes in the serialized output. Attribute names must be specified
0
+    #   as strings.
0
+    #
0
+    # ==== Examples
0
+    #   person = Person.new(:first_name => "Jim", :last_name => "Smith")
0
+    #   person.to_json
0
+    #   # => {"first_name": "Jim", "last_name": "Smith"}
0
+    #
0
+    #   person.to_json(:only => ["first_name"])
0
+    #   # => {"first_name": "Jim"}
0
+    #
0
+    #   person.to_json(:except => ["first_name"])
0
+    #   # => {"last_name": "Smith"}
0
+    def to_json(options={})
0
+      attributes.to_json(options)
0
+    end
0
+
0
+    # Returns the serialized string representation of the resource in the configured
0
+    # serialization format specified in ActiveResource::Base.format. The options
0
+    # applicable depend on the configured encoding format.
0
     def encode(options={})
0
       case self.class.format
0
         when ActiveResource::Formats[:xml]
...
12
13
14
15
 
16
17
18
...
12
13
14
 
15
16
17
18
0
@@ -12,7 +12,7 @@ module ActiveResource
0
       end
0
 
0
       def encode(hash, options={})
0
-        hash.to_json
0
+        hash.to_json(options)
0
       end
0
 
0
       def decode(json)
...
1
2
 
3
4
5
...
83
84
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
87
88
...
1
2
3
4
5
6
...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
0
@@ -1,5 +1,6 @@
0
 require 'abstract_unit'
0
 require "fixtures/person"
0
+require "fixtures/street_address"
0
 
0
 class FormatTest < Test::Unit::TestCase
0
   def setup
0
@@ -83,6 +84,22 @@ class FormatTest < Test::Unit::TestCase
0
     assert_equal ActiveResource::Formats[:json], resource.connection.format
0
   end
0
 
0
+  def test_serialization_of_nested_resource
0
+   address  = { :street => '12345 Street' }
0
+   person  = { :name=> 'Rus', :address => address}
0
+
0
+   [:json, :xml].each do |format|
0
+     encoded_person = ActiveResource::Formats[format].encode(person)
0
+     assert_match /12345 Street/, encoded_person
0
+     remote_person = Person.new(person.update({:address => StreetAddress.new(address)}))
0
+     assert_kind_of StreetAddress, remote_person.address
0
+     using_format(Person, format) do
0
+       ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, encoded_person, 201, {'Location' => "/people/5.#{format}"}
0
+       remote_person.save
0
+     end
0
+   end
0
+ end
0
+
0
   private
0
     def using_format(klass, mime_type_reference)
0
       previous_format = klass.format

Comments