diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2020-01-30 12:48:05 +0100 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2020-07-04 12:14:47 +0200 |
commit | 8a18cf6a14fc1fae54b3d581434a255c9d98ceea (patch) | |
tree | 059a4f393a91df8506b4212e5158282fe1b07402 /lib | |
parent | 617428650d48db8a4b5439968e441f859605c6f3 (diff) | |
download | json-8a18cf6a14fc1fae54b3d581434a255c9d98ceea.tar.gz |
Add an option to escape forward slash character
Squashed commit of the following:
commit 26d181059989279a79c433cedcd893b4f52e42ee
Author: Francois Chagnon <francois.chagnon@jadedpixel.com>
Date: Tue Sep 15 21:17:34 2015 +0000
add config options for escape_slash
commit fa282334051b16df91ca097dd7304b46f3bc7719
Author: Francois Chagnon <francois.chagnon@jadedpixel.com>
Date: Mon Feb 9 21:09:33 2015 +0000
add forward slash to escape character
Diffstat (limited to 'lib')
-rw-r--r-- | lib/json/common.rb | 3 | ||||
-rw-r--r-- | lib/json/pure/generator.rb | 30 |
2 files changed, 26 insertions, 7 deletions
diff --git a/lib/json/common.rb b/lib/json/common.rb index 132e9c8..ed4e047 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -436,12 +436,13 @@ module JSON # Sets or returns the default options for the JSON.dump method. # Initially: # opts = JSON.dump_default_options - # opts # => {:max_nesting=>false, :allow_nan=>true} + # opts # => {:max_nesting=>false, :allow_nan=>true, :escape_slash=>false} attr_accessor :dump_default_options end self.dump_default_options = { :max_nesting => false, :allow_nan => true, + :escape_slash => false, } # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb index 471af63..ef1c2e8 100644 --- a/lib/json/pure/generator.rb +++ b/lib/json/pure/generator.rb @@ -37,20 +37,26 @@ module JSON '\\' => '\\\\', } # :nodoc: + ESCAPE_SLASH_MAP = MAP.merge( + '/' => '\\/', + ) + # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with # UTF16 big endian characters as \u????, and return it. - def utf8_to_json(string) # :nodoc: + def utf8_to_json(string, escape_slash = false) # :nodoc: string = string.dup string.force_encoding(::Encoding::ASCII_8BIT) - string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] } + map = escape_slash ? ESCAPE_SLASH_MAP : MAP + string.gsub!(/[\/"\\\x0-\x1f]/) { map[$&] || $& } string.force_encoding(::Encoding::UTF_8) string end - def utf8_to_json_ascii(string) # :nodoc: + def utf8_to_json_ascii(string, escape_slash = false) # :nodoc: string = string.dup string.force_encoding(::Encoding::ASCII_8BIT) - string.gsub!(/["\\\x0-\x1f]/n) { MAP[$&] } + map = escape_slash ? ESCAPE_SLASH_MAP : MAP + string.gsub!(/[\/"\\\x0-\x1f]/n) { map[$&] || $& } string.gsub!(/( (?: [\xc2-\xdf][\x80-\xbf] | @@ -109,6 +115,7 @@ module JSON # * *space_before*: a string that is put before a : pair delimiter (default: ''), # * *object_nl*: a string that is put at the end of a JSON object (default: ''), # * *array_nl*: a string that is put at the end of a JSON array (default: ''), + # * *escape_slash*: true if forward slash (/) should be escaped (default: false) # * *check_circular*: is deprecated now, use the :max_nesting option instead, # * *max_nesting*: sets the maximum level of data structure nesting in # the generated JSON, max_nesting = 0 if no maximum should be checked. @@ -123,6 +130,7 @@ module JSON @array_nl = '' @allow_nan = false @ascii_only = false + @escape_slash = false @buffer_initial_length = 1024 configure opts end @@ -148,6 +156,10 @@ module JSON # the generated JSON, max_nesting = 0 if no maximum is checked. attr_accessor :max_nesting + # If this attribute is set to true, forward slashes will be escaped in + # all json strings. + attr_accessor :escape_slash + # :stopdoc: attr_reader :buffer_initial_length @@ -187,6 +199,11 @@ module JSON @ascii_only end + # Returns true, if forward slashes are escaped. Otherwise returns false. + def escape_slash? + @escape_slash + end + # Configure this State instance with the Hash _opts_, and return # itself. def configure(opts) @@ -209,6 +226,7 @@ module JSON @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only) @depth = opts[:depth] || 0 @buffer_initial_length ||= opts[:buffer_initial_length] + @escape_slash = !!opts[:escape_slash] if opts.key?(:escape_slash) if !opts.key?(:max_nesting) # defaults to 100 @max_nesting = 100 @@ -399,9 +417,9 @@ module JSON string = encode(::Encoding::UTF_8) end if state.ascii_only? - '"' << JSON.utf8_to_json_ascii(string) << '"' + '"' << JSON.utf8_to_json_ascii(string, state.escape_slash) << '"' else - '"' << JSON.utf8_to_json(string) << '"' + '"' << JSON.utf8_to_json(string, state.escape_slash) << '"' end end |