Module: ActiveSupport::JSON::Encoding

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

Overview

:nodoc:

Defined Under Namespace

Classes: JSONGemCoderEncoder, JSONGemEncoder

Constant Summary collapse

U2028 =
-"\u2028".b
U2029 =
-"\u2029".b
ESCAPED_CHARS =
{
  U2028 => '\u2028'.b,
  U2029 => '\u2029'.b,
  ">".b => '\u003e'.b,
  "<".b => '\u003c'.b,
  "&".b => '\u0026'.b,
}
HTML_ENTITIES_REGEX =
Regexp.union(*(ESCAPED_CHARS.keys - [U2028, U2029]))
FULL_ESCAPE_REGEX =
Regexp.union(*ESCAPED_CHARS.keys)
JS_SEPARATORS_REGEX =
Regexp.union(U2028, U2029)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.escape_html_entities_in_jsonObject

If true, encode >, <, & as escaped unicode sequences (e.g. > as \u003e) as a safety measure.



214
215
216
# File 'lib/active_support/json/encoding.rb', line 214

def escape_html_entities_in_json
  @escape_html_entities_in_json
end

.escape_js_separators_in_jsonObject

If true, encode LINE SEPARATOR (U+2028) and PARAGRAPH SEPARATOR (U+2029) as escaped unicode sequences (‘\u2028’ and ‘\u2029’). Historically these characters were not valid inside JavaScript strings but that changed in ECMAScript 2019. As such it’s no longer a concern in modern browsers: caniuse.com/mdn-javascript_builtins_json_json_superset.



221
222
223
# File 'lib/active_support/json/encoding.rb', line 221

def escape_js_separators_in_json
  @escape_js_separators_in_json
end

.json_encoderObject

Sets the encoder used by Rails to encode Ruby objects into JSON strings in Object#to_json and ActiveSupport::JSON.encode.



229
230
231
# File 'lib/active_support/json/encoding.rb', line 229

def json_encoder
  @json_encoder
end

.time_precisionObject

Sets the precision of encoded time values. Defaults to 3 (equivalent to millisecond precision)



225
226
227
# File 'lib/active_support/json/encoding.rb', line 225

def time_precision
  @time_precision
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.



210
211
212
# File 'lib/active_support/json/encoding.rb', line 210

def use_standard_json_time_format
  @use_standard_json_time_format
end

Class Method Details

.encode_without_escape(value) ⇒ Object

:nodoc:



241
242
243
# File 'lib/active_support/json/encoding.rb', line 241

def encode_without_escape(value) # :nodoc:
  @encoder_without_escape.encode(value)
end

.encode_without_options(value) ⇒ Object

:nodoc:



237
238
239
# File 'lib/active_support/json/encoding.rb', line 237

def encode_without_options(value) # :nodoc:
  @encoder_without_options.encode(value)
end