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`
This commit is contained in:
Gary Russell
2018-05-04 12:09:00 -04:00
committed by Artem Bilan
parent b547c923d1
commit e98738788d
2 changed files with 18 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.integration.json; package org.springframework.integration.json;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
@@ -29,6 +30,7 @@ import com.jayway.jsonpath.Predicate;
* Note {@link #evaluate} is used as {@code #jsonPath()} SpEL function. * Note {@link #evaluate} is used as {@code #jsonPath()} SpEL function.
* *
* @author Artem Bilan * @author Artem Bilan
* @author Gary Russell
* *
* @since 3.0 * @since 3.0
*/ */
@@ -38,6 +40,9 @@ public final class JsonPathUtils {
if (json instanceof String) { if (json instanceof String) {
return JsonPath.read((String) json, jsonPath, predicates); return JsonPath.read((String) json, jsonPath, predicates);
} }
else if (json instanceof byte[]) {
return JsonPath.read(new ByteArrayInputStream((byte[]) json), jsonPath, predicates);
}
else if (json instanceof File) { else if (json instanceof File) {
return JsonPath.read((File) json, jsonPath, predicates); return JsonPath.read((File) json, jsonPath, predicates);
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -129,6 +129,11 @@ public class JsonPathTests {
assertNotNull(receive); assertNotNull(receive);
assertEquals("Nigel Rees", receive.getPayload()); assertEquals("Nigel Rees", receive.getPayload());
this.transformerInput.send(new GenericMessage<>(JSON.getBytes()));
receive = this.output.receive(10000);
assertNotNull(receive);
assertEquals("Nigel Rees", receive.getPayload());
this.transformerInput.send(new GenericMessage<File>(JSON_FILE)); this.transformerInput.send(new GenericMessage<File>(JSON_FILE));
receive = this.output.receive(1000); receive = this.output.receive(1000);
assertNotNull(receive); assertNotNull(receive);
@@ -232,6 +237,12 @@ public class JsonPathTests {
assertEquals("Nigel Rees", receive.getPayload()); assertEquals("Nigel Rees", receive.getPayload());
} }
@Test
public void testJsonInByteArray() throws Exception {
byte[] json = "{\"foo\":\"bar\"}".getBytes();
assertEquals("bar", JsonPathUtils.evaluate(json, "$.foo"));
}
@Configuration @Configuration
@ImportResource("classpath:org/springframework/integration/json/JsonPathTests-context.xml") @ImportResource("classpath:org/springframework/integration/json/JsonPathTests-context.xml")
@EnableIntegration @EnableIntegration