From 7daade21c4ef296499f11d0413c91d9e029bc576 Mon Sep 17 00:00:00 2001 From: DevOrc Date: Tue, 18 Sep 2018 20:41:57 -0400 Subject: [PATCH] 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 --- .../org/springframework/boot/json/BasicJsonParser.java | 10 ++++++++++ .../boot/json/AbstractJsonParserTests.java | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java index ace90a3e83..fc8d8c0e23 100644 --- a/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java +++ b/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java @@ -125,9 +125,16 @@ public class BasicJsonParser implements JsonParser { int inObject = 0; int inList = 0; boolean inValue = false; + boolean inEscape = false; StringBuilder build = new StringBuilder(); while (index < json.length()) { char current = json.charAt(index); + if (inEscape) { + build.append(current); + index++; + inEscape = false; + continue; + } if (current == '{') { inObject++; } @@ -147,6 +154,9 @@ public class BasicJsonParser implements JsonParser { list.add(build.toString()); build.setLength(0); } + else if (current == '\\') { + inEscape = true; + } else { build.append(current); } diff --git a/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java b/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java index 662e97de88..e43d45369b 100644 --- a/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/json/AbstractJsonParserTests.java @@ -170,4 +170,11 @@ public abstract class AbstractJsonParserTests { this.parser.parseList("\n\t{}"); } + @Test + public void escapeQuote() { + String input = "{\"foo\": \"\\\"bar\\\"\"}"; + Map map = this.parser.parseMap(input); + assertThat(map.get("foo")).isEqualTo("\"bar\""); + } + }