Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/activesupport/lib/active_support/json/encoding.rb

Revision 9238, 1.0 kB (checked in by rick, 1 month ago)

Add config.active_support.escape_html_entities_in_json to allow disabling of html entity escaping. [rick]

Line 
1 require 'active_support/json/variable'
2 require 'active_support/json/encoders/object' # Require explicitly for rdoc.
3 Dir["#{File.dirname(__FILE__)}/encoders/**/*.rb"].each do |file|
4   basename = File.basename(file, '.rb')
5   unless basename == 'object'
6     require "active_support/json/encoders/#{basename}"
7   end
8 end
9
10 module ActiveSupport
11   module JSON
12     class CircularReferenceError < StandardError
13     end
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
35     end
36   end
37 end
Note: See TracBrowser for help on using the browser.