Browse Source

Fix parsing of quoted strings

Pretty printing was not distinguishing between single-quote delimited strings and double-quote delimited strings, which in earlier revisions caused an exception which the latest revision caught and swallowed, but didn't really fix. This change refactors the prettyPrint method as a state machine which correctly handles both quoting and escaping within strings. The isEscaped method is eliminated.
pull/106/head
dnetmj 9 years ago
parent
commit
0aa5bb6a09
  1. 133
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonFormatter.java

133
json-path/src/main/java/com/jayway/jsonpath/internal/JsonFormatter.java

@ -25,78 +25,97 @@ public class JsonFormatter {
private static final String NEW_LINE = System.getProperty("line.separator"); private static final String NEW_LINE = System.getProperty("line.separator");
private static final int MODE_SINGLE = 100;
private static final int MODE_DOUBLE = 101;
private static final int MODE_ESCAPE_SINGLE = 102;
private static final int MODE_ESCAPE_DOUBLE = 103;
private static final int MODE_BETWEEN = 104;
private static void appendIndent(StringBuilder sb, int count) { private static void appendIndent(StringBuilder sb, int count) {
for (; count > 0; --count) sb.append(INDENT); for (; count > 0; --count) sb.append(INDENT);
} }
private static boolean isEscaped(StringBuilder sb, int index) {
boolean escaped = false;
int idx = Math.min(index, sb.length());
try {
while (idx > 0 && sb.charAt(--idx) == '\\') {
escaped = !escaped;
}
} catch (Exception e){
logger.warn("Failed to check escaped ", e);
}
return escaped;
}
public static String prettyPrint(String input) { public static String prettyPrint(String input) {
input = input.replaceAll("[\\r\\n]", ""); input = input.replaceAll("[\\r\\n]", "");
StringBuilder output = new StringBuilder(input.length() * 2); StringBuilder output = new StringBuilder(input.length() * 2);
boolean quoteOpened = false; int mode = MODE_BETWEEN;
int depth = 0; int depth = 0;
for (int i = 0; i < input.length(); ++i) { for (int i = 0; i < input.length(); ++i) {
char ch = input.charAt(i); char ch = input.charAt(i);
switch (ch) { switch (mode) {
case '{': case MODE_BETWEEN:
case '[': switch (ch) {
output.append(ch); case '{':
if (!quoteOpened) { case '[':
output.append(NEW_LINE); output.append(ch);
appendIndent(output, ++depth); output.append(NEW_LINE);
} appendIndent(output, ++depth);
break; break;
case '}': case '}':
case ']': case ']':
if (quoteOpened) output.append(NEW_LINE);
output.append(ch); appendIndent(output, --depth);
else { output.append(ch);
output.append(NEW_LINE); break;
appendIndent(output, --depth); case ',':
output.append(ch); output.append(ch);
} output.append(NEW_LINE);
break; appendIndent(output, depth);
case '"': break;
case '\'': case ':':
output.append(ch); output.append(" : ");
if (quoteOpened) { break;
if (!isEscaped(output, i)) case '\'':
quoteOpened = false; output.append(ch);
} else quoteOpened = true; mode = MODE_SINGLE;
break; break;
case ',': case '"':
output.append(ch); output.append(ch);
if (!quoteOpened) { mode = MODE_DOUBLE;
output.append(NEW_LINE); break;
appendIndent(output, depth); case ' ':
} break;
break; default:
case ':': output.append(ch);
if (quoteOpened) output.append(ch); break;
else output.append(" : "); }
break; break;
default: case MODE_ESCAPE_SINGLE:
if (quoteOpened || !(ch == ' ')) output.append(ch);
output.append(ch); mode = MODE_SINGLE;
break; break;
case MODE_ESCAPE_DOUBLE:
output.append(ch);
mode = MODE_DOUBLE;
break;
case MODE_SINGLE:
output.append(ch);
switch (ch) {
case '\'':
mode = MODE_BETWEEN;
break;
case '\\':
mode = MODE_ESCAPE_SINGLE;
break;
}
break;
case MODE_DOUBLE:
output.append(ch);
switch (ch) {
case '"':
mode = MODE_BETWEEN;
break;
case '\\':
mode = MODE_ESCAPE_DOUBLE;
break;
}
break;
} }
} }
return output.toString(); return output.toString();
} }
} }

Loading…
Cancel
Save