aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-11-23 12:36:03 -0800
committerJeremy Kemper <jeremy@bitsweat.net>2008-11-23 12:36:03 -0800
commite9aa975cc9beb8a670c098b21a1dae514bdd8b9c (patch)
treed993a990ad5cf91195542df8a58d97ac40793cdb /activesupport
parent4454ff1bcba360fb246acdd820a7cb4a2bfd8e11 (diff)
downloadrails-e9aa975cc9beb8a670c098b21a1dae514bdd8b9c.tar.gz
rails-e9aa975cc9beb8a670c098b21a1dae514bdd8b9c.tar.bz2
rails-e9aa975cc9beb8a670c098b21a1dae514bdd8b9c.zip
Eliminate thread-local circular reference stack by passing it as an argument instead
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/json/encoding.rb28
1 files changed, 8 insertions, 20 deletions
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