diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index 8ba45f7..47df7ff 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -177,7 +177,11 @@ end class Array def as_json(options = nil) self end #:nodoc: - def encode_json(encoder) "[#{map { |v| encoder.encode(v) } * ','}]" end #:nodoc: + def encode_json(encoder) + collection_root = encoder.options[:collection_root] rescue nil + results = "[#{map { |v| encoder.encode(v) } * ','}]" + collection_root.nil? ? results : "{\"#{collection_root}\" : #{results}}" + end #:nodoc: end class Hash diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index cf9a635..6bba26d 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -115,6 +115,10 @@ class TestJSONEncoding < Test::Unit::TestCase ActiveSupport.use_standard_json_time_format = false end + def test_enumerable_to_json_allows_collection_root + assert_equal %({\"myCollection\" : [{\"a\":1},{\"a\":2}]}), ActiveSupport::JSON.encode([{:a => 1}, {:a => 2}], :collection_root => "myCollection") + end + def test_nested_hash_with_float assert_nothing_raised do hash = {