aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2008-04-08 05:05:54 +0000
committerRick Olson <technoweenie@gmail.com>2008-04-08 05:05:54 +0000
commit4d594cffcfc93b37fad4e423ec8593299e50133c (patch)
tree59643ee7a1eec4e3208f81f6e344a66207f648ec /actionpack/lib/action_controller
parent0ff7a2d89fc95dcb0a32ed92aab7156b0778a7ea (diff)
downloadrails-4d594cffcfc93b37fad4e423ec8593299e50133c.tar.gz
rails-4d594cffcfc93b37fad4e423ec8593299e50133c.tar.bz2
rails-4d594cffcfc93b37fad4e423ec8593299e50133c.zip
Automatically parse posted JSON content for Mime::JSON requests. [rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9242 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller')
-rwxr-xr-xactionpack/lib/action_controller/base.rb5
-rwxr-xr-xactionpack/lib/action_controller/request.rb15
2 files changed, 14 insertions, 6 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index d7e4116581..7c838ba769 100755
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -316,9 +316,10 @@ module ActionController #:nodoc:
# A YAML parser is also available and can be turned on with:
#
# ActionController::Base.param_parsers[Mime::YAML] = :yaml
- @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
+ @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
Mime::URL_ENCODED_FORM => :url_encoded_form,
- Mime::XML => :xml_simple }
+ Mime::XML => :xml_simple,
+ Mime::JSON => :json }
cattr_accessor :param_parsers
# Controls the default charset for all renders.
diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb
index 745161def8..823271d13f 100755
--- a/actionpack/lib/action_controller/request.rb
+++ b/actionpack/lib/action_controller/request.rb
@@ -402,6 +402,14 @@ EOM
body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
when :yaml
YAML.load(body)
+ when :json
+ if body.blank?
+ {}
+ else
+ data = ActiveSupport::JSON.decode(body)
+ data = {:_json => data} unless data.is_a?(Hash)
+ data.with_indifferent_access
+ end
else
{}
end
@@ -507,7 +515,6 @@ EOM
end
end
-
MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n
EOL = "\015\012"
@@ -604,12 +611,12 @@ EOM
end
raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/
- begin
+ begin
body.rewind if body.respond_to?(:rewind)
- rescue Errno::ESPIPE
+ rescue Errno::ESPIPE
# Handles exceptions raised by input streams that cannot be rewound
# such as when using plain CGI under Apache
- end
+ end
params
end