Skip to content

Commit 02aadeb

Browse files
committed
Enhance the escaping flow
1 parent 297b701 commit 02aadeb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/main/java/com/cdpn/jsonautorepair/internal/EscapeProcessor.java

+6
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ private void handleClosedBracket(char currentChar, int position) {
106106

107107
private void handleCommaToFixMissingClosedQuote(char currentChar, int position) {
108108
char nextNonSpaceChar = findNextNonSpaceChar(position + 1);
109+
int nextNonSpaceCharPosition = getNextNonSpaceCharPosition(position + 1);
109110
if (nextNonSpaceChar == DOUBLE_QUOTE_CHAR ) {
111+
// We MUST ignore adding close quote if the next quote is a good close quote
112+
if(isValidCloseQuoteAtPosition(nextNonSpaceCharPosition)) {
113+
escapedJson.append(currentChar);
114+
return;
115+
}
110116
escapedJson.append(DOUBLE_QUOTE_CHAR);
111117
inQuotes = false;
112118
}

src/test/java/com/cdpn/jsonautorepair/JSONAutoRepairerTest.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,21 @@ public void repair_should_fix_missing_end_quote_of_value() {
7474
"""));
7575
}
7676

77-
78-
77+
@Test
78+
public void repair_should_treat_the_comma_before_a_good_close_quote_as_other_characters() {
79+
String originalJSON = """
80+
{
81+
"type": "5,
82+
"hits": [
83+
{
84+
"hitText": "hello,",
85+
"index": "2"
86+
}
87+
]
88+
}```
89+
""";
90+
assertNotNull(jsonAutoRepairer.repair(originalJSON));
91+
}
7992
@Test
8093
public void repair_should_return_good_JSON_string_output_when_the_string_is_a_valid_JSON() {
8194
String originalJSON = """

0 commit comments

Comments
 (0)