diff --git a/src/main/java/com/cdpn/jsonautorepair/internal/EscapeProcessor.java b/src/main/java/com/cdpn/jsonautorepair/internal/EscapeProcessor.java index cc39978..9fb12af 100644 --- a/src/main/java/com/cdpn/jsonautorepair/internal/EscapeProcessor.java +++ b/src/main/java/com/cdpn/jsonautorepair/internal/EscapeProcessor.java @@ -106,7 +106,13 @@ private void handleClosedBracket(char currentChar, int position) { private void handleCommaToFixMissingClosedQuote(char currentChar, int position) { char nextNonSpaceChar = findNextNonSpaceChar(position + 1); + int nextNonSpaceCharPosition = getNextNonSpaceCharPosition(position + 1); if (nextNonSpaceChar == DOUBLE_QUOTE_CHAR ) { + // We MUST ignore adding close quote if the next quote is a good close quote + if(isValidCloseQuoteAtPosition(nextNonSpaceCharPosition)) { + escapedJson.append(currentChar); + return; + } escapedJson.append(DOUBLE_QUOTE_CHAR); inQuotes = false; } diff --git a/src/test/java/com/cdpn/jsonautorepair/JSONAutoRepairerTest.java b/src/test/java/com/cdpn/jsonautorepair/JSONAutoRepairerTest.java index 5c8ad5d..e4acd4d 100644 --- a/src/test/java/com/cdpn/jsonautorepair/JSONAutoRepairerTest.java +++ b/src/test/java/com/cdpn/jsonautorepair/JSONAutoRepairerTest.java @@ -74,8 +74,21 @@ public void repair_should_fix_missing_end_quote_of_value() { """)); } - - + @Test + public void repair_should_treat_the_comma_before_a_good_close_quote_as_other_characters() { + String originalJSON = """ + { + "type": "5, + "hits": [ + { + "hitText": "hello,", + "index": "2" + } + ] + }``` + """; + assertNotNull(jsonAutoRepairer.repair(originalJSON)); + } @Test public void repair_should_return_good_JSON_string_output_when_the_string_is_a_valid_JSON() { String originalJSON = """