Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
diff --git a/lib/merb-core/dispatch/request.rb b/lib/merb-core/dispatch/request.rb index ff55484..54445e0 100644 --- a/lib/merb-core/dispatch/request.rb +++ b/lib/merb-core/dispatch/request.rb @@ -113,10 +113,19 @@ module Merb # ==== Returns # Hash:: Parameters from body if this is a JSON request. + # + # ==== Notes + # If the JSON object parses as a Hash, it will be merged with the + # parameters hash. If it parses to anything else (such as an Array, or + # if it inflates to an Object) it will be put in the json_object + # parameter. def json_params @json_params ||= begin if Merb::Const::JSON_MIME_TYPE_REGEXP.match(content_type) - JSON.parse(raw_post) + jobj = JSON.parse(raw_post) + # If the JSON object inflates to something other than a Hash, + # we should return it as the :json_object in the params. + jobj.kind_of?(Hash) ? jobj : { "json_object" => jobj } end end end diff --git a/spec/public/request/request_spec.rb b/spec/public/request/request_spec.rb index 8be17a6..7bd4e68 100644 --- a/spec/public/request/request_spec.rb +++ b/spec/public/request/request_spec.rb @@ -66,7 +66,13 @@ describe Merb::Request, " query and body params" do request.stub!(:route_params).and_return({}) request.params.should == {"foo" => "bar"} end - + + it "should support put JSON params that do not inflate to a hash in param 'json_object'" do + request = fake_request({:content_type => "application/json"}, :req => %{["foo", "bar"]}) + request.stub!(:route_params).and_return({}) + request.params.should == {"json_object" => [ "foo", "bar" ]} + end + it "should support XML params" do request = fake_request({:content_type => "application/xml"}, :req => %{<foo bar="baz"><baz/></foo>}) request.stub!(:route_params).and_return({})
This paste will be private.
From the Design Piracy series on my blog: