Merge pull request #12428 from atelechev
* atelechev/refactor-spring-boot-json-parsers: Polish "refactor spring-boot JSON parser" Refactor spring-boot JSON parsers
This commit is contained in:
commit
1412e7e690
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright 2012-2018 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 java.util.concurrent.Callable;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Base class for parsers wrapped or implemented in this package.
|
||||
*
|
||||
* @author Anton Telechev
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
abstract class AbstractJsonParser implements JsonParser {
|
||||
|
||||
protected final Map<String, Object> parseMap(String json,
|
||||
Function<String, Map<String, Object>> parser) {
|
||||
return trimParse(json, "{", parser);
|
||||
}
|
||||
|
||||
protected final List<Object> parseList(String json,
|
||||
Function<String, List<Object>> parser) {
|
||||
return trimParse(json, "[", parser);
|
||||
}
|
||||
|
||||
protected final <T> T trimParse(String json, String prefix,
|
||||
Function<String, T> parser) {
|
||||
String trimmed = (json == null ? "" : json.trim());
|
||||
if (trimmed.startsWith(prefix)) {
|
||||
return parser.apply(trimmed);
|
||||
}
|
||||
throw new JsonParseException();
|
||||
}
|
||||
|
||||
protected final <T> T tryParse(Callable<T> parser, Class<? extends Exception> check) {
|
||||
try {
|
||||
return parser.call();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (check.isAssignableFrom(ex.getClass())) {
|
||||
throw new JsonParseException(ex);
|
||||
}
|
||||
ReflectionUtils.rethrowRuntimeException(ex);
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -35,28 +35,16 @@ import org.springframework.util.StringUtils;
|
|||
* @since 1.2.0
|
||||
* @see JsonParserFactory
|
||||
*/
|
||||
public class BasicJsonParser implements JsonParser {
|
||||
public class BasicJsonParser extends AbstractJsonParser {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> parseMap(String json) {
|
||||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("{")) {
|
||||
return parseMapInternal(json);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot parse JSON");
|
||||
return parseMap(json, this::parseMapInternal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> parseList(String json) {
|
||||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("[")) {
|
||||
return parseListInternal(json);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot parse JSON");
|
||||
return parseList(json, this::parseListInternal);
|
||||
}
|
||||
|
||||
private List<Object> parseListInternal(String json) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
|
|
@ -31,7 +31,7 @@ import com.google.gson.reflect.TypeToken;
|
|||
* @since 1.2.0
|
||||
* @see JsonParserFactory
|
||||
*/
|
||||
public class GsonJsonParser implements JsonParser {
|
||||
public class GsonJsonParser extends AbstractJsonParser {
|
||||
|
||||
private static final TypeToken<?> MAP_TYPE = new MapTypeToken();
|
||||
|
||||
|
|
@ -41,24 +41,14 @@ public class GsonJsonParser implements JsonParser {
|
|||
|
||||
@Override
|
||||
public Map<String, Object> parseMap(String json) {
|
||||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("{")) {
|
||||
return this.gson.fromJson(json, MAP_TYPE.getType());
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot parse JSON");
|
||||
return parseMap(json,
|
||||
(trimmed) -> this.gson.fromJson(trimmed, MAP_TYPE.getType()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> parseList(String json) {
|
||||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("[")) {
|
||||
return this.gson.fromJson(json, LIST_TYPE.getType());
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot parse JSON");
|
||||
return parseList(json,
|
||||
(trimmed) -> this.gson.fromJson(trimmed, LIST_TYPE.getType()));
|
||||
}
|
||||
|
||||
private static final class MapTypeToken extends TypeToken<Map<String, Object>> {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
|
|
@ -28,7 +28,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
* @author Dave Syer
|
||||
* @see JsonParserFactory
|
||||
*/
|
||||
public class JacksonJsonParser implements JsonParser {
|
||||
public class JacksonJsonParser extends AbstractJsonParser {
|
||||
|
||||
private static final TypeReference<?> MAP_TYPE = new MapTypeReference();
|
||||
|
||||
|
|
@ -38,22 +38,14 @@ public class JacksonJsonParser implements JsonParser {
|
|||
|
||||
@Override
|
||||
public Map<String, Object> parseMap(String json) {
|
||||
try {
|
||||
return getObjectMapper().readValue(json, MAP_TYPE);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Cannot parse JSON", ex);
|
||||
}
|
||||
return tryParse(() -> getObjectMapper().readValue(json, MAP_TYPE),
|
||||
Exception.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> parseList(String json) {
|
||||
try {
|
||||
return getObjectMapper().readValue(json, LIST_TYPE);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Cannot parse JSON", ex);
|
||||
}
|
||||
return tryParse(() -> getObjectMapper().readValue(json, LIST_TYPE),
|
||||
Exception.class);
|
||||
}
|
||||
|
||||
private ObjectMapper getObjectMapper() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2012-2018 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;
|
||||
|
||||
/**
|
||||
* {@link IllegalArgumentException} thrown when source JSON is invalid.
|
||||
*
|
||||
* @author Anton Telechev
|
||||
* @author Phillip Webb
|
||||
* @since 2.0.1
|
||||
*/
|
||||
public class JsonParseException extends IllegalArgumentException {
|
||||
|
||||
public JsonParseException() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public JsonParseException(Throwable cause) {
|
||||
super("Cannot parse JSON", cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
|
|
@ -35,14 +35,16 @@ public interface JsonParser {
|
|||
* Parse the specified JSON string into a Map.
|
||||
* @param json the JSON to parse
|
||||
* @return the parsed JSON as a map
|
||||
* @throws JsonParseException if the JSON cannot be parsed
|
||||
*/
|
||||
Map<String, Object> parseMap(String json);
|
||||
Map<String, Object> parseMap(String json) throws JsonParseException;
|
||||
|
||||
/**
|
||||
* Parse the specified JSON string into a List.
|
||||
* @param json the JSON to parse
|
||||
* @return the parsed JSON as a list
|
||||
* @throws JsonParseException if the JSON cannot be parsed
|
||||
*/
|
||||
List<Object> parseList(String json);
|
||||
List<Object> parseList(String json) throws JsonParseException;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
|
|
@ -30,28 +30,20 @@ import org.json.simple.parser.ParseException;
|
|||
* @since 1.2.0
|
||||
* @see JsonParserFactory
|
||||
*/
|
||||
public class JsonSimpleJsonParser implements JsonParser {
|
||||
public class JsonSimpleJsonParser extends AbstractJsonParser {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> parseMap(String json) {
|
||||
try {
|
||||
return (Map<String, Object>) new JSONParser().parse(json);
|
||||
}
|
||||
catch (ParseException ex) {
|
||||
throw new IllegalArgumentException("Cannot parse JSON", ex);
|
||||
}
|
||||
return (Map<String, Object>) tryParse(() -> new JSONParser().parse(json),
|
||||
ParseException.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> parseList(String json) {
|
||||
try {
|
||||
return (List<Object>) new JSONParser().parse(json);
|
||||
}
|
||||
catch (ParseException ex) {
|
||||
throw new IllegalArgumentException("Cannot parse JSON", ex);
|
||||
}
|
||||
return (List<Object>) tryParse(() -> new JSONParser().parse(json),
|
||||
ParseException.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
|
|
@ -28,30 +28,18 @@ import org.yaml.snakeyaml.Yaml;
|
|||
* @author Jean de Klerk
|
||||
* @see JsonParserFactory
|
||||
*/
|
||||
public class YamlJsonParser implements JsonParser {
|
||||
public class YamlJsonParser extends AbstractJsonParser {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> parseMap(String json) {
|
||||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("{")) {
|
||||
return new Yaml().loadAs(json, Map.class);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot parse JSON");
|
||||
return parseMap(json, (trimmed) -> new Yaml().loadAs(trimmed, Map.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> parseList(String json) {
|
||||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("[")) {
|
||||
return new Yaml().loadAs(json, List.class);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot parse JSON");
|
||||
return parseList(json, (trimmed) -> new Yaml().loadAs(trimmed, List.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue