Skip to content

Commit fa28233

Browse files
author
Francois Chagnon
committed
add forward slash to escape character
1 parent f7394ad commit fa28233

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

ext/json/ext/generator/generator.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
171171
case '\\':
172172
fbuffer_append(buffer, "\\\\", 2);
173173
break;
174+
case '/':
175+
fbuffer_append(buffer, "\\/", 2);
176+
break;
174177
case '"':
175178
fbuffer_append(buffer, "\\\"", 2);
176179
break;
@@ -268,6 +271,10 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
268271
escape = "\\\\";
269272
escape_len = 2;
270273
break;
274+
case '/':
275+
escape = "\\/";
276+
escape_len = 2;
277+
break;
271278
case '"':
272279
escape = "\\\"";
273280
escape_len = 2;

java/src/json/ext/StringEncoder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void encode(ByteList src, ByteList out) {
5050
private void handleChar(int c) {
5151
switch (c) {
5252
case '"':
53+
case '/':
5354
case '\\':
5455
escapeChar((char)c);
5556
break;

lib/json/pure/generator.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module JSON
3434
"\x1f" => '\u001f',
3535
'"' => '\"',
3636
'\\' => '\\\\',
37+
'/' => '\\/',
3738
} # :nodoc:
3839

3940
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
@@ -42,15 +43,15 @@ module JSON
4243
def utf8_to_json(string) # :nodoc:
4344
string = string.dup
4445
string.force_encoding(::Encoding::ASCII_8BIT)
45-
string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
46+
string.gsub!(/[\/"\\\x0-\x1f]/) { MAP[$&] }
4647
string.force_encoding(::Encoding::UTF_8)
4748
string
4849
end
4950

5051
def utf8_to_json_ascii(string) # :nodoc:
5152
string = string.dup
5253
string.force_encoding(::Encoding::ASCII_8BIT)
53-
string.gsub!(/["\\\x0-\x1f]/n) { MAP[$&] }
54+
string.gsub!(/[\/"\\\x0-\x1f]/n) { MAP[$&] }
5455
string.gsub!(/(
5556
(?:
5657
[\xc2-\xdf][\x80-\xbf] |
@@ -79,11 +80,11 @@ def valid_utf8?(string)
7980
module_function :valid_utf8?
8081
else
8182
def utf8_to_json(string) # :nodoc:
82-
string.gsub(/["\\\x0-\x1f]/n) { MAP[$&] }
83+
string.gsub(/[\/"\\\x0-\x1f]/n) { MAP[$&] }
8384
end
8485

8586
def utf8_to_json_ascii(string) # :nodoc:
86-
string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
87+
string = string.gsub(/[\/"\\\x0-\x1f]/) { MAP[$&] }
8788
string.gsub!(/(
8889
(?:
8990
[\xc2-\xdf][\x80-\xbf] |

tests/test_json.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ def test_backslash
409409
assert_equal json, JSON.generate(data)
410410
assert_equal data, JSON.parse(json)
411411
#
412-
json = '["/"]'
413-
data = JSON.parse(json)
414-
assert_equal ['/'], data
412+
data = [ '/' ]
413+
json = '["\/"]'
415414
assert_equal json, JSON.generate(data)
415+
assert_equal data, JSON.parse(json)
416416
#
417417
json = '["\""]'
418418
data = JSON.parse(json)

0 commit comments

Comments
 (0)