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 7065e36744..92a7e0f536 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-2015 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,8 @@ import com.jayway.jsonpath.Predicate; * Note {@link #evaluate} is used as {@code #jsonPath()} SpEL function. * * @author Artem Bilan + * @author Gary Russell + * * @since 3.0 */ public final class JsonPathUtils { @@ -37,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 73babbaf36..380b63472e 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-2016 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. @@ -123,6 +123,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); @@ -208,6 +213,12 @@ public class JsonPathTests { } + @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") public static class JsonPathTestsContextConfiguration {