Skip to content

Refactor BasicJsonParser to simplify JSON parsing logic #41876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public List<Object> parseList(String json) {

private List<Object> parseListInternal(int nesting, String json) {
List<Object> list = new ArrayList<>();
json = trimLeadingCharacter(trimTrailingCharacter(json, ']'), '[').trim();
json = trimEdges(json, '[', ']').trim();
for (String value : tokenize(json)) {
list.add(parseInternal(nesting + 1, value));
}
Expand All @@ -70,37 +70,39 @@ private Object parseInternal(int nesting, String json) {
return parseMapInternal(nesting + 1, json);
}
if (json.startsWith("\"")) {
return trimTrailingCharacter(trimLeadingCharacter(json, '"'), '"');
return trimEdges(json, '"', '"');
}
try {
return Long.valueOf(json);
}
catch (NumberFormatException ex) {
// ignore
}
try {
return Double.valueOf(json);
}
catch (NumberFormatException ex) {
// ignore
}
return json;
return parseNumber(json);
}

private Map<String, Object> parseMapInternal(int nesting, String json) {
Map<String, Object> map = new LinkedHashMap<>();
json = trimLeadingCharacter(trimTrailingCharacter(json, '}'), '{').trim();
json = trimEdges(json, '{', '}').trim();
for (String pair : tokenize(json)) {
String[] values = StringUtils.trimArrayElements(StringUtils.split(pair, ":"));
Assert.state(values[0].startsWith("\"") && values[0].endsWith("\""),
"Expecting double-quotes around field names");
String key = trimLeadingCharacter(trimTrailingCharacter(values[0], '"'), '"');
String key = trimEdges(values[0], '"', '"');
Object value = parseInternal(nesting, values[1]);
map.put(key, value);
}
return map;
}

private Object parseNumber(String json) {
try {
return Long.valueOf(json);
}
catch (NumberFormatException e) {
try {
return Double.valueOf(json);
}
catch (NumberFormatException ex) {
return json;
}
}
}

private static String trimTrailingCharacter(String string, char c) {
if (!string.isEmpty() && string.charAt(string.length() - 1) == c) {
return string.substring(0, string.length() - 1);
Expand All @@ -115,6 +117,10 @@ private static String trimLeadingCharacter(String string, char c) {
return string;
}

private static String trimEdges(String string, char leadingChar, char trailingChar) {
return trimTrailingCharacter(trimLeadingCharacter(string, leadingChar), trailingChar);
}

private List<String> tokenize(String json) {
List<String> list = new ArrayList<>();
int index = 0;
Expand Down
Loading