Skip to content
Browse files

Eliminate thread-local circular reference stack by passing it as an a…

…rgument instead
  • Loading branch information...
1 parent 4454ff1 commit e9aa975cc9beb8a670c098b21a1dae514bdd8b9c @jeremy jeremy committed Nov 23, 2008
Showing with 8 additions and 20 deletions.
  1. +8 −20 activesupport/lib/active_support/json/encoding.rb
View
28 activesupport/lib/active_support/json/encoding.rb
@@ -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

0 comments on commit e9aa975

Please sign in to comment.
Something went wrong with that request. Please try again.