diff --git a/spring-integration-core/src/main/java/org/springframework/integration/json/JsonPathUtils.java b/spring-integration-core/src/main/java/org/springframework/integration/json/JsonPathUtils.java index 6a485d173b..01b6c6eaab 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/json/JsonPathUtils.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/json/JsonPathUtils.java @@ -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"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.integration.json; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.net.URL; @@ -29,6 +30,7 @@ import com.jayway.jsonpath.Predicate; * Note {@link #evaluate} is used as {@code #jsonPath()} SpEL function. * * @author Artem Bilan + * @author Gary Russell * * @since 3.0 */ @@ -38,6 +40,9 @@ public final class JsonPathUtils { if (json instanceof String) { 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) { return JsonPath.read((File) json, jsonPath, predicates); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/json/JsonPathTests.java b/spring-integration-core/src/test/java/org/springframework/integration/json/JsonPathTests.java index 705e2e4f63..88dfdafbe0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/json/JsonPathTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/json/JsonPathTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -129,6 +129,11 @@ public class JsonPathTests { assertNotNull(receive); 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(JSON_FILE)); receive = this.output.receive(1000); assertNotNull(receive); @@ -232,6 +237,12 @@ public class JsonPathTests { assertEquals("Nigel Rees", receive.getPayload()); } + @Test + public void testJsonInByteArray() throws Exception { + byte[] json = "{\"foo\":\"bar\"}".getBytes(); + assertEquals("bar", JsonPathUtils.evaluate(json, "$.foo")); + } + @Configuration @ImportResource("classpath:org/springframework/integration/json/JsonPathTests-context.xml") @EnableIntegration