Permalink
Browse files

Revert "Simplify code in AS json encoder"

This reverts commit 046e27a.
Check #8815 (comment) for
details.
  • Loading branch information...
1 parent 15d693d commit 3762ee0baa2285bdc4a76d4d492fbe34a10cdc3a @guilleiguaran guilleiguaran committed Feb 25, 2013
Showing with 15 additions and 8 deletions.
  1. +15 −8 activesupport/lib/active_support/json/encoding.rb
View
23 activesupport/lib/active_support/json/encoding.rb
@@ -38,6 +38,7 @@ module Encoding #:nodoc:
class CircularReferenceError < StandardError; end
class Encoder
+ attr_reader :options
def initialize(options = nil)
@options = options || {}
@@ -62,9 +63,9 @@ def options_for(value)
if value.is_a?(Array) || value.is_a?(Hash)
# hashes and arrays need to get encoder in the options, so that
# they can detect circular references.
- @options.merge(:encoder => self)
+ options.merge(:encoder => self)
else
- @options.dup
+ options.dup
end
end
@@ -252,6 +253,18 @@ def as_json(options = nil) #:nodoc:
module Enumerable
def as_json(options = nil) #:nodoc:
+ to_a.as_json(options)
+ end
+end
+
+class Range
+ def as_json(options = nil) #:nodoc:
+ to_s
+ end
+end
+
+class Array
+ def as_json(options = nil) #:nodoc:
# use encoder as a proxy to call as_json on all elements, to protect from circular references
encoder = options && options[:encoder] || ActiveSupport::JSON::Encoding::Encoder.new(options)
map { |v| encoder.as_json(v, options) }
@@ -263,12 +276,6 @@ def encode_json(encoder) #:nodoc:
end
end
-class Range
- def as_json(options = nil) #:nodoc:
- to_s
- end
-end
-
class Hash
def as_json(options = nil) #:nodoc:
# create a subset of the hash by applying :only or :except

0 comments on commit 3762ee0

Please sign in to comment.