Skip to content

Commit 2bfaabb

Browse files
Garus, Henningolegz
authored andcommitted
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
1 parent c8c7ce4 commit 2bfaabb

File tree

3 files changed

+19
-44
lines changed

3 files changed

+19
-44
lines changed

spring-cloud-function-context/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@
6464
<groupId>org.springframework.boot</groupId>
6565
<artifactId>spring-boot-starter-test</artifactId>
6666
<optional>true</optional>
67-
<exclusions>
68-
<exclusion>
69-
<groupId>com.vaadin.external.google</groupId>
70-
<artifactId>android-json</artifactId>
71-
</exclusion>
72-
</exclusions>
7367
</dependency>
7468
<dependency>
7569
<groupId>io.projectreactor</groupId>
@@ -112,12 +106,6 @@
112106
<version>2.2.0</version>
113107
<optional>true</optional>
114108
</dependency>
115-
116-
<dependency>
117-
<groupId>org.json</groupId>
118-
<artifactId>json</artifactId>
119-
<version>20240303</version>
120-
</dependency>
121109

122110
<!-- Actuator -->
123111
<dependency>

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/json/JsonMapper.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
import java.util.HashSet;
2525
import java.util.List;
2626

27+
import com.fasterxml.jackson.core.JsonProcessingException;
2728
import com.fasterxml.jackson.databind.DeserializationFeature;
29+
import com.fasterxml.jackson.databind.JsonNode;
2830
import com.fasterxml.jackson.databind.ObjectMapper;
31+
import com.fasterxml.jackson.databind.node.ArrayNode;
32+
import com.fasterxml.jackson.databind.node.ObjectNode;
2933
import org.apache.commons.logging.Log;
3034
import org.apache.commons.logging.LogFactory;
31-
import org.json.JSONArray;
32-
import org.json.JSONException;
33-
import org.json.JSONObject;
3435

3536
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
3637

@@ -137,33 +138,33 @@ public static boolean isJsonStringRepresentsCollection(Object value) {
137138
&& !value.getClass().getPackage().getName().startsWith("reactor.util.function")) {
138139
return true;
139140
}
140-
if (value instanceof byte[]) {
141-
value = new String((byte[]) value, StandardCharsets.UTF_8);
141+
if (value instanceof byte[] byteValue) {
142+
value = new String(byteValue, StandardCharsets.UTF_8);
142143
}
143-
if (value instanceof String) {
144+
if (value instanceof String stringValue) {
144145
try {
145-
new JSONArray((String) value);
146+
JsonNode node = mapper.readTree(stringValue);
147+
return node instanceof ArrayNode;
146148
}
147-
catch (JSONException e) {
149+
catch (JsonProcessingException e) {
148150
return false;
149151
}
150-
return true;
151152
}
152153
return false;
153154
}
154155

155156
public static boolean isJsonStringRepresentsMap(Object value) {
156-
if (value instanceof byte[]) {
157-
value = new String((byte[]) value, StandardCharsets.UTF_8);
157+
if (value instanceof byte[] byteValue) {
158+
value = new String(byteValue, StandardCharsets.UTF_8);
158159
}
159-
if (value instanceof String) {
160+
if (value instanceof String stringValue) {
160161
try {
161-
new JSONObject(value);
162+
JsonNode node = mapper.readTree(stringValue);
163+
return node instanceof ObjectNode;
162164
}
163-
catch (JSONException e) {
165+
catch (JsonProcessingException e) {
164166
return false;
165167
}
166-
return true;
167168
}
168169
return false;
169170
}

spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistryTests.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import org.springframework.messaging.converter.ByteArrayMessageConverter;
7676
import org.springframework.messaging.converter.CompositeMessageConverter;
7777
import org.springframework.messaging.converter.MessageConverter;
78+
import org.springframework.messaging.converter.ProtobufMessageConverter;
7879
import org.springframework.messaging.converter.StringMessageConverter;
7980
import org.springframework.messaging.support.MessageBuilder;
8081
import org.springframework.util.MimeType;
@@ -115,7 +116,7 @@ public void testSCF1094(String stringValue) throws IOException {
115116
Function<StringValue, String> getValue = msg -> msg != null ? msg.getValue() : null;
116117
Type functionType = ResolvableType.forClassWithGenerics(Function.class, ResolvableType.forClass(StringValue.class), ResolvableType.forClass(String.class)).getType();
117118

118-
var catalog = new SimpleFunctionRegistry(this.conversionService, this.messageConverter, new JacksonMapper(new ObjectMapper()));
119+
var catalog = new SimpleFunctionRegistry(this.conversionService, new CompositeMessageConverter(List.of(new ProtobufMessageConverter())), new JacksonMapper(new ObjectMapper()));
119120
catalog.register(new FunctionRegistration<>(getValue, "getValue").type(functionType));
120121
FunctionInvocationWrapper lookedUpFunction = catalog.lookup("getValue");
121122

@@ -129,22 +130,7 @@ public void testSCF1094(String stringValue) throws IOException {
129130
.setHeader("contentType", "application/x-protobuf")
130131
.build();
131132

132-
if (stringValue.equals("aaaaaaaaaa")) {
133-
try {
134-
lookedUpFunction.apply(inputMessage);
135-
}
136-
catch (Exception ex) {
137-
assertThat(ex).isInstanceOf(ClassCastException.class);
138-
}
139-
}
140-
else {
141-
try {
142-
lookedUpFunction.apply(inputMessage);
143-
}
144-
catch (Exception ex) {
145-
assertThat(ex).isInstanceOf(IllegalStateException.class);
146-
}
147-
}
133+
assertThat(lookedUpFunction.apply(inputMessage)).isEqualTo(stringValue);
148134
}
149135

150136
@SuppressWarnings("rawtypes")

0 commit comments

Comments
 (0)