class ActiveSupport::JSON::Encoding::Encoder
def as_json(value, use_options = true)
def as_json(value, use_options = true) check_for_circular_references(value) do use_options ? value.as_json(options_for(value)) : value.as_json end end
def check_for_circular_references(value)
def check_for_circular_references(value) unless @seen.add?(value.__id__) raise CircularReferenceError, 'object references itself' end yield ensure @seen.delete(value.__id__) end
def encode(value, use_options = true)
def encode(value, use_options = true) check_for_circular_references(value) do jsonified = use_options ? value.as_json(options_for(value)) : value.as_json jsonified.encode_json(self) end end
def escape(string)
def escape(string) Encoding.escape(string) end
def initialize(options = nil)
def initialize(options = nil) @options = options || {} @seen = Set.new end
def options_for(value)
def options_for(value) if value.is_a?(Array) || value.is_a?(Hash) # hashes and arrays need to get encoder in the options, so that # they can detect circular references. options.merge(:encoder => self) else options.dup end end