lib/active_support/json/encoding.rb in activesupport-3.0.20 vs lib/active_support/json/encoding.rb in activesupport-3.1.0.beta1
- old
+ new
@@ -151,10 +151,16 @@
instance_values
end
end
end
+class Struct
+ def as_json(options = nil) #:nodoc:
+ Hash[members.zip(values)]
+ end
+end
+
class TrueClass
AS_JSON = ActiveSupport::JSON::Variable.new('true').freeze
def as_json(options = nil) AS_JSON end #:nodoc:
end
@@ -230,12 +236,11 @@
self
end
# 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)
# values are encoded with use_options = false, because we don't want hash representations from ActiveModel to be
# processed once again with as_json with options, as this could cause unexpected results (i.e. missing fields);