Removed dependency on org.json

Resolves #508
This commit is contained in:
Oleg Zhurakousky
2020-05-04 15:49:34 +02:00
parent 5f3f0cf965
commit 10b9e3d30d
3 changed files with 37 additions and 22 deletions

View File

@@ -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;
}

View File

@@ -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> 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;
}
}