diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index 6e9d62bd16881bf8cee0f4c28be935edca0d962b..c8cac520275c275bacc7e6d07d1832f39019381d 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -232,9 +232,8 @@ def as_json(options = nil) #:nodoc: # use encoder as a proxy to call as_json on all values in the subset, to protect from circular references encoder = options && options[:encoder] || ActiveSupport::JSON::Encoding::Encoder.new(options) - pairs = subset.map { |k, v| [k.to_s, encoder.as_json(v)] } - result = self.is_a?(ActiveSupport::OrderedHash) ? ActiveSupport::OrderedHash.new : Hash.new - pairs.inject(result) { |hash, pair| hash[pair.first] = pair.last; hash } + result = self.is_a?(ActiveSupport::OrderedHash) ? ActiveSupport::OrderedHash : Hash + result[subset.map { |k, v| [k.to_s, encoder.as_json(v)] }] end def encode_json(encoder)