Add 2 more JsonParser implementations
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
@@ -102,6 +104,7 @@
|
||||
<reactor.version>1.1.5.RELEASE</reactor.version>
|
||||
<reactor-spring.version>1.1.3.RELEASE</reactor-spring.version>
|
||||
<servlet-api.version>3.1.0</servlet-api.version>
|
||||
<simple-json.version>1.1.1</simple-json.version>
|
||||
<slf4j.version>1.7.7</slf4j.version>
|
||||
<snakeyaml.version>1.14</snakeyaml.version>
|
||||
<solr.version>4.7.2</solr.version>
|
||||
@@ -464,6 +467,12 @@
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>${simple-json.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
@@ -1528,7 +1537,7 @@
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<repositories>
|
||||
<!-- Repositories to allow snapshot and milestone BOM imports during
|
||||
<!-- Repositories to allow snapshot and milestone BOM imports during
|
||||
development. This section is stripped out when a full release is prepared. -->
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
|
||||
@@ -59,6 +59,11 @@
|
||||
<artifactId>gson</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.jms</groupId>
|
||||
<artifactId>jms-api</artifactId>
|
||||
|
||||
@@ -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<String, Object> parseMap(String json) {
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> value = (Map<String, Object>) new JSONParser()
|
||||
.parse(json);
|
||||
map.putAll(value);
|
||||
}
|
||||
catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Cannot parse Json", e);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> parseList(String json) {
|
||||
List<Object> nested = new ArrayList<Object>();
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Object> value = (List<Object>) new JSONParser().parse(json);
|
||||
nested.addAll(value);
|
||||
}
|
||||
catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Cannot parse Json", e);
|
||||
}
|
||||
return nested;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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<String, Object> parseMap(String json) {
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
putAll(map, new JSONObject(json));
|
||||
return map;
|
||||
}
|
||||
|
||||
private void putAll(Map<String, Object> map, JSONObject object) {
|
||||
for (Object key : object.keySet()) {
|
||||
String name = key.toString();
|
||||
Object value = object.get(name);
|
||||
if (value instanceof JSONObject) {
|
||||
Map<String, Object> nested = new LinkedHashMap<String, Object>();
|
||||
putAll(nested, (JSONObject) value);
|
||||
value = nested;
|
||||
}
|
||||
if (value instanceof JSONArray) {
|
||||
List<Object> nested = new ArrayList<Object>();
|
||||
addAll(nested, (JSONArray) value);
|
||||
value = nested;
|
||||
}
|
||||
map.put(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
private void addAll(List<Object> list, JSONArray array) {
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
Object value = array.get(i);
|
||||
if (value instanceof JSONObject) {
|
||||
Map<String, Object> nested = new LinkedHashMap<String, Object>();
|
||||
putAll(nested, (JSONObject) value);
|
||||
value = nested;
|
||||
}
|
||||
if (value instanceof JSONArray) {
|
||||
List<Object> nested = new ArrayList<Object>();
|
||||
addAll(nested, (JSONArray) value);
|
||||
value = nested;
|
||||
}
|
||||
list.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> parseList(String json) {
|
||||
List<Object> nested = new ArrayList<Object>();
|
||||
addAll(nested, new JSONArray(json));
|
||||
return nested;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user