Class: ActiveSupport::JSON::Encoding::ActiveSupportEncoder

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

Constant Summary collapse

ESCAPED_CHARS =
{
"\u0000" => '\u0000', "\u0001" => '\u0001',
"\u0002" => '\u0002', "\u0003" => '\u0003',
"\u0004" => '\u0004', "\u0005" => '\u0005',
"\u0006" => '\u0006', "\u0007" => '\u0007',
"\u0008" => '\b',     "\u0009" => '\t',
"\u000A" => '\n',     "\u000B" => '\u000B',
"\u000C" => '\f',     "\u000D" => '\r',
"\u000E" => '\u000E', "\u000F" => '\u000F',
"\u0010" => '\u0010', "\u0011" => '\u0011',
"\u0012" => '\u0012', "\u0013" => '\u0013',
"\u0014" => '\u0014', "\u0015" => '\u0015',
"\u0016" => '\u0016', "\u0017" => '\u0017',
"\u0018" => '\u0018', "\u0019" => '\u0019',
"\u001A" => '\u001A', "\u001B" => '\u001B',
"\u001C" => '\u001C', "\u001D" => '\u001D',
"\u001E" => '\u001E', "\u001F" => '\u001F',
"\u2028" => '\u2028', "\u2029" => '\u2029',
'"'  => '\"',
'\\' => '\\\\',
'>' => '\u003E',
'<' => '\u003C',
'&' => '\u0026'}
ESCAPE_REGEX_WITH_HTML =
/[\u0000-\u001F\u2028\u2029"\\><&]/u
ESCAPE_REGEX_WITHOUT_HTML =
/[\u0000-\u001F\u2028\u2029"\\]/u

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ ActiveSupportEncoder

Returns a new instance of ActiveSupportEncoder.


15
16
17
18
# File 'lib/active_support/json/encoding/active_support_encoder.rb', line 15

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.


13
14
15
# File 'lib/active_support/json/encoding/active_support_encoder.rb', line 13

def options
  @options
end

Class Method Details

.escape(string) ⇒ Object


76
77
78
79
80
# File 'lib/active_support/json/encoding/active_support_encoder.rb', line 76

def escape(string)
  string = string.encode ::Encoding::UTF_8, undef: :replace
  regex = Encoding.escape_html_entities_in_json ? ESCAPE_REGEX_WITH_HTML : ESCAPE_REGEX_WITHOUT_HTML
  %("#{string.gsub regex, ESCAPED_CHARS}")
end

Instance Method Details

#as_json(value, use_options = true) ⇒ Object

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


28
29
30
31
32
# File 'lib/active_support/json/encoding/active_support_encoder.rb', line 28

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


20
21
22
23
24
25
# File 'lib/active_support/json/encoding/active_support_encoder.rb', line 20

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.respond_to?(:encode_json) ? jsonified.encode_json(self) : encode(jsonified, false)
  end
end

#escape(string) ⇒ Object


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

def escape(string)
  self.class.escape(string)
end

#options_for(value) ⇒ Object


34
35
36
37
38
39
40
41
42
# File 'lib/active_support/json/encoding/active_support_encoder.rb', line 34

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