Module: Gon::JsonDumper

Defined in:
lib/gon/json_dumper.rb

Constant Summary collapse

JSON_ESCAPE_REGEXP =

Taken from ERB::Util

/[\u2028\u2029&><]/u
JSON_ESCAPE =
{
  "&" => '\u0026',
  ">" => '\u003e',
  "<" => '\u003c',
  "\u2028" => '\u2028',
  "\u2029" => '\u2029'
}

Class Method Summary collapse

Class Method Details

.dump(object) ⇒ Object



13
14
15
16
17
# File 'lib/gon/json_dumper.rb', line 13

def self.dump(object)
  dumped_json = MultiJson.dump object,
    mode: :compat, escape_mode: :xss_safe, time_format: :ruby
  escape(dumped_json)
end

.escape(json) ⇒ Object



19
20
21
# File 'lib/gon/json_dumper.rb', line 19

def self.escape(json)
  json.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
end