Module: ActiveSupport::JSON::Encoding
- Defined in:
- lib/active_support/json/encoding.rb
Overview
:nodoc:
Defined Under Namespace
Classes: CircularReferenceError
Constant Summary collapse
- ESCAPED_CHARS =
{ "\010" => '\b', "\f" => '\f', "\n" => '\n', "\r" => '\r', "\t" => '\t', '"' => '\"', '\\' => '\\\\', '>' => '\u003E', '<' => '\u003C', '&' => '\u0026' }
Class Attribute Summary collapse
-
.escape_html_entities_in_json ⇒ Object
Returns the value of attribute escape_html_entities_in_json.
-
.escape_regex ⇒ Object
Returns the value of attribute escape_regex.
-
.use_standard_json_time_format ⇒ Object
If true, use ISO 8601 format for dates and times.
Class Method Summary collapse
-
.encode(value, options = nil) ⇒ Object
Converts a Ruby object into a JSON string.
- .escape(string) ⇒ Object
Class Attribute Details
.escape_html_entities_in_json ⇒ Object
Returns the value of attribute escape_html_entities_in_json.
41 42 43 |
# File 'lib/active_support/json/encoding.rb', line 41 def escape_html_entities_in_json @escape_html_entities_in_json end |
.escape_regex ⇒ Object
Returns the value of attribute escape_regex.
40 41 42 |
# File 'lib/active_support/json/encoding.rb', line 40 def escape_regex @escape_regex end |
.use_standard_json_time_format ⇒ Object
If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
38 39 40 |
# File 'lib/active_support/json/encoding.rb', line 38 def use_standard_json_time_format @use_standard_json_time_format end |
Class Method Details
.encode(value, options = nil) ⇒ Object
Converts a Ruby object into a JSON string.
65 66 67 68 69 70 71 72 73 |
# File 'lib/active_support/json/encoding.rb', line 65 def encode(value, = nil) = {} unless Hash === seen = ([:seen] ||= []) raise CircularReferenceError, 'object references itself' if seen.include?(value) seen << value value.to_json() ensure seen.pop end |
.escape(string) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_support/json/encoding.rb', line 52 def escape(string) string = string.dup.force_encoding(::Encoding::BINARY) if string.respond_to?(:force_encoding) json = string. gsub(escape_regex) { |s| ESCAPED_CHARS[s] }. gsub(/([\xC0-\xDF][\x80-\xBF]| [\xE0-\xEF][\x80-\xBF]{2}| [\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s| s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/n, '\\\\u\&') } %("#{json}") end |