From 10b9e3d30d011a2c1af1f80bec3a5a9c9d227f86 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 4 May 2020 15:49:34 +0200 Subject: [PATCH] Removed dependency on org.json Resolves #508 --- spring-cloud-function-context/pom.xml | 6 +++ .../catalog/SimpleFunctionRegistry.java | 12 ++---- .../cloud/function/json/JsonMapper.java | 41 +++++++++++++------ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/spring-cloud-function-context/pom.xml b/spring-cloud-function-context/pom.xml index b2b4eefa2..960e9e5ad 100644 --- a/spring-cloud-function-context/pom.xml +++ b/spring-cloud-function-context/pom.xml @@ -52,6 +52,12 @@ org.springframework.boot spring-boot-starter-test true + + + com.vaadin.external.google + android-json + + io.projectreactor diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java index 4125a7abe..16fe797f7 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java @@ -42,7 +42,6 @@ import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -56,6 +55,7 @@ import org.springframework.cloud.function.context.FunctionProperties; import org.springframework.cloud.function.context.FunctionRegistration; import org.springframework.cloud.function.context.FunctionRegistry; import org.springframework.cloud.function.context.config.RoutingFunction; +import org.springframework.cloud.function.json.JsonMapper; import org.springframework.core.convert.ConversionService; import org.springframework.expression.Expression; import org.springframework.expression.spel.standard.SpelExpressionParser; @@ -844,14 +844,8 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect String v = value instanceof byte[] ? new String((byte[]) value, StandardCharsets.UTF_8) : (value instanceof String ? (String) value : null); - if (v != null) { - try { - new JSONObject(v); - return true; - } - catch (Exception ex) { - // ignore - } + if (v != null && JsonMapper.isJsonString(v)) { + return true; } return false; } diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/json/JsonMapper.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/json/JsonMapper.java index bd65b15fd..73f5a9791 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/json/JsonMapper.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/json/JsonMapper.java @@ -23,7 +23,6 @@ import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; import org.springframework.core.ResolvableType; @@ -64,20 +63,18 @@ public abstract class JsonMapper { public abstract T fromJson(Object json, Type type); public byte[] toJson(Object value) { - if (value instanceof String) { - try { - new JSONObject((String) value); - if (logger.isDebugEnabled()) { - logger.debug( - "String already represents JSON. Skipping conversion in favor of 'getBytes(StandardCharsets.UTF_8'."); - } - return ((String) value).getBytes(StandardCharsets.UTF_8); - } - catch (Exception ex) { - // ignore + byte[] result = null; + if (isJsonString(value)) { + if (logger.isDebugEnabled()) { + logger.debug( + "String already represents JSON. Skipping conversion in favor of 'getBytes(StandardCharsets.UTF_8'."); } + result = ((String) value).getBytes(StandardCharsets.UTF_8); } - return null; + else { + logger.warn("Object does not represent a valid JSON. Object is: " + value); + } + return result; } /** @@ -94,4 +91,22 @@ public abstract class JsonMapper { public abstract String toString(Object value); + /** + * Performs a simple validation on an object to see if it appears to be a JSON string. + * NOTE: the validation is very rudimentary and simply checks that the object is a String and begins + * and ends with matching pairs of "{}" or "[]" or "\"\"" and therefore may not handle some corner cases. + * Primarily intended for internal of the framework. + * @param value candidate object to evaluate + * @return true if and object appears to be a valid JSON string, otherwise false. + */ + public static boolean isJsonString(Object value) { + boolean isJson = false; + if (value instanceof String) { + String str = ((String) value).trim(); + isJson = (str.startsWith("\"") && str.endsWith("\"")) || + (str.startsWith("{") && str.endsWith("}")) || + (str.startsWith("[") && str.endsWith("]")); + } + return isJson; + } }