Commit 1194fc88 authored by Dave Syer's avatar Dave Syer

Add feature to SimpleJsonParser

Now it can parse nested lists as map values.

Fixes gh-169
parent bddf624b
......@@ -110,13 +110,8 @@ public class SimpleJsonParser implements JsonParser {
if (values.length > 0) {
String string = trimLeadingCharacter(
trimTrailingCharacter(values[1], '"'), '"');
if (string.startsWith("{") && string.endsWith("}")) {
value = parseInternal(string);
}
else {
value = string;
}
}
map.put(key, value);
}
return map;
......@@ -126,6 +121,7 @@ public class SimpleJsonParser implements JsonParser {
List<String> list = new ArrayList<String>();
int index = 0;
int inObject = 0;
int inList = 0;
StringBuilder build = new StringBuilder();
while (index < json.length()) {
char current = json.charAt(index);
......@@ -135,7 +131,13 @@ public class SimpleJsonParser implements JsonParser {
if (current == '}') {
inObject--;
}
if (current == ',' && inObject == 0) {
if (current == '[') {
inList++;
}
if (current == ']') {
inList--;
}
if (current == ',' && inObject == 0 && inList == 0) {
list.add(build.toString());
build.setLength(0);
}
......
......@@ -20,8 +20,6 @@ import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.boot.config.JsonParser;
import org.springframework.boot.config.SimpleJsonParser;
import static org.junit.Assert.assertEquals;
......@@ -74,4 +72,13 @@ public class SimpleJsonParserTests {
assertEquals(2, ((Map<String, Object>) list.get(1)).size());
}
@SuppressWarnings("unchecked")
@Test
public void testMapOfLists() {
Map<String, Object> map = this.parser
.parseMap("{\"foo\":[{\"foo\":\"bar\",\"spam\":1},{\"foo\":\"baz\",\"spam\":2}]}");
assertEquals(1, map.size());
assertEquals(2, ((List<Object>) map.get("foo")).size());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment