Skip to content

Commit

Permalink
Merge pull request #3 from coderphonui/enhance-fixing-missing-end-quote
Browse files Browse the repository at this point in the history
enhance-fixing-missing-end-quote
  • Loading branch information
coderphonui authored Nov 28, 2024
2 parents ea751d2 + 0898af0 commit e863de6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
1 change: 1 addition & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
name: jacoco-report
path: target/site/jacoco/
- name: Commit and push
if: github.event_name != 'pull_request'
run: |
cd badges
if [[ `git status --porcelain *.svg` ]]; then
Expand Down
59 changes: 43 additions & 16 deletions src/main/java/com/cdpn/jsonautorepair/internal/EscapeProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,6 @@ private boolean hasNextQuoteRightAfterCurrentQuoteWithoutComma(int position) {
}

private void handleNonQuoteCharacter(char currentChar, int position) {
if (currentChar == COMMA) {
char nextNonSpaceChar = findNextNonSpaceChar(position + 1);
if (inQuotes) {
if (nextNonSpaceChar == DOUBLE_QUOTE_CHAR ) {
escapedJson.append(DOUBLE_QUOTE_CHAR);
inQuotes = false;
}
if (nextNonSpaceChar == CLOSED_BRACKET ) {
escapedJson.append(DOUBLE_QUOTE_CHAR);
inQuotes = false;
return;
}
}
escapedJson.append(currentChar);
return;
}
if (!inQuotes) {
escapedJson.append(currentChar);
return;
Expand All @@ -99,6 +83,38 @@ private void handleNonQuoteCharacter(char currentChar, int position) {
escapedJson.append(getEscapeStringFromChar(currentChar));
return;
}
if (currentChar == COMMA) {
handleCommaToFixMissingClosedQuote(currentChar, position);
return;
}
if (currentChar == CLOSED_BRACKET) {
handleClosedBracket(currentChar, position);
return;
}
escapedJson.append(currentChar);
}

private void handleClosedBracket(char currentChar, int position) {
int previousNonSpaceCharPosition = getPreviousNonSpaceCharPosition(position - 1);
if (previousNonSpaceCharPosition != -1) {
escapedJson = new StringBuilder(escapedJson.substring(0, previousNonSpaceCharPosition + 1 ));
}
escapedJson.append(DOUBLE_QUOTE_CHAR);
escapedJson.append(currentChar);
inQuotes = false;
}

private void handleCommaToFixMissingClosedQuote(char currentChar, int position) {
char nextNonSpaceChar = findNextNonSpaceChar(position + 1);
if (nextNonSpaceChar == DOUBLE_QUOTE_CHAR ) {
escapedJson.append(DOUBLE_QUOTE_CHAR);
inQuotes = false;
}
if (nextNonSpaceChar == CLOSED_BRACKET ) {
escapedJson.append(DOUBLE_QUOTE_CHAR);
inQuotes = false;
return;
}
escapedJson.append(currentChar);
}

Expand Down Expand Up @@ -128,6 +144,7 @@ private char findNextNonSpaceChar(int position) {
return EOF;
}


private int getNextNonSpaceCharPosition(int position) {
for (int i = position; i < inputString.length(); i++) {
char currentChar = inputString.charAt(i);
Expand All @@ -138,4 +155,14 @@ private int getNextNonSpaceCharPosition(int position) {
return -1;
}

private int getPreviousNonSpaceCharPosition(int position) {
for (int i = position; i >= 0; i--) {
char currentChar = inputString.charAt(i);
if (currentChar != SPACE_CHAR && currentChar != BREAK_LINE_CHAR && currentChar != TAB_CHAR) {
return i;
}
}
return -1;
}

}
28 changes: 19 additions & 9 deletions src/test/java/com/cdpn/jsonautorepair/JSONAutoRepairerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,39 @@ public void repair_should_cleanup_markdown_code_block_when_string_contain_json_c

@Test
public void repair_should_fix_missing_end_quote_of_value() {
String originalJSON = """

assertEquals(JsonParser.parseString("""
{
"name": "Alice,
"name": "Alice",
"age": 30
}
""";
assertEquals(JsonParser.parseString("""
""").toString(), jsonAutoRepairer.repair("""
{
"name": "Alice",
"name": "Alice,
"age": 30
}
""").toString(), jsonAutoRepairer.repair(originalJSON));
"""));

originalJSON = """
assertEquals(JsonParser.parseString("""
{
"name": "Alice"
}
""").toString(), jsonAutoRepairer.repair("""
{
"name": "Alice,
}
""";
"""));


assertEquals(JsonParser.parseString("""
{
"name": "Alice"
}
""").toString(), jsonAutoRepairer.repair(originalJSON));
""").toString(), jsonAutoRepairer.repair("""
{
"name": "Alice
}
"""));
}


Expand Down

0 comments on commit e863de6

Please sign in to comment.