Commit 08ddbe79 authored by Dave Syer's avatar Dave Syer

Add GsonJsonParser

parent ab6878ab
......@@ -54,6 +54,11 @@
<artifactId>jackson-databind</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms-api</artifactId>
......
/*
* 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<String, Object> parseMap(String json) {
@SuppressWarnings("unchecked")
Map<String, Object> value = this.gson.fromJson(json, Map.class);
return value;
}
@Override
public List<Object> parseList(String json) {
@SuppressWarnings("unchecked")
List<Object> value = this.gson.fromJson(json, List.class);
return value;
}
}
......@@ -26,6 +26,7 @@ import java.util.Map;
* @see JsonParserFactory
* @see SimpleJsonParser
* @see JacksonJsonParser
* @see GsonJsonParser
* @see YamlJsonParser
*/
public interface JsonParser {
......
......@@ -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();
}
......
/*
* 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();
}
}
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