Skip to content

Commit 7daade2

Browse files
ncharlton02snicoll
authored andcommitted
Support escaped characters in BasicJsonParser
This commit adds the ability to have escaped characters, like the quote, when using the BasicJsonParser. It also adds a short test for escaped quotes. Closes gh-14521
1 parent a91f9b6 commit 7daade2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,16 @@ private List<String> tokenize(String json) {
125125
int inObject = 0;
126126
int inList = 0;
127127
boolean inValue = false;
128+
boolean inEscape = false;
128129
StringBuilder build = new StringBuilder();
129130
while (index < json.length()) {
130131
char current = json.charAt(index);
132+
if (inEscape) {
133+
build.append(current);
134+
index++;
135+
inEscape = false;
136+
continue;
137+
}
131138
if (current == '{') {
132139
inObject++;
133140
}
@@ -147,6 +154,9 @@ private List<String> tokenize(String json) {
147154
list.add(build.toString());
148155
build.setLength(0);
149156
}
157+
else if (current == '\\') {
158+
inEscape = true;
159+
}
150160
else {
151161
build.append(current);
152162
}

spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,11 @@ public void listWithLeadingWhitespaceMapThrowsARuntimeException() {
170170
this.parser.parseList("\n\t{}");
171171
}
172172

173+
@Test
174+
public void escapeQuote() {
175+
String input = "{\"foo\": \"\\\"bar\\\"\"}";
176+
Map<String, Object> map = this.parser.parseMap(input);
177+
assertThat(map.get("foo")).isEqualTo("\"bar\"");
178+
}
179+
173180
}

0 commit comments

Comments
 (0)