@@ -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}
0 commit comments