Class: ActiveSupport::JSON::Encoding::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/json/encoding.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Encoder

Returns a new instance of Encoder.



43
44
45
46
# File 'lib/active_support/json/encoding.rb', line 43

def initialize(options = nil)
  @options = options || {}
  @seen = Set.new
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



41
42
43
# File 'lib/active_support/json/encoding.rb', line 41

def options
  @options
end

Instance Method Details

#as_json(value, use_options = true) ⇒ Object

like encode, but only calls as_json, without encoding to string.



56
57
58
59
60
# File 'lib/active_support/json/encoding.rb', line 56

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

#encode(value, use_options = true) ⇒ Object



48
49
50
51
52
53
# File 'lib/active_support/json/encoding.rb', line 48

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

#escape(string) ⇒ Object



72
73
74
# File 'lib/active_support/json/encoding.rb', line 72

def escape(string)
  Encoding.escape(string)
end

#options_for(value) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/active_support/json/encoding.rb', line 62

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