aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib/active_resource/formats
diff options
context:
space:
mode:
Diffstat (limited to 'activeresource/lib/active_resource/formats')
-rw-r--r--activeresource/lib/active_resource/formats/json_format.rb23
-rw-r--r--activeresource/lib/active_resource/formats/xml_format.rb34
2 files changed, 57 insertions, 0 deletions
diff --git a/activeresource/lib/active_resource/formats/json_format.rb b/activeresource/lib/active_resource/formats/json_format.rb
new file mode 100644
index 0000000000..df0d6ca372
--- /dev/null
+++ b/activeresource/lib/active_resource/formats/json_format.rb
@@ -0,0 +1,23 @@
+module ActiveResource
+ module Formats
+ module JsonFormat
+ extend self
+
+ def extension
+ "json"
+ end
+
+ def mime_type
+ "application/json"
+ end
+
+ def encode(hash)
+ hash.to_json
+ end
+
+ def decode(json)
+ ActiveSupport::JSON.decode(json)
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activeresource/lib/active_resource/formats/xml_format.rb b/activeresource/lib/active_resource/formats/xml_format.rb
new file mode 100644
index 0000000000..01c28dcee6
--- /dev/null
+++ b/activeresource/lib/active_resource/formats/xml_format.rb
@@ -0,0 +1,34 @@
+module ActiveResource
+ module Formats
+ module XmlFormat
+ extend self
+
+ def extension
+ "xml"
+ end
+
+ def mime_type
+ "application/xml"
+ end
+
+ def encode(hash)
+ hash.to_xml
+ end
+
+ def decode(xml)
+ from_xml_data(Hash.from_xml(xml))
+ end
+
+ private
+ # Manipulate from_xml Hash, because xml_simple is not exactly what we
+ # want for ActiveResource.
+ def from_xml_data(data)
+ if data.is_a?(Hash) && data.keys.size == 1
+ data.values.first
+ else
+ data
+ end
+ end
+ end
+ end
+end \ No newline at end of file