diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-11-23 12:36:03 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-11-23 12:36:03 -0800 |
commit | e9aa975cc9beb8a670c098b21a1dae514bdd8b9c (patch) | |
tree | d993a990ad5cf91195542df8a58d97ac40793cdb | |
parent | 4454ff1bcba360fb246acdd820a7cb4a2bfd8e11 (diff) | |
download | rails-e9aa975cc9beb8a670c098b21a1dae514bdd8b9c.tar.gz rails-e9aa975cc9beb8a670c098b21a1dae514bdd8b9c.tar.bz2 rails-e9aa975cc9beb8a670c098b21a1dae514bdd8b9c.zip |
Eliminate thread-local circular reference stack by passing it as an argument instead
-rw-r--r-- | activesupport/lib/active_support/json/encoding.rb | 28 |
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 |