GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/ddollar/rails.git
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
technoweenie (author)
Mon Apr 07 22:05:54 -0700 2008
commit  4d594cffcfc93b37fad4e423ec8593299e50133c
tree    59643ee7a1eec4e3208f81f6e344a66207f648ec
parent  0ff7a2d89fc95dcb0a32ed92aab7156b0778a7ea
...
1
2
 
 
 
 
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1,5 +1,15 @@
0
 *SVN*
0
 
0
+* Automatically parse posted JSON content for Mime::JSON requests. [rick]
0
+
0
+ POST /posts
0
+ {"post": {"title": "Breaking News"}}
0
+
0
+ def create
0
+ @post = Post.create params[:post]
0
+ # ...
0
+ end
0
+
0
 * add json_escape ERB util to escape html entities in json strings that are output in HTML pages. [rick]
0
 
0
 * Provide a helper proxy to access helper methods from outside views. Closes #10839 [Josh Peek]
...
316
317
318
319
 
320
321
 
 
322
323
324
...
316
317
318
 
319
320
 
321
322
323
324
325
0
@@ -316,9 +316,10 @@ module ActionController #:nodoc:
0
     # A YAML parser is also available and can be turned on with:
0
     #
0
     # ActionController::Base.param_parsers[Mime::YAML] = :yaml
0
- @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
0
+ @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
0
                         Mime::URL_ENCODED_FORM => :url_encoded_form,
0
- Mime::XML => :xml_simple }
0
+ Mime::XML => :xml_simple,
0
+ Mime::JSON => :json }
0
     cattr_accessor :param_parsers
0
 
0
     # Controls the default charset for all renders.
...
402
403
404
 
 
 
 
 
 
 
 
405
406
407
...
507
508
509
510
511
512
513
...
604
605
606
607
 
608
609
 
610
611
612
 
613
614
615
...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
...
515
516
517
 
518
519
520
...
611
612
613
 
614
615
 
616
617
618
 
619
620
621
622
0
@@ -402,6 +402,14 @@ EOM
0
             body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
0
           when :yaml
0
             YAML.load(body)
0
+ when :json
0
+ if body.blank?
0
+ {}
0
+ else
0
+ data = ActiveSupport::JSON.decode(body)
0
+ data = {:_json => data} unless data.is_a?(Hash)
0
+ data.with_indifferent_access
0
+ end
0
           else
0
             {}
0
         end
0
@@ -507,7 +515,6 @@ EOM
0
           end
0
         end
0
 
0
-
0
         MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n
0
 
0
         EOL = "\015\012"
0
@@ -604,12 +611,12 @@ EOM
0
           end
0
           raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/
0
 
0
- begin
0
+ begin
0
             body.rewind if body.respond_to?(:rewind)
0
- rescue Errno::ESPIPE
0
+ rescue Errno::ESPIPE
0
             # Handles exceptions raised by input streams that cannot be rewound
0
             # such as when using plain CGI under Apache
0
- end
0
+ end
0
 
0
           params
0
         end
...
3
4
5
6
 
7
8
9
...
3
4
5
 
6
7
8
9
0
@@ -3,7 +3,7 @@ require 'erb'
0
 class ERB
0
   module Util
0
     HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
0
- JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C'}
0
+ JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
0
 
0
     # A utility method for escaping HTML tag characters.
0
     # This method is also aliased as <tt>h</tt>.
...
851
852
853
854
855
 
 
 
 
 
 
856
857
858
...
899
900
901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
851
852
853
 
854
855
856
857
858
859
860
861
862
863
...
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
0
@@ -851,8 +851,13 @@ class MultipartRequestParameterParsingTest < Test::Unit::TestCase
0
     end
0
 end
0
 
0
-
0
 class XmlParamsParsingTest < Test::Unit::TestCase
0
+ def test_hash_params
0
+ person = parse_body("<person><name>David</name></person>")[:person]
0
+ assert_kind_of Hash, person
0
+ assert_equal 'David', person['name']
0
+ end
0
+
0
   def test_single_file
0
     person = parse_body("<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></person>")
0
 
0
@@ -899,3 +904,19 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest
0
       ActionController::CgiRequest.new(cgi).request_parameters
0
     end
0
 end
0
+
0
+class JsonParamsParsingTest < Test::Unit::TestCase
0
+ def test_hash_params
0
+ person = parse_body({:person => {:name => "David"}}.to_json)[:person]
0
+ assert_kind_of Hash, person
0
+ assert_equal 'David', person['name']
0
+ end
0
+
0
+ private
0
+ def parse_body(body)
0
+ env = { 'CONTENT_TYPE' => 'application/json',
0
+ 'CONTENT_LENGTH' => body.size.to_s }
0
+ cgi = ActionController::Integration::Session::StubCGI.new(env, body)
0
+ ActionController::CgiRequest.new(cgi).request_parameters
0
+ end
0
+end

Comments

    No one has commented yet.