Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activesupport / lib / active_support / json / encoding.rb
blobadfbfd5f7296142bfaacf16ecaa6181e222e0fe7
1 require 'active_support/json/variable'
3 require 'active_support/json/encoders/object' # Require explicitly for rdoc.
4 Dir["#{File.dirname(__FILE__)}/encoders/**/*.rb"].each do |file|
5   basename = File.basename(file, '.rb')
6   unless basename == 'object'
7     require "active_support/json/encoders/#{basename}"
8   end
9 end
11 module ActiveSupport
12   module JSON
13     class CircularReferenceError < StandardError
14     end
16     class << self
17       REFERENCE_STACK_VARIABLE = :json_reference_stack #:nodoc:
19       # Converts a Ruby object into a JSON string.
20       def encode(value, options = {})
21         raise_on_circular_reference(value) do
22           value.send(:to_json, options)
23         end
24       end
26       protected
27         def raise_on_circular_reference(value) #:nodoc:
28           stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
29           raise CircularReferenceError, 'object references itself' if
30             stack.include? value
31           stack << value
32           yield
33         ensure
34           stack.pop
35         end
36     end
37   end
38 end