mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Revert "Simplify code in AS json encoder"
This reverts commit 046e27a733
.
Check https://github.com/rails/rails/pull/8815#issuecomment-14026122 for
details.
This commit is contained in:
parent
15d693df93
commit
3762ee0baa
1 changed files with 15 additions and 8 deletions
|
@ -38,6 +38,7 @@ module ActiveSupport
|
||||||
class CircularReferenceError < StandardError; end
|
class CircularReferenceError < StandardError; end
|
||||||
|
|
||||||
class Encoder
|
class Encoder
|
||||||
|
attr_reader :options
|
||||||
|
|
||||||
def initialize(options = nil)
|
def initialize(options = nil)
|
||||||
@options = options || {}
|
@options = options || {}
|
||||||
|
@ -62,9 +63,9 @@ module ActiveSupport
|
||||||
if value.is_a?(Array) || value.is_a?(Hash)
|
if value.is_a?(Array) || value.is_a?(Hash)
|
||||||
# hashes and arrays need to get encoder in the options, so that
|
# hashes and arrays need to get encoder in the options, so that
|
||||||
# they can detect circular references.
|
# they can detect circular references.
|
||||||
@options.merge(:encoder => self)
|
options.merge(:encoder => self)
|
||||||
else
|
else
|
||||||
@options.dup
|
options.dup
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -251,6 +252,18 @@ class Regexp
|
||||||
end
|
end
|
||||||
|
|
||||||
module Enumerable
|
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:
|
def as_json(options = nil) #:nodoc:
|
||||||
# use encoder as a proxy to call as_json on all elements, to protect from circular references
|
# 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)
|
encoder = options && options[:encoder] || ActiveSupport::JSON::Encoding::Encoder.new(options)
|
||||||
|
@ -263,12 +276,6 @@ module Enumerable
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Range
|
|
||||||
def as_json(options = nil) #:nodoc:
|
|
||||||
to_s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Hash
|
class Hash
|
||||||
def as_json(options = nil) #:nodoc:
|
def as_json(options = nil) #:nodoc:
|
||||||
# create a subset of the hash by applying :only or :except
|
# create a subset of the hash by applying :only or :except
|
||||||
|
|
Loading…
Reference in a new issue