diff --git a/activesupport/lib/active_support/json/encoders/hash.rb b/activesupport/lib/active_support/json/encoders/hash.rb index cfe21e4..db03437 100644 --- a/activesupport/lib/active_support/json/encoders/hash.rb +++ b/activesupport/lib/active_support/json/encoders/hash.rb @@ -31,6 +31,7 @@ class Hash # allowing the posts association in the User model to be converted to JSON # as well. def to_json(options = nil) #:nodoc: + options = options.to_h unless options.is_a? Hash hash = as_json(options) result = '{' diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index ab8e4e8..97ddb06 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -72,6 +72,10 @@ class TestJSONEncoding < Test::Unit::TestCase assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json end + def test_hash_encoding_using_json + assert_equal %({\"a\":\"b\"}), JSON.dump(:a => :b) + end + def test_utf8_string_encoded_properly_when_kcode_is_utf8 with_kcode 'UTF8' do result = ActiveSupport::JSON.encode('€2.99')