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:
Garus, Henning
2024-08-12 21:18:27 +02:00
committed by Oleg Zhurakousky
parent c8c7ce41cc
commit 2bfaabb570
3 changed files with 19 additions and 44 deletions

View File

@@ -64,12 +64,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
@@ -112,12 +106,6 @@
<version>2.2.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<!-- Actuator -->
<dependency>

View File

@@ -24,13 +24,14 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
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.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
@@ -137,33 +138,33 @@ public abstract class JsonMapper {
&& !value.getClass().getPackage().getName().startsWith("reactor.util.function")) {
return true;
}
if (value instanceof byte[]) {
value = new String((byte[]) value, StandardCharsets.UTF_8);
if (value instanceof byte[] byteValue) {
value = new String(byteValue, StandardCharsets.UTF_8);
}
if (value instanceof String) {
if (value instanceof String stringValue) {
try {
new JSONArray((String) value);
JsonNode node = mapper.readTree(stringValue);
return node instanceof ArrayNode;
}
catch (JSONException e) {
catch (JsonProcessingException e) {
return false;
}
return true;
}
return false;
}
public static boolean isJsonStringRepresentsMap(Object value) {
if (value instanceof byte[]) {
value = new String((byte[]) value, StandardCharsets.UTF_8);
if (value instanceof byte[] byteValue) {
value = new String(byteValue, StandardCharsets.UTF_8);
}
if (value instanceof String) {
if (value instanceof String stringValue) {
try {
new JSONObject(value);
JsonNode node = mapper.readTree(stringValue);
return node instanceof ObjectNode;
}
catch (JSONException e) {
catch (JsonProcessingException e) {
return false;
}
return true;
}
return false;
}

View File

@@ -75,6 +75,7 @@ import org.springframework.messaging.converter.AbstractMessageConverter;
import org.springframework.messaging.converter.ByteArrayMessageConverter;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.ProtobufMessageConverter;
import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeType;
@@ -115,7 +116,7 @@ public class SimpleFunctionRegistryTests {
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();
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));
FunctionInvocationWrapper lookedUpFunction = catalog.lookup("getValue");
@@ -129,22 +130,7 @@ public class SimpleFunctionRegistryTests {
.setHeader("contentType", "application/x-protobuf")
.build();
if (stringValue.equals("aaaaaaaaaa")) {
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);
}
}
assertThat(lookedUpFunction.apply(inputMessage)).isEqualTo(stringValue);
}
@SuppressWarnings("rawtypes")