Skip to content

Commit 8826fac

Browse files
committed
Fix missing end quote of value with more cases
1 parent ea751d2 commit 8826fac

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

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

+43-16
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,6 @@ private boolean hasNextQuoteRightAfterCurrentQuoteWithoutComma(int position) {
7575
}
7676

7777
private void handleNonQuoteCharacter(char currentChar, int position) {
78-
if (currentChar == COMMA) {
79-
char nextNonSpaceChar = findNextNonSpaceChar(position + 1);
80-
if (inQuotes) {
81-
if (nextNonSpaceChar == DOUBLE_QUOTE_CHAR ) {
82-
escapedJson.append(DOUBLE_QUOTE_CHAR);
83-
inQuotes = false;
84-
}
85-
if (nextNonSpaceChar == CLOSED_BRACKET ) {
86-
escapedJson.append(DOUBLE_QUOTE_CHAR);
87-
inQuotes = false;
88-
return;
89-
}
90-
}
91-
escapedJson.append(currentChar);
92-
return;
93-
}
9478
if (!inQuotes) {
9579
escapedJson.append(currentChar);
9680
return;
@@ -99,6 +83,38 @@ private void handleNonQuoteCharacter(char currentChar, int position) {
9983
escapedJson.append(getEscapeStringFromChar(currentChar));
10084
return;
10185
}
86+
if (currentChar == COMMA) {
87+
handleCommaToFixMissingClosedQuote(currentChar, position);
88+
return;
89+
}
90+
if (currentChar == CLOSED_BRACKET) {
91+
handleClosedBracket(currentChar, position);
92+
return;
93+
}
94+
escapedJson.append(currentChar);
95+
}
96+
97+
private void handleClosedBracket(char currentChar, int position) {
98+
int previousNonSpaceCharPosition = getPreviousNonSpaceCharPosition(position - 1);
99+
if (previousNonSpaceCharPosition != -1) {
100+
escapedJson = new StringBuilder(escapedJson.substring(0, previousNonSpaceCharPosition + 1 ));
101+
}
102+
escapedJson.append(DOUBLE_QUOTE_CHAR);
103+
escapedJson.append(currentChar);
104+
inQuotes = false;
105+
}
106+
107+
private void handleCommaToFixMissingClosedQuote(char currentChar, int position) {
108+
char nextNonSpaceChar = findNextNonSpaceChar(position + 1);
109+
if (nextNonSpaceChar == DOUBLE_QUOTE_CHAR ) {
110+
escapedJson.append(DOUBLE_QUOTE_CHAR);
111+
inQuotes = false;
112+
}
113+
if (nextNonSpaceChar == CLOSED_BRACKET ) {
114+
escapedJson.append(DOUBLE_QUOTE_CHAR);
115+
inQuotes = false;
116+
return;
117+
}
102118
escapedJson.append(currentChar);
103119
}
104120

@@ -128,6 +144,7 @@ private char findNextNonSpaceChar(int position) {
128144
return EOF;
129145
}
130146

147+
131148
private int getNextNonSpaceCharPosition(int position) {
132149
for (int i = position; i < inputString.length(); i++) {
133150
char currentChar = inputString.charAt(i);
@@ -138,4 +155,14 @@ private int getNextNonSpaceCharPosition(int position) {
138155
return -1;
139156
}
140157

158+
private int getPreviousNonSpaceCharPosition(int position) {
159+
for (int i = position; i >= 0; i--) {
160+
char currentChar = inputString.charAt(i);
161+
if (currentChar != SPACE_CHAR && currentChar != BREAK_LINE_CHAR && currentChar != TAB_CHAR) {
162+
return i;
163+
}
164+
}
165+
return -1;
166+
}
167+
141168
}

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

+19-9
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,39 @@ public void repair_should_cleanup_markdown_code_block_when_string_contain_json_c
3939

4040
@Test
4141
public void repair_should_fix_missing_end_quote_of_value() {
42-
String originalJSON = """
42+
43+
assertEquals(JsonParser.parseString("""
4344
{
44-
"name": "Alice,
45+
"name": "Alice",
4546
"age": 30
4647
}
47-
""";
48-
assertEquals(JsonParser.parseString("""
48+
""").toString(), jsonAutoRepairer.repair("""
4949
{
50-
"name": "Alice",
50+
"name": "Alice,
5151
"age": 30
5252
}
53-
""").toString(), jsonAutoRepairer.repair(originalJSON));
53+
"""));
5454

55-
originalJSON = """
55+
assertEquals(JsonParser.parseString("""
56+
{
57+
"name": "Alice"
58+
}
59+
""").toString(), jsonAutoRepairer.repair("""
5660
{
5761
"name": "Alice,
5862
}
59-
""";
63+
"""));
64+
65+
6066
assertEquals(JsonParser.parseString("""
6167
{
6268
"name": "Alice"
6369
}
64-
""").toString(), jsonAutoRepairer.repair(originalJSON));
70+
""").toString(), jsonAutoRepairer.repair("""
71+
{
72+
"name": "Alice
73+
}
74+
"""));
6575
}
6676

6777

0 commit comments

Comments
 (0)