@@ -75,22 +75,6 @@ private boolean hasNextQuoteRightAfterCurrentQuoteWithoutComma(int position) {
75
75
}
76
76
77
77
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
- }
94
78
if (!inQuotes ) {
95
79
escapedJson .append (currentChar );
96
80
return ;
@@ -99,6 +83,38 @@ private void handleNonQuoteCharacter(char currentChar, int position) {
99
83
escapedJson .append (getEscapeStringFromChar (currentChar ));
100
84
return ;
101
85
}
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
+ }
102
118
escapedJson .append (currentChar );
103
119
}
104
120
@@ -128,6 +144,7 @@ private char findNextNonSpaceChar(int position) {
128
144
return EOF ;
129
145
}
130
146
147
+
131
148
private int getNextNonSpaceCharPosition (int position ) {
132
149
for (int i = position ; i < inputString .length (); i ++) {
133
150
char currentChar = inputString .charAt (i );
@@ -138,4 +155,14 @@ private int getNextNonSpaceCharPosition(int position) {
138
155
return -1 ;
139
156
}
140
157
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
+
141
168
}
0 commit comments