Skip to content
This repository
Browse code

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

…rgument instead
  • Loading branch information...
commit e9aa975cc9beb8a670c098b21a1dae514bdd8b9c 1 parent 4454ff1
Jeremy Kemper jeremy authored

Showing 1 changed file with 8 additions and 20 deletions. Show diff stats Hide diff stats

  1. +8 20 activesupport/lib/active_support/json/encoding.rb
28 activesupport/lib/active_support/json/encoding.rb
@@ -12,26 +12,14 @@ module JSON
12 12 class CircularReferenceError < StandardError
13 13 end
14 14
15   - class << self
16   - REFERENCE_STACK_VARIABLE = :json_reference_stack #:nodoc:
17   -
18   - # Converts a Ruby object into a JSON string.
19   - def encode(value, options = {})
20   - raise_on_circular_reference(value) do
21   - value.send(:to_json, options)
22   - end
23   - end
24   -
25   - protected
26   - def raise_on_circular_reference(value) #:nodoc:
27   - stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
28   - raise CircularReferenceError, 'object references itself' if
29   - stack.include? value
30   - stack << value
31   - yield
32   - ensure
33   - stack.pop
34   - end
  15 + # Converts a Ruby object into a JSON string.
  16 + def self.encode(value, options = {})
  17 + seen = (options[:seen] ||= [])
  18 + raise CircularReferenceError, 'object references itself' if seen.include?(value)
  19 + seen << value
  20 + value.send(:to_json, options)
  21 + ensure
  22 + seen.pop
35 23 end
36 24 end
37 25 end

0 comments on commit e9aa975

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