Class: RapidJSON::ActiveSupportEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/rapidjson/active_support_encoder.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ ActiveSupportEncoder

Returns a new instance of ActiveSupportEncoder.



3
4
5
6
7
8
9
10
11
12
# File 'lib/rapidjson/active_support_encoder.rb', line 3

def initialize(options = nil)
  @options = options
  @coder = RapidJSON::Coder.new do |value, is_key|
    if is_key
      value.to_s
    else
      value.as_json
    end
  end
end

Instance Method Details

#encode(value) ⇒ Object

Encode the given object into a JSON string



15
16
17
18
19
20
21
22
23
24
# File 'lib/rapidjson/active_support_encoder.rb', line 15

def encode(value)
  if @options && !@options.empty?
    value = value.as_json(@options.dup)
  end
  json = @coder.dump(value)
  if ActiveSupport::JSON::Encoding.escape_html_entities_in_json
    json = RapidJSON.json_escape(json)
  end
  json
end