lib/active_support/json/encoders/enumerable.rb in activesupport-2.3.2 vs lib/active_support/json/encoders/enumerable.rb in activesupport-2.3.3
- old
+ new
@@ -1,12 +1,17 @@
module Enumerable
- # Returns a JSON string representing the enumerable. Any +options+
- # given will be passed on to its elements. For example:
- #
- # users = User.find(:all)
- # # => users.to_json(:only => :name)
- #
- # will pass the <tt>:only => :name</tt> option to each user.
- def to_json(options = {}) #:nodoc:
- "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ', '}]"
+ # Coerces the enumerable to an array for JSON encoding.
+ def as_json(options = nil) #:nodoc:
+ to_a
+ end
+end
+
+class Array
+ # Returns a JSON string representing the Array. +options+ are passed to each element.
+ def to_json(options = nil) #:nodoc:
+ "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ','}]"
+ end
+
+ def as_json(options = nil) #:nodoc:
+ self
end
end