Class: ActiveSupport::JSON::Encoding::JSONGemCoderEncoder
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | activesupport/lib/active_support/json/encoding.rb |
Constant Summary
-
CODER =
# File 'activesupport/lib/active_support/json/encoding.rb', line 152::JSON::Coder.new do |value, is_key| json_value = value.as_json # Keep compatibility by calling to_s on non-String keys next json_value.to_s if is_key # Handle objects returning self from as_json if json_value.equal?(value) next ::JSON::Fragment.new(::JSON.generate(json_value)) end # Handle objects not returning JSON-native types from as_json count = 5 until JSON_NATIVE_TYPES.include?(json_value.class) raise SystemStackError if count == 0 json_value = json_value.as_json count -= 1 end json_value end
-
JSON_NATIVE_TYPES =
# File 'activesupport/lib/active_support/json/encoding.rb', line 151[Hash, Array, Float, String, Symbol, Integer, NilClass, TrueClass, FalseClass, ::JSON::Fragment].freeze
Class Method Summary
- .new(options = nil) ⇒ JSONGemCoderEncoder constructor
Instance Method Summary
-
#encode(value)
Encode the given object into a
::ActiveSupport::JSON
string.
Constructor Details
.new(options = nil) ⇒ JSONGemCoderEncoder
# File 'activesupport/lib/active_support/json/encoding.rb', line 171
def initialize( = nil) if = .dup @escape = .delete(:escape) { true } @options = .freeze else @escape = true @options = {}.freeze end end
Instance Method Details
#encode(value)
Encode the given object into a ::ActiveSupport::JSON
string
# File 'activesupport/lib/active_support/json/encoding.rb', line 183
def encode(value) value = value.as_json(@options) unless @options.empty? json = CODER.dump(value) return json unless @escape json.force_encoding(::Encoding::BINARY) if @options.fetch(:escape_html_entities, Encoding.escape_html_entities_in_json) if Encoding.escape_js_separators_in_json json.gsub!(FULL_ESCAPE_REGEX, ESCAPED_CHARS) else json.gsub!(HTML_ENTITIES_REGEX, ESCAPED_CHARS) end elsif Encoding.escape_js_separators_in_json json.gsub!(JS_SEPARATORS_REGEX, ESCAPED_CHARS) end json.force_encoding(::Encoding::UTF_8) end