File: json_dumper.rb

package info (click to toggle)
ruby-gon 6.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 504 kB
  • sloc: ruby: 1,383; makefile: 9
file content (23 lines) | stat: -rw-r--r-- 517 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Gon
  module JsonDumper
    # Taken from ERB::Util
    JSON_ESCAPE_REGEXP	=	/[\u2028\u2029&><]/u
    JSON_ESCAPE	=	{
      "&" => '\u0026',
      ">" => '\u003e',
      "<" => '\u003c',
      "\u2028" => '\u2028',
      "\u2029" => '\u2029'
    }

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

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