Skip to content

Commit e987387

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`
1 parent b547c92 commit e987387

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 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,7 @@
2930
* Note {@link #evaluate} is used as {@code #jsonPath()} SpEL function.
3031
*
3132
* @author Artem Bilan
33+
* @author Gary Russell
3234
*
3335
* @since 3.0
3436
*/
@@ -38,6 +40,9 @@ public static <T> T evaluate(Object json, String jsonPath, Predicate... predicat
3840
if (json instanceof String) {
3941
return JsonPath.read((String) json, jsonPath, predicates);
4042
}
43+
else if (json instanceof byte[]) {
44+
return JsonPath.read(new ByteArrayInputStream((byte[]) json), jsonPath, predicates);
45+
}
4146
else if (json instanceof File) {
4247
return JsonPath.read((File) json, jsonPath, predicates);
4348
}

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-2017 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.
@@ -129,6 +129,11 @@ public void testInt3139JsonPathTransformer() throws IOException {
129129
assertNotNull(receive);
130130
assertEquals("Nigel Rees", receive.getPayload());
131131

132+
this.transformerInput.send(new GenericMessage<>(JSON.getBytes()));
133+
receive = this.output.receive(10000);
134+
assertNotNull(receive);
135+
assertEquals("Nigel Rees", receive.getPayload());
136+
132137
this.transformerInput.send(new GenericMessage<File>(JSON_FILE));
133138
receive = this.output.receive(1000);
134139
assertNotNull(receive);
@@ -232,6 +237,12 @@ public void testJsonPathOnPayloadAnnotation() {
232237
assertEquals("Nigel Rees", receive.getPayload());
233238
}
234239

240+
@Test
241+
public void testJsonInByteArray() throws Exception {
242+
byte[] json = "{\"foo\":\"bar\"}".getBytes();
243+
assertEquals("bar", JsonPathUtils.evaluate(json, "$.foo"));
244+
}
245+
235246
@Configuration
236247
@ImportResource("classpath:org/springframework/integration/json/JsonPathTests-context.xml")
237248
@EnableIntegration

0 commit comments

Comments
 (0)