Skip to content

Commit 5d5093f

Browse files
garyrussellartembilan
authored andcommitted
INT-4461: Support byte[] in #jsonPath
JIRA: https://jira.spring.io/browse/INT-4461 Convert `byte[]` to `String` using `URF-8` by default. Add optional evaluate methods to JsonPathUtils with a Charset to use when converting `byte[]` to `String`. This is not currently exposed using SpEL. It can be done, but probably not worth the effort until somebody asks for it. **cherry-pick to 5.0.x, 4.3.x** * Use `BAIS` (cherry picked from commit e987387) # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/json/JsonPathUtils.java # spring-integration-core/src/test/java/org/springframework/integration/json/JsonPathTests.java
1 parent 2c7c9d0 commit 5d5093f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

spring-integration-core/src/main/java/org/springframework/integration/json/JsonPathUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.json;
1818

19+
import java.io.ByteArrayInputStream;
1920
import java.io.File;
2021
import java.io.InputStream;
2122
import java.net.URL;
@@ -29,6 +30,8 @@
2930
* Note {@link #evaluate} is used as {@code #jsonPath()} SpEL function.
3031
*
3132
* @author Artem Bilan
33+
* @author Gary Russell
34+
*
3235
* @since 3.0
3336
*/
3437
public final class JsonPathUtils {
@@ -37,6 +40,9 @@ public static <T> T evaluate(Object json, String jsonPath, Predicate... predicat
3740
if (json instanceof String) {
3841
return JsonPath.read((String) json, jsonPath, predicates);
3942
}
43+
else if (json instanceof byte[]) {
44+
return JsonPath.read(new ByteArrayInputStream((byte[]) json), jsonPath, predicates);
45+
}
4046
else if (json instanceof File) {
4147
return JsonPath.read((File) json, jsonPath, predicates);
4248
}

spring-integration-core/src/test/java/org/springframework/integration/json/JsonPathTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2013-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -123,6 +123,11 @@ public void testInt3139JsonPathTransformer() throws IOException {
123123
assertNotNull(receive);
124124
assertEquals("Nigel Rees", receive.getPayload());
125125

126+
this.transformerInput.send(new GenericMessage<>(JSON.getBytes()));
127+
receive = this.output.receive(10000);
128+
assertNotNull(receive);
129+
assertEquals("Nigel Rees", receive.getPayload());
130+
126131
this.transformerInput.send(new GenericMessage<File>(JSON_FILE));
127132
receive = this.output.receive(1000);
128133
assertNotNull(receive);
@@ -208,6 +213,12 @@ public void testInt3139JsonPathRouter() {
208213
}
209214

210215

216+
@Test
217+
public void testJsonInByteArray() throws Exception {
218+
byte[] json = "{\"foo\":\"bar\"}".getBytes();
219+
assertEquals("bar", JsonPathUtils.evaluate(json, "$.foo"));
220+
}
221+
211222
@Configuration
212223
@ImportResource("classpath:org/springframework/integration/json/JsonPathTests-context.xml")
213224
public static class JsonPathTestsContextConfiguration {

0 commit comments

Comments
 (0)