提交 e9aa975c 编写于 作者: J Jeremy Kemper

Eliminate thread-local circular reference stack by passing it as an argument instead

上级 4454ff1b
......@@ -12,26 +12,14 @@ module JSON
class CircularReferenceError < StandardError
end
class << self
REFERENCE_STACK_VARIABLE = :json_reference_stack #:nodoc:
# Converts a Ruby object into a JSON string.
def encode(value, options = {})
raise_on_circular_reference(value) do
value.send(:to_json, options)
end
end
protected
def raise_on_circular_reference(value) #:nodoc:
stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
raise CircularReferenceError, 'object references itself' if
stack.include? value
stack << value
yield
ensure
stack.pop
end
# Converts a Ruby object into a JSON string.
def self.encode(value, options = {})
seen = (options[:seen] ||= [])
raise CircularReferenceError, 'object references itself' if seen.include?(value)
seen << value
value.send(:to_json, options)
ensure
seen.pop
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册