Skip to content
Merged
Show file tree
Hide file tree
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 @@ -245,13 +245,15 @@ private void readJsonObjectAsSettable(Settable values, String json, int depth) {
}
if (json.charAt(sepIndex + j) == OBJECT_START) {
int closingIndex = getClosingIndex(json, OBJECT_START, OBJECT_END, sepIndex + j);
closingIndex = requireClosingIndex(closingIndex, OBJECT_START, OBJECT_END);
String newJson = json.substring(sepIndex + j + 1, closingIndex);
MapSettable nextMap = new MapSettable();
readJsonObjectAsSettable(nextMap, newJson, depth + 1);
values.put(name, nextMap.map);
i = closingIndex + 1;
} else if (json.charAt(sepIndex + j) == ARRAY_START) {
int closingIndex = getClosingIndex(json, ARRAY_START, ARRAY_END, sepIndex + j);
closingIndex = requireClosingIndex(closingIndex, ARRAY_START, ARRAY_END);
String newJson = json.substring(sepIndex + j + 1, closingIndex);
values.put(name, internalFromJsonAsList(name, newJson, depth + 1));
i = closingIndex + 1;
Expand Down Expand Up @@ -281,10 +283,16 @@ private List<Object> internalFromJsonAsList(String name, String json, int depth)
}
if (json.charAt(i) == OBJECT_START) {
int closingIndex = getClosingIndex(json, OBJECT_START, OBJECT_END, i);
closingIndex = requireClosingIndex(closingIndex, OBJECT_START, OBJECT_END);
MapSettable nextMap = new MapSettable();
readJsonObjectAsSettable(nextMap, json.substring(i + 1, closingIndex), depth + 1);
values.add(nextMap.map);
i = closingIndex + 1;
} else if (json.charAt(i) == ARRAY_START) {
int closingIndex = getClosingIndex(json, ARRAY_START, ARRAY_END, i);
closingIndex = requireClosingIndex(closingIndex, ARRAY_START, ARRAY_END);
values.add(internalFromJsonAsList(name, json.substring(i + 1, closingIndex), depth + 1));
Comment thread
coheigea marked this conversation as resolved.
i = closingIndex + 1;
} else {
int commaIndex = getCommaIndex(json, i);
Object value = readPrimitiveValue(name, json, i, commaIndex);
Expand Down Expand Up @@ -339,6 +347,15 @@ protected static int getClosingIndex(String json, char openChar, char closeChar,
return closingIndex;
}

private static int requireClosingIndex(int closingIndex, char openChar, char closeChar) {
if (closingIndex == -1) {
throw new UncheckedIOException(new IOException(
"Error in parsing json: missing closing '" + closeChar
+ "' for '" + openChar + "'"));
}
return closingIndex;
}

protected static int getNextSepCharIndex(String json, char curlyBracketChar, int from) {
int nextCurlyBracketIndex = -1;
boolean inString = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,13 @@ public void testRejectNaNNumericValue() {
assertInvalidNumericLiteral("NaN");
}

@Test
public void testNestedArrayValueParsesSuccessfully() {
Map<String, Object> map = new JsonMapObjectReaderWriter().fromJson("{\"a\":[[]]}");
assertEquals(1, map.size());
assertEquals(Collections.singletonList(Collections.emptyList()), map.get("a"));
}

private void assertInvalidNumericLiteral(String value) {
JsonMapObjectReaderWriter jsonMapObjectReaderWriter = new JsonMapObjectReaderWriter();
try {
Expand Down
Loading