From 1194fc882b81c0189ecfc87228bbb8a225ea0640 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 17 Dec 2013 09:53:21 +0000 Subject: [PATCH] Add feature to SimpleJsonParser Now it can parse nested lists as map values. Fixes gh-169 --- .../boot/config/SimpleJsonParser.java | 16 +++++++++------- .../boot/config/SimpleJsonParserTests.java | 11 +++++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/config/SimpleJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/config/SimpleJsonParser.java index d551d039b0..1eb382d537 100644 --- a/spring-boot/src/main/java/org/springframework/boot/config/SimpleJsonParser.java +++ b/spring-boot/src/main/java/org/springframework/boot/config/SimpleJsonParser.java @@ -110,12 +110,7 @@ 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; - } + value = parseInternal(string); } map.put(key, value); } @@ -126,6 +121,7 @@ public class SimpleJsonParser implements JsonParser { List list = new ArrayList(); 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); } diff --git a/spring-boot/src/test/java/org/springframework/boot/config/SimpleJsonParserTests.java b/spring-boot/src/test/java/org/springframework/boot/config/SimpleJsonParserTests.java index 51c77131e3..132578f68c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/config/SimpleJsonParserTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/config/SimpleJsonParserTests.java @@ -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) list.get(1)).size()); } + @SuppressWarnings("unchecked") + @Test + public void testMapOfLists() { + Map map = this.parser + .parseMap("{\"foo\":[{\"foo\":\"bar\",\"spam\":1},{\"foo\":\"baz\",\"spam\":2}]}"); + assertEquals(1, map.size()); + assertEquals(2, ((List) map.get("foo")).size()); + } + }