1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Replace multi_json with json

This commit is contained in:
Erik Michaels-Ober 2013-05-11 13:59:37 -07:00
parent 98ade30e42
commit ce4456fde6
4 changed files with 19 additions and 46 deletions

View file

@ -62,7 +62,7 @@ class JsonParamsParsingTest < ActionDispatch::IntegrationTest
$stderr = StringIO.new # suppress the log
json = "[\"person]\": {\"name\": \"David\"}}"
exception = assert_raise(ActionDispatch::ParamsParser::ParseError) { post "/parse", json, {'CONTENT_TYPE' => 'application/json', 'action_dispatch.show_exceptions' => false} }
assert_equal MultiJson::DecodeError, exception.original_exception.class
assert_equal JSON::ParserError, exception.original_exception.class
assert_equal exception.original_exception.message, exception.message
ensure
$stderr = STDERR

View file

@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.rdoc_options.concat ['--encoding', 'UTF-8']
s.add_dependency('i18n', '~> 0.6', '>= 0.6.4')
s.add_dependency 'multi_json', '~> 1.3'
s.add_dependency 'json', '~> 1.7'
s.add_dependency 'tzinfo', '~> 0.3.37'
s.add_dependency 'minitest', '~> 4.2'
s.add_dependency 'thread_safe','~> 0.1'

View file

@ -1,6 +1,6 @@
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/module/delegation'
require 'multi_json'
require 'json'
module ActiveSupport
# Look for and parse json strings that look like ISO 8601 times.
@ -13,8 +13,8 @@ module ActiveSupport
#
# ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}")
# => {"team" => "rails", "players" => "36"}
def decode(json, options ={})
data = MultiJson.load(json, options)
def decode(json, proc = nil, options = {})
data = ::JSON.load(json, proc, options)
if ActiveSupport.parse_json_times
convert_dates_from(data)
else
@ -22,23 +22,6 @@ module ActiveSupport
end
end
def engine
MultiJson.adapter
end
alias :backend :engine
def engine=(name)
MultiJson.use(name)
end
alias :backend= :engine=
def with_backend(name)
old_backend, self.backend = backend, name
yield
ensure
self.backend = old_backend
end
# Returns the class of the error that will be raised when there is an
# error in decoding JSON. Using this method means you won't directly
# depend on the ActiveSupport's JSON implementation, in case it changes
@ -50,7 +33,7 @@ module ActiveSupport
# Rails.logger.warn("Attempted to decode invalid JSON: #{some_string}")
# end
def parse_error
MultiJson::DecodeError
::JSON::ParserError
end
private

View file

@ -55,35 +55,25 @@ class TestJSONDecoding < ActiveSupport::TestCase
%q({"a":"Line1\u000aLine2"}) => {"a"=>"Line1\nLine2"}
}
backends = [:ok_json]
backends << :json_gem if defined?(::JSON)
backends << :yajl if defined?(::Yajl)
backends.each do |backend|
TESTS.each do |json, expected|
test "json decodes #{json} with the #{backend} backend" do
prev = ActiveSupport.parse_json_times
ActiveSupport.parse_json_times = true
silence_warnings do
ActiveSupport::JSON.with_backend backend do
assert_equal expected, ActiveSupport::JSON.decode(json)
end
end
ActiveSupport.parse_json_times = prev
end
end
test "json decodes time json with time parsing disabled with the #{backend} backend" do
TESTS.each do |json, expected|
test "json decodes #{json}" do
prev = ActiveSupport.parse_json_times
ActiveSupport.parse_json_times = false
expected = {"a" => "2007-01-01 01:12:34 Z"}
ActiveSupport::JSON.with_backend backend do
assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"}))
ActiveSupport.parse_json_times = true
silence_warnings do
assert_equal expected, ActiveSupport::JSON.decode(json)
end
ActiveSupport.parse_json_times = prev
end
end
test "json decodes time json with time parsing disabled" do
prev = ActiveSupport.parse_json_times
ActiveSupport.parse_json_times = false
expected = {"a" => "2007-01-01 01:12:34 Z"}
assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"}))
ActiveSupport.parse_json_times = prev
end
def test_failed_json_decoding
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({: 1})) }
end