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 120::JSON::Coder.new do |value| json_value = value.as_json # 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 119[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 137
def initialize( = nil) @options = ? .dup.freeze : {}.freeze end
Instance Method Details
#encode(value)
Encode the given object into a ::ActiveSupport::JSON
string
# File 'activesupport/lib/active_support/json/encoding.rb', line 142
def encode(value) value = value.as_json(@options) unless @options.empty? json = CODER.dump(value) # Rails does more escaping than the JSON gem natively does (we # escape \u2028 and \u2029 and optionally >, <, & to work around # certain browser problems). json.force_encoding(::Encoding::BINARY) if @options.fetch(:escape_html_entities, Encoding.escape_html_entities_in_json) json.gsub!(ESCAPE_REGEX_WITH_HTML_ENTITIES, ESCAPED_CHARS) else json.gsub!(ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, ESCAPED_CHARS) end json.force_encoding(::Encoding::UTF_8) end