Skip to content

Commit

Permalink
fix issues/861
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Sep 20, 2024
1 parent 0a03ab7 commit 5dfbb61
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class JsonEscapeUtil {

// From RFC-8259 page 5

// %x22 / ; " quotation mark U+0022
// %x5C / ; \ reverse solidus U+005C
// %x2F / ; / solidus U+002F
// " quotation mark U+0022 -- escaped as \"
// \ reverse solidus U+005C -- escaped as \\
// / solidus U+002F -- escaped as \/

// %x62 / ; b backspace U+0008
// %x74 / ; t tab U+0009
// %x6E / ; n line feed U+000A
// %x66 / ; f form feed U+000C
// %x72 / ; r carriage return U+000D
// backspace U+0008 -- escaped as \b
// tab U+0009 -- escaped as \t
// line feed U+000A -- escaped as \n
// form feed U+000C -- escaped as \f
// carriage return U+000D -- escaped as \r

static {
for (char c = 0; c < ESCAPE_CODES_COUNT; c++) {
Expand Down Expand Up @@ -80,13 +80,19 @@ private static String _computeEscapeCodeBelowASCII32(char c) {
// %x22 / ; " quotation mark U+0022
// %x5C / ; \ reverse solidus U+005C

// " quotation mark U+0022 -- escaped as \"
// \ reverse solidus U+005C -- escaped as \\
// / solidus U+002F -- escaped as \/

static String getObligatoryEscapeCode(char c) {
if (c < 32)
return ESCAPE_CODES[c];
if (c == 0x22)
return "\\\"";
if (c == 0x5C)
if (c == 0x2F)
return "\\/";
if (c == 0x5C)
return "\\\\";

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class JsonEscapeUtilTest {

@Test
public void smokeTestEscapeCodes() {
public void smokeTestEscapeCodesUnder32() {
assertEquals("\\u0001", JsonEscapeUtil.ESCAPE_CODES[1]);
assertEquals("\\u0005", JsonEscapeUtil.ESCAPE_CODES[5]);
assertEquals("\\b", JsonEscapeUtil.ESCAPE_CODES[8]);
Expand Down Expand Up @@ -49,6 +49,22 @@ public void testEscapingLF() {
assertEquals("{\\nhello: "+'\\'+'"'+"wo\\nrld\\\"}", JsonEscapeUtil.jsonEscapeString(input));
}

@Test
public void testBackslash() {
String input = "{x:com\\foo}";
System.out.println(input);
assertEquals("{x:com\\\\foo}", JsonEscapeUtil.jsonEscapeString(input));
}

@Test
public void testForwardslash() {
String input = "{x:com/foo}";
System.out.println(input);
assertEquals("{x:com\\/foo}", JsonEscapeUtil.jsonEscapeString(input));
}



@Test
public void testEscapingTab() {
String input = "{hello: \"\tworld\"}";
Expand Down

0 comments on commit 5dfbb61

Please sign in to comment.