Commit 90949669 authored by Phillip Webb's avatar Phillip Webb

Polish "refactor spring-boot JSON parser"

Polish JSON parser refactoring and remove some more duplication.

Closes gh-12428
parent aa696210
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,74 +18,49 @@ package org.springframework.boot.json; ...@@ -18,74 +18,49 @@ package org.springframework.boot.json;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.concurrent.Callable;
import java.util.function.Function; import java.util.function.Function;
import org.springframework.util.ReflectionUtils;
/** /**
* Base class for parsers wrapped or implemented in this package. * Base class for parsers wrapped or implemented in this package.
* *
* @author Anton Telechev * @author Anton Telechev
* @author Phillip Webb
*/ */
abstract class AbstractJsonParser implements JsonParser { abstract class AbstractJsonParser implements JsonParser {
/** Start symbol of a JSON map. **/ protected final Map<String, Object> parseMap(String json,
private static final String START_MAP = "{";
/** Start symbol of a JSON list. **/
private static final String START_LIST = "[";
/**
* Parses the specified JSON string and returns the extracted contents as a Map of
* String to Object.
*
* @param json the JSON string to parse.
* @param parser the parser function.
* @return Map&lt;String, Object&gt; parsed contents
* @throws IllegalArgumentException if the json String cannot be parsed as a
* Map&lt;String, Object&gt;
*/
Map<String, Object> parseMap(String json,
Function<String, Map<String, Object>> parser) { Function<String, Map<String, Object>> parser) {
assert parser != null; return trimParse(json, "{", parser);
return trimIfStartsWith(json, START_MAP).map(parser::apply)
.orElseThrow(AbstractJsonParser::cannotParseJson);
}
/**
* Parses the specified JSON string and returns the extracted contents as a List of Objects.
*
* @param json the JSON string to parse.
* @param parser the parser function.
* @return List&lt;Object&gt; parsed contents
* @throws IllegalArgumentException if the json String cannot be parsed as a
* List&lt;Object&gt;
*/
List<Object> parseList(String json, Function<String, List<Object>> parser) {
assert parser != null;
return trimIfStartsWith(json, START_LIST).map(parser::apply)
.orElseThrow(AbstractJsonParser::cannotParseJson);
} }
private static IllegalArgumentException cannotParseJson() { protected final List<Object> parseList(String json,
return cannotParseJson(null); Function<String, List<Object>> parser) {
return trimParse(json, "[", parser);
} }
static IllegalArgumentException cannotParseJson(Exception cause) { protected final <T> T trimParse(String json, String prefix,
return new IllegalArgumentException("Cannot parse JSON", cause); Function<String, T> parser) {
String trimmed = (json == null ? "" : json.trim());
if (trimmed.startsWith(prefix)) {
return parser.apply(trimmed);
}
throw new JsonParseException();
} }
private static Optional<String> trimIfStartsWith(String json, String expectedPrefix) { protected final <T> T tryParse(Callable<T> parser, Class<? extends Exception> check) {
assert expectedPrefix != null; try {
return parser.call();
if (json != null) { }
final String trimmed = json.trim(); catch (Exception ex) {
if (trimmed.startsWith(expectedPrefix)) { if (check.isAssignableFrom(ex.getClass())) {
return Optional.of(trimmed); throw new JsonParseException(ex);
} }
ReflectionUtils.rethrowRuntimeException(ex);
throw new IllegalStateException(ex);
} }
return Optional.empty();
} }
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -28,7 +28,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -28,7 +28,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author Dave Syer * @author Dave Syer
* @see JsonParserFactory * @see JsonParserFactory
*/ */
public class JacksonJsonParser implements JsonParser { public class JacksonJsonParser extends AbstractJsonParser {
private static final TypeReference<?> MAP_TYPE = new MapTypeReference(); private static final TypeReference<?> MAP_TYPE = new MapTypeReference();
...@@ -38,22 +38,14 @@ public class JacksonJsonParser implements JsonParser { ...@@ -38,22 +38,14 @@ public class JacksonJsonParser implements JsonParser {
@Override @Override
public Map<String, Object> parseMap(String json) { public Map<String, Object> parseMap(String json) {
try { return tryParse(() -> getObjectMapper().readValue(json, MAP_TYPE),
return getObjectMapper().readValue(json, MAP_TYPE); Exception.class);
}
catch (Exception ex) {
throw AbstractJsonParser.cannotParseJson(ex);
}
} }
@Override @Override
public List<Object> parseList(String json) { public List<Object> parseList(String json) {
try { return tryParse(() -> getObjectMapper().readValue(json, LIST_TYPE),
return getObjectMapper().readValue(json, LIST_TYPE); Exception.class);
}
catch (Exception ex) {
throw AbstractJsonParser.cannotParseJson(ex);
}
} }
private ObjectMapper getObjectMapper() { private ObjectMapper getObjectMapper() {
......
/*
* 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);
}
}
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,14 +35,16 @@ public interface JsonParser { ...@@ -35,14 +35,16 @@ public interface JsonParser {
* Parse the specified JSON string into a Map. * Parse the specified JSON string into a Map.
* @param json the JSON to parse * @param json the JSON to parse
* @return the parsed JSON as a map * @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. * Parse the specified JSON string into a List.
* @param json the JSON to parse * @param json the JSON to parse
* @return the parsed JSON as a list * @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;
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -30,28 +30,20 @@ import org.json.simple.parser.ParseException; ...@@ -30,28 +30,20 @@ import org.json.simple.parser.ParseException;
* @since 1.2.0 * @since 1.2.0
* @see JsonParserFactory * @see JsonParserFactory
*/ */
public class JsonSimpleJsonParser implements JsonParser { public class JsonSimpleJsonParser extends AbstractJsonParser {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, Object> parseMap(String json) { public Map<String, Object> parseMap(String json) {
try { return (Map<String, Object>) tryParse(() -> new JSONParser().parse(json),
return (Map<String, Object>) new JSONParser().parse(json); ParseException.class);
}
catch (ParseException ex) {
throw AbstractJsonParser.cannotParseJson(ex);
}
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<Object> parseList(String json) { public List<Object> parseList(String json) {
try { return (List<Object>) tryParse(() -> new JSONParser().parse(json),
return (List<Object>) new JSONParser().parse(json); ParseException.class);
}
catch (ParseException ex) {
throw AbstractJsonParser.cannotParseJson(ex);
}
} }
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
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