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

Inherits:
Object
  • Object
show all
Defined in:
activesupport/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.



32
33
34
# File 'activesupport/lib/active_support/json/encoding.rb', line 32

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'activesupport/lib/active_support/json/encoding.rb', line 30

def options
  @options
end

Instance Method Details

#encode(value) ⇒ Object

Encode the given object into a JSON string



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'activesupport/lib/active_support/json/encoding.rb', line 37

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

  # Rails does more escaping than the JSON gem natively does (we
  # escape \u2028 and \u2029 and optionally >, <, & to work around
  # certain browser problems).
  if Encoding.escape_html_entities_in_json
    json.gsub!(">", '\u003e')
    json.gsub!("<", '\u003c')
    json.gsub!("&", '\u0026')
  end
  json.gsub!("\u2028", '\u2028')
  json.gsub!("\u2029", '\u2029')
  json
end