GH-1156 Remove org.json:json dependency
Use the already present Jackson ObjectMapper instead to provide the same behaviour, where a value is parsed to check if it is a valid json structure, ie an array or an object. Resolves #1173 Resolves #1156
This commit is contained in:
committed by
Oleg Zhurakousky
parent
c8c7ce41cc
commit
2bfaabb570
@@ -64,12 +64,6 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>com.vaadin.external.google</groupId>
|
|
||||||
<artifactId>android-json</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.projectreactor</groupId>
|
<groupId>io.projectreactor</groupId>
|
||||||
@@ -112,12 +106,6 @@
|
|||||||
<version>2.2.0</version>
|
<version>2.2.0</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.json</groupId>
|
|
||||||
<artifactId>json</artifactId>
|
|
||||||
<version>20240303</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Actuator -->
|
<!-- Actuator -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -24,13 +24,14 @@ import java.util.Collection;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
|
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
|
||||||
|
|
||||||
@@ -137,33 +138,33 @@ public abstract class JsonMapper {
|
|||||||
&& !value.getClass().getPackage().getName().startsWith("reactor.util.function")) {
|
&& !value.getClass().getPackage().getName().startsWith("reactor.util.function")) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (value instanceof byte[]) {
|
if (value instanceof byte[] byteValue) {
|
||||||
value = new String((byte[]) value, StandardCharsets.UTF_8);
|
value = new String(byteValue, StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
if (value instanceof String) {
|
if (value instanceof String stringValue) {
|
||||||
try {
|
try {
|
||||||
new JSONArray((String) value);
|
JsonNode node = mapper.readTree(stringValue);
|
||||||
|
return node instanceof ArrayNode;
|
||||||
}
|
}
|
||||||
catch (JSONException e) {
|
catch (JsonProcessingException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isJsonStringRepresentsMap(Object value) {
|
public static boolean isJsonStringRepresentsMap(Object value) {
|
||||||
if (value instanceof byte[]) {
|
if (value instanceof byte[] byteValue) {
|
||||||
value = new String((byte[]) value, StandardCharsets.UTF_8);
|
value = new String(byteValue, StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
if (value instanceof String) {
|
if (value instanceof String stringValue) {
|
||||||
try {
|
try {
|
||||||
new JSONObject(value);
|
JsonNode node = mapper.readTree(stringValue);
|
||||||
|
return node instanceof ObjectNode;
|
||||||
}
|
}
|
||||||
catch (JSONException e) {
|
catch (JsonProcessingException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ import org.springframework.messaging.converter.AbstractMessageConverter;
|
|||||||
import org.springframework.messaging.converter.ByteArrayMessageConverter;
|
import org.springframework.messaging.converter.ByteArrayMessageConverter;
|
||||||
import org.springframework.messaging.converter.CompositeMessageConverter;
|
import org.springframework.messaging.converter.CompositeMessageConverter;
|
||||||
import org.springframework.messaging.converter.MessageConverter;
|
import org.springframework.messaging.converter.MessageConverter;
|
||||||
|
import org.springframework.messaging.converter.ProtobufMessageConverter;
|
||||||
import org.springframework.messaging.converter.StringMessageConverter;
|
import org.springframework.messaging.converter.StringMessageConverter;
|
||||||
import org.springframework.messaging.support.MessageBuilder;
|
import org.springframework.messaging.support.MessageBuilder;
|
||||||
import org.springframework.util.MimeType;
|
import org.springframework.util.MimeType;
|
||||||
@@ -115,7 +116,7 @@ public class SimpleFunctionRegistryTests {
|
|||||||
Function<StringValue, String> getValue = msg -> msg != null ? msg.getValue() : null;
|
Function<StringValue, String> getValue = msg -> msg != null ? msg.getValue() : null;
|
||||||
Type functionType = ResolvableType.forClassWithGenerics(Function.class, ResolvableType.forClass(StringValue.class), ResolvableType.forClass(String.class)).getType();
|
Type functionType = ResolvableType.forClassWithGenerics(Function.class, ResolvableType.forClass(StringValue.class), ResolvableType.forClass(String.class)).getType();
|
||||||
|
|
||||||
var catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter, new JacksonMapper(new ObjectMapper()));
|
var catalog = new SimpleFunctionRegistry(this.conversionService, new CompositeMessageConverter(List.of(new ProtobufMessageConverter())), new JacksonMapper(new ObjectMapper()));
|
||||||
catalog.register(new FunctionRegistration<>(getValue, "getValue").type(functionType));
|
catalog.register(new FunctionRegistration<>(getValue, "getValue").type(functionType));
|
||||||
FunctionInvocationWrapper lookedUpFunction = catalog.lookup("getValue");
|
FunctionInvocationWrapper lookedUpFunction = catalog.lookup("getValue");
|
||||||
|
|
||||||
@@ -129,22 +130,7 @@ public class SimpleFunctionRegistryTests {
|
|||||||
.setHeader("contentType", "application/x-protobuf")
|
.setHeader("contentType", "application/x-protobuf")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
if (stringValue.equals("aaaaaaaaaa")) {
|
assertThat(lookedUpFunction.apply(inputMessage)).isEqualTo(stringValue);
|
||||||
try {
|
|
||||||
lookedUpFunction.apply(inputMessage);
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
assertThat(ex).isInstanceOf(ClassCastException.class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
try {
|
|
||||||
lookedUpFunction.apply(inputMessage);
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
assertThat(ex).isInstanceOf(IllegalStateException.class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
|
|||||||
Reference in New Issue
Block a user