Skip to content

Commit 5dfbb61

Browse files
committed
fix issues/861
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 0a03ab7 commit 5dfbb61

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

logback-core/src/main/java/ch/qos/logback/core/encoder/JsonEscapeUtil.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public class JsonEscapeUtil {
2424

2525
// From RFC-8259 page 5
2626

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

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

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

83+
// " quotation mark U+0022 -- escaped as \"
84+
// \ reverse solidus U+005C -- escaped as \\
85+
// / solidus U+002F -- escaped as \/
86+
8387
static String getObligatoryEscapeCode(char c) {
8488
if (c < 32)
8589
return ESCAPE_CODES[c];
8690
if (c == 0x22)
8791
return "\\\"";
88-
if (c == 0x5C)
92+
if (c == 0x2F)
8993
return "\\/";
94+
if (c == 0x5C)
95+
return "\\\\";
9096

9197
return null;
9298
}

logback-core/src/test/java/ch/qos/logback/core/encoder/JsonEscapeUtilTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class JsonEscapeUtilTest {
2222

2323
@Test
24-
public void smokeTestEscapeCodes() {
24+
public void smokeTestEscapeCodesUnder32() {
2525
assertEquals("\\u0001", JsonEscapeUtil.ESCAPE_CODES[1]);
2626
assertEquals("\\u0005", JsonEscapeUtil.ESCAPE_CODES[5]);
2727
assertEquals("\\b", JsonEscapeUtil.ESCAPE_CODES[8]);
@@ -49,6 +49,22 @@ public void testEscapingLF() {
4949
assertEquals("{\\nhello: "+'\\'+'"'+"wo\\nrld\\\"}", JsonEscapeUtil.jsonEscapeString(input));
5050
}
5151

52+
@Test
53+
public void testBackslash() {
54+
String input = "{x:com\\foo}";
55+
System.out.println(input);
56+
assertEquals("{x:com\\\\foo}", JsonEscapeUtil.jsonEscapeString(input));
57+
}
58+
59+
@Test
60+
public void testForwardslash() {
61+
String input = "{x:com/foo}";
62+
System.out.println(input);
63+
assertEquals("{x:com\\/foo}", JsonEscapeUtil.jsonEscapeString(input));
64+
}
65+
66+
67+
5268
@Test
5369
public void testEscapingTab() {
5470
String input = "{hello: \"\tworld\"}";

0 commit comments

Comments
 (0)