Skip to content

Commit

Permalink
add forward slash to escape character
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Chagnon committed Sep 15, 2015
1 parent f7394ad commit fa28233
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
7 changes: 7 additions & 0 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
case '\\':
fbuffer_append(buffer, "\\\\", 2);
break;
case '/':
fbuffer_append(buffer, "\\/", 2);
break;
case '"':
fbuffer_append(buffer, "\\\"", 2);
break;
Expand Down Expand Up @@ -268,6 +271,10 @@ static void convert_UTF8_to_JSON(FBuffer *buffer, VALUE string)
escape = "\\\\";
escape_len = 2;
break;
case '/':
escape = "\\/";
escape_len = 2;
break;
case '"':
escape = "\\\"";
escape_len = 2;
Expand Down
1 change: 1 addition & 0 deletions java/src/json/ext/StringEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void encode(ByteList src, ByteList out) {
private void handleChar(int c) {
switch (c) {
case '"':
case '/':
case '\\':
escapeChar((char)c);
break;
Expand Down
9 changes: 5 additions & 4 deletions lib/json/pure/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module JSON
"\x1f" => '\u001f',
'"' => '\"',
'\\' => '\\\\',
'/' => '\\/',
} # :nodoc:

# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
Expand All @@ -42,15 +43,15 @@ module JSON
def utf8_to_json(string) # :nodoc:
string = string.dup
string.force_encoding(::Encoding::ASCII_8BIT)
string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
string.gsub!(/[\/"\\\x0-\x1f]/) { MAP[$&] }
string.force_encoding(::Encoding::UTF_8)
string
end

def utf8_to_json_ascii(string) # :nodoc:
string = string.dup
string.force_encoding(::Encoding::ASCII_8BIT)
string.gsub!(/["\\\x0-\x1f]/n) { MAP[$&] }
string.gsub!(/[\/"\\\x0-\x1f]/n) { MAP[$&] }
string.gsub!(/(
(?:
[\xc2-\xdf][\x80-\xbf] |
Expand Down Expand Up @@ -79,11 +80,11 @@ def valid_utf8?(string)
module_function :valid_utf8?
else
def utf8_to_json(string) # :nodoc:
string.gsub(/["\\\x0-\x1f]/n) { MAP[$&] }
string.gsub(/[\/"\\\x0-\x1f]/n) { MAP[$&] }
end

def utf8_to_json_ascii(string) # :nodoc:
string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
string = string.gsub(/[\/"\\\x0-\x1f]/) { MAP[$&] }
string.gsub!(/(
(?:
[\xc2-\xdf][\x80-\xbf] |
Expand Down
6 changes: 3 additions & 3 deletions tests/test_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ def test_backslash
assert_equal json, JSON.generate(data)
assert_equal data, JSON.parse(json)
#
json = '["/"]'
data = JSON.parse(json)
assert_equal ['/'], data
data = [ '/' ]
json = '["\/"]'
assert_equal json, JSON.generate(data)
assert_equal data, JSON.parse(json)
#
json = '["\""]'
data = JSON.parse(json)
Expand Down

0 comments on commit fa28233

Please sign in to comment.