From e9aa975cc9beb8a670c098b21a1dae514bdd8b9c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 23 Nov 2008 12:36:03 -0800 Subject: Eliminate thread-local circular reference stack by passing it as an argument instead --- activesupport/lib/active_support/json/encoding.rb | 28 +++++++---------------- 1 file changed, 8 insertions(+), 20 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index 8650e34228..d7caffbab3 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -12,26 +12,14 @@ module ActiveSupport 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 -- cgit v1.2.3