From 843e6d7cfa0ee9e7c400d3e109e1b873aa14736d Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 1 Dec 2014 13:41:13 +0000 Subject: [PATCH] Add 2 more JsonParser implementations --- spring-boot-dependencies/pom.xml | 13 ++- spring-boot/pom.xml | 5 ++ .../boot/json/JsonJsonParser.java | 65 ++++++++++++++ .../boot/json/JsonParserFactory.java | 10 ++- .../boot/json/SimpleJsonJsonParser.java | 85 +++++++++++++++++++ .../boot/json/JsonJsonParserTests.java | 30 +++++++ .../boot/json/SimpleJsonJsonParserTests.java | 30 +++++++ 7 files changed, 235 insertions(+), 3 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/json/JsonJsonParser.java create mode 100644 spring-boot/src/main/java/org/springframework/boot/json/SimpleJsonJsonParser.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/json/JsonJsonParserTests.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/json/SimpleJsonJsonParserTests.java diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 6406b036f7..2de7306bfd 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -1,4 +1,6 @@ - + + 4.0.0 org.springframework.boot spring-boot-dependencies @@ -102,6 +104,7 @@ 1.1.5.RELEASE 1.1.3.RELEASE 3.1.0 + 1.1.1 1.7.7 1.14 4.7.2 @@ -464,6 +467,12 @@ gson ${gson.version} + + com.googlecode.json-simple + json-simple + ${simple-json.version} + true + com.h2database h2 @@ -1528,7 +1537,7 @@ true - spring-milestones diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 1f7ecd787f..1d8c6bff9d 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -59,6 +59,11 @@ gson true + + com.googlecode.json-simple + json-simple + true + javax.jms jms-api diff --git a/spring-boot/src/main/java/org/springframework/boot/json/JsonJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/JsonJsonParser.java new file mode 100644 index 0000000000..7184b75fc3 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/json/JsonJsonParser.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.json; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +/** + * Thin wrapper to adapt {@link JSONObject} to a {@link JsonParser}. + * + * @author Dave Syer + * @since 1.2.0 + * @see JsonParserFactory + */ +public class JsonJsonParser implements JsonParser { + + @Override + public Map parseMap(String json) { + Map map = new LinkedHashMap(); + try { + @SuppressWarnings("unchecked") + Map value = (Map) new JSONParser() + .parse(json); + map.putAll(value); + } + catch (ParseException e) { + throw new IllegalArgumentException("Cannot parse Json", e); + } + return map; + } + + @Override + public List parseList(String json) { + List nested = new ArrayList(); + try { + @SuppressWarnings("unchecked") + List value = (List) new JSONParser().parse(json); + nested.addAll(value); + } + catch (ParseException e) { + throw new IllegalArgumentException("Cannot parse Json", e); + } + return nested; + } +} diff --git a/spring-boot/src/main/java/org/springframework/boot/json/JsonParserFactory.java b/spring-boot/src/main/java/org/springframework/boot/json/JsonParserFactory.java index 2f2fd622df..c7ca26c1a7 100644 --- a/spring-boot/src/main/java/org/springframework/boot/json/JsonParserFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/json/JsonParserFactory.java @@ -30,13 +30,21 @@ public abstract class JsonParserFactory { /** * Static factory for the "best" JSON parser available on the classpath. Tries Jackson - * 2, then Snake YAML, and then falls back to the {@link SimpleJsonParser}. + * 2, then JSON (from eclipse), Simple JSON, Gson, Snake YAML, and then falls back to + * the {@link SimpleJsonParser}. + * * @return a {@link JsonParser} */ public static JsonParser getJsonParser() { if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", null)) { return new JacksonJsonParser(); } + if (ClassUtils.isPresent("org.json.JSONObject", null)) { + return new JsonJsonParser(); + } + if (ClassUtils.isPresent("org.json.simple.JSONObject", null)) { + return new SimpleJsonJsonParser(); + } if (ClassUtils.isPresent("com.google.gson.Gson", null)) { return new GsonJsonParser(); } diff --git a/spring-boot/src/main/java/org/springframework/boot/json/SimpleJsonJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/SimpleJsonJsonParser.java new file mode 100644 index 0000000000..b0c5193621 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/json/SimpleJsonJsonParser.java @@ -0,0 +1,85 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.json; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.JSONArray; +import org.json.JSONObject; + +/** + * Thin wrapper to adapt {@link JSONObject} to a {@link JsonParser}. + * + * @author Dave Syer + * @since 1.2.0 + * @see JsonParserFactory + */ +public class SimpleJsonJsonParser implements JsonParser { + + @Override + public Map parseMap(String json) { + Map map = new LinkedHashMap(); + putAll(map, new JSONObject(json)); + return map; + } + + private void putAll(Map map, JSONObject object) { + for (Object key : object.keySet()) { + String name = key.toString(); + Object value = object.get(name); + if (value instanceof JSONObject) { + Map nested = new LinkedHashMap(); + putAll(nested, (JSONObject) value); + value = nested; + } + if (value instanceof JSONArray) { + List nested = new ArrayList(); + addAll(nested, (JSONArray) value); + value = nested; + } + map.put(name, value); + } + } + + private void addAll(List list, JSONArray array) { + for (int i = 0; i < array.length(); i++) { + Object value = array.get(i); + if (value instanceof JSONObject) { + Map nested = new LinkedHashMap(); + putAll(nested, (JSONObject) value); + value = nested; + } + if (value instanceof JSONArray) { + List nested = new ArrayList(); + addAll(nested, (JSONArray) value); + value = nested; + } + list.add(value); + } + } + + @Override + public List parseList(String json) { + List nested = new ArrayList(); + addAll(nested, new JSONArray(json)); + return nested; + } + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/json/JsonJsonParserTests.java b/spring-boot/src/test/java/org/springframework/boot/json/JsonJsonParserTests.java new file mode 100644 index 0000000000..2eb4d892ec --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/json/JsonJsonParserTests.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.json; + +/** + * Tests for {@link JsonJsonParser}. + * + * @author Dave Syer + */ +public class JsonJsonParserTests extends SimpleJsonParserTests { + + @Override + protected JsonParser getParser() { + return new JsonJsonParser(); + } +} diff --git a/spring-boot/src/test/java/org/springframework/boot/json/SimpleJsonJsonParserTests.java b/spring-boot/src/test/java/org/springframework/boot/json/SimpleJsonJsonParserTests.java new file mode 100644 index 0000000000..7587cc2d8a --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/json/SimpleJsonJsonParserTests.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.json; + +/** + * Tests for {@link SimpleJsonJsonParser}. + * + * @author Dave Syer + */ +public class SimpleJsonJsonParserTests extends SimpleJsonParserTests { + + @Override + protected JsonParser getParser() { + return new SimpleJsonJsonParser(); + } +}