From 08ddbe79cd77167248844cd7a83123daa5d42a40 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 20 Nov 2014 17:53:29 +0000 Subject: [PATCH] Add GsonJsonParser --- spring-boot/pom.xml | 5 ++ .../boot/json/GsonJsonParser.java | 46 +++++++++++++++++++ .../springframework/boot/json/JsonParser.java | 1 + .../boot/json/JsonParserFactory.java | 3 ++ .../boot/json/GsonJsonParserTests.java | 30 ++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/json/GsonJsonParserTests.java diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index bf47c8d141..1f7ecd787f 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -54,6 +54,11 @@ jackson-databind true + + com.google.code.gson + gson + true + javax.jms jms-api diff --git a/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java new file mode 100644 index 0000000000..f19c0d44a1 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java @@ -0,0 +1,46 @@ +/* + * 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.List; +import java.util.Map; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * @author dsyer + */ +public class GsonJsonParser implements JsonParser { + + private Gson gson = new GsonBuilder().create(); + + @Override + public Map parseMap(String json) { + @SuppressWarnings("unchecked") + Map value = this.gson.fromJson(json, Map.class); + return value; + } + + @Override + public List parseList(String json) { + @SuppressWarnings("unchecked") + List value = this.gson.fromJson(json, List.class); + return value; + } + +} diff --git a/spring-boot/src/main/java/org/springframework/boot/json/JsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/JsonParser.java index 86c0aab406..bf71ed8c35 100644 --- a/spring-boot/src/main/java/org/springframework/boot/json/JsonParser.java +++ b/spring-boot/src/main/java/org/springframework/boot/json/JsonParser.java @@ -26,6 +26,7 @@ import java.util.Map; * @see JsonParserFactory * @see SimpleJsonParser * @see JacksonJsonParser + * @see GsonJsonParser * @see YamlJsonParser */ public interface JsonParser { 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 77291cb1e6..8b47bf7f43 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 @@ -37,6 +37,9 @@ public abstract class JsonParserFactory { if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", null)) { return new JacksonJsonParser(); } + if (ClassUtils.isPresent("com.google.gson.Gson", null)) { + return new GsonJsonParser(); + } if (ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) { return new YamlJsonParser(); } diff --git a/spring-boot/src/test/java/org/springframework/boot/json/GsonJsonParserTests.java b/spring-boot/src/test/java/org/springframework/boot/json/GsonJsonParserTests.java new file mode 100644 index 0000000000..ae0a108da1 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/json/GsonJsonParserTests.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2013 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 YamlJsonParser}. + * + * @author Dave Syer + */ +public class GsonJsonParserTests extends SimpleJsonParserTests { + + @Override + protected JsonParser getParser() { + return new GsonJsonParser(); + } +}