Class: ActiveSupport::JSON::Encoding::JSONGemEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/json/encoding.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ JSONGemEncoder

Returns a new instance of JSONGemEncoder.



78
79
80
# File 'lib/active_support/json/encoding.rb', line 78

def initialize(options = nil)
  @options = options || {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



76
77
78
# File 'lib/active_support/json/encoding.rb', line 76

def options
  @options
end

Instance Method Details

#encode(value) ⇒ Object

Encode the given object into a JSON string



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_support/json/encoding.rb', line 83

def encode(value)
  unless options.empty?
    value = value.as_json(options.dup.freeze)
  end
  json = stringify(jsonify(value))

  return json unless @options.fetch(:escape, true)

  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