Module: ActiveSupport::JSON::Encoding

Defined in:
activesupport/lib/active_support/json/encoding.rb

Overview

:nodoc:

Defined Under Namespace

Classes: CircularReferenceError, Encoder

Constant Summary collapse

ESCAPED_CHARS =
{
"\x00" => '\u0000', "\x01" => '\u0001', "\x02" => '\u0002',
"\x03" => '\u0003', "\x04" => '\u0004', "\x05" => '\u0005',
"\x06" => '\u0006', "\x07" => '\u0007', "\x0B" => '\u000B',
"\x0E" => '\u000E', "\x0F" => '\u000F', "\x10" => '\u0010',
"\x11" => '\u0011', "\x12" => '\u0012', "\x13" => '\u0013',
"\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016',
"\x17" => '\u0017', "\x18" => '\u0018', "\x19" => '\u0019',
"\x1A" => '\u001A', "\x1B" => '\u001B', "\x1C" => '\u001C',
"\x1D" => '\u001D', "\x1E" => '\u001E', "\x1F" => '\u001F',
"\010" =>  '\b',
"\f"   =>  '\f',
"\n"   =>  '\n',
"\xe2\x80\xa8" => '\u2028',
"\xe2\x80\xa9" => '\u2029',
"\r"   =>  '\r',
"\t"   =>  '\t',
'"'    =>  '\"',
'\\'   =>  '\\\\',
'>'    =>  '\u003E',
'<'    =>  '\u003C',
'&'    =>  '\u0026',
"#{0xe2.chr}#{0x80.chr}#{0xa8.chr}" => '\u2028',
"#{0xe2.chr}#{0x80.chr}#{0xa9.chr}" => '\u2029',
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.encode_big_decimal_as_stringObject

If false, serializes BigDecimal objects as numeric instead of wrapping them in a string.



122
123
124
# File 'activesupport/lib/active_support/json/encoding.rb', line 122

def encode_big_decimal_as_string
  @encode_big_decimal_as_string
end

.escape_html_entities_in_jsonObject

Returns the value of attribute escape_html_entities_in_json



125
126
127
# File 'activesupport/lib/active_support/json/encoding.rb', line 125

def escape_html_entities_in_json
  @escape_html_entities_in_json
end

.escape_regexObject

Returns the value of attribute escape_regex



124
125
126
# File 'activesupport/lib/active_support/json/encoding.rb', line 124

def escape_regex
  @escape_regex
end

.use_standard_json_time_formatObject

If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.



118
119
120
# File 'activesupport/lib/active_support/json/encoding.rb', line 118

def use_standard_json_time_format
  @use_standard_json_time_format
end

Class Method Details

.escape(string) ⇒ Object



136
137
138
139
140
141
142
# File 'activesupport/lib/active_support/json/encoding.rb', line 136

def escape(string)
  string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
  json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }
  json = %("#{json}")
  json.force_encoding(::Encoding::UTF_8)
  json
end