3
3
public class EscapeProcessor {
4
4
private static final char EOF = '\0' ;
5
5
private static final char TAB_CHAR = '\t' ;
6
+ private static final char COMMA = ',' ;
7
+ private static final char CLOSED_BRACKET = '}' ;
6
8
private static final char BREAK_LINE_CHAR = '\n' ;
7
9
private static final char SPACE_CHAR = ' ' ;
8
10
private static final char DOUBLE_QUOTE_CHAR = '\"' ;
@@ -23,7 +25,7 @@ public String process() {
23
25
for (int i = 0 ; i < inputString .length (); i ++) {
24
26
char currentChar = inputString .charAt (i );
25
27
if (currentChar != DOUBLE_QUOTE_CHAR ) {
26
- handleNonQuoteCharacter (currentChar );
28
+ handleNonQuoteCharacter (currentChar , i );
27
29
}
28
30
else {
29
31
handleQuoteCharacter (currentChar , i );
@@ -72,7 +74,23 @@ private boolean hasNextQuoteRightAfterCurrentQuoteWithoutComma(int position) {
72
74
return findNextNonSpaceChar (position + 1 ) == DOUBLE_QUOTE_CHAR ;
73
75
}
74
76
75
- private void handleNonQuoteCharacter (char currentChar ) {
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
+ }
76
94
if (!inQuotes ) {
77
95
escapedJson .append (currentChar );
78
96
return ;
0 commit comments