From b94e4ced877ca8e9eb331d4601136d8b13e80406 Mon Sep 17 00:00:00 2001 From: jochenberger Date: Thu, 29 Jun 2017 12:14:18 +0200 Subject: [PATCH 1/5] Update Travis badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0109c7ba..42d3cd47 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Jayway JsonPath **A Java DSL for reading JSON documents.** -[![Build Status](https://travis-ci.org/jayway/JsonPath.svg?branch=master)](https://travis-ci.org/jayway/JsonPath) +[![Build Status](https://travis-ci.org/json-path/JsonPath.svg?branch=master)](https://travis-ci.org/jayway/JsonPath) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.jayway.jsonpath/json-path/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.jayway.jsonpath/json-path) [![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.jayway.jsonpath/json-path/badge.svg)](http://www.javadoc.io/doc/com.jayway.jsonpath/json-path) From 78befbb5ee80fb8e403f76375c8d693c60ba46e0 Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Thu, 29 Jun 2017 12:48:13 +0200 Subject: [PATCH 2/5] `null` should be mapped to `null` (fixes #351) --- .../spi/mapper/GsonMappingProvider.java | 6 ++++++ .../jayway/jsonpath/GsonJsonProviderTest.java | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/GsonMappingProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/GsonMappingProvider.java index 7e5153b4..2b5ead3c 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/GsonMappingProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/GsonMappingProvider.java @@ -62,6 +62,9 @@ public class GsonMappingProvider implements MappingProvider { @Override public T map(Object source, Class targetType, Configuration configuration) { + if(source == null){ + return null; + } try { return factory.call().getAdapter(targetType).fromJsonTree((JsonElement) source); } catch (Exception e){ @@ -71,6 +74,9 @@ public class GsonMappingProvider implements MappingProvider { @Override public T map(Object source, TypeRef targetType, Configuration configuration) { + if(source == null){ + return null; + } try { return (T) factory.call().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source); } catch (Exception e){ diff --git a/json-path/src/test/java/com/jayway/jsonpath/GsonJsonProviderTest.java b/json-path/src/test/java/com/jayway/jsonpath/GsonJsonProviderTest.java index 1fc85bd9..5cca22b4 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/GsonJsonProviderTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/GsonJsonProviderTest.java @@ -3,6 +3,8 @@ package com.jayway.jsonpath; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.jayway.jsonpath.spi.json.GsonJsonProvider; +import com.jayway.jsonpath.spi.mapper.GsonMappingProvider; import com.jayway.jsonpath.spi.mapper.MappingException; import org.junit.Test; @@ -181,6 +183,25 @@ public class GsonJsonProviderTest extends BaseTest { using(GSON_CONFIGURATION).parse(JSON).read("$", typeRef); } + + @Test + // https://github.com/json-path/JsonPath/issues/351 + public void no_error_when_mapping_null() throws IOException { + + Configuration configuration = Configuration + .builder() + .mappingProvider(new GsonMappingProvider()) + .jsonProvider(new GsonJsonProvider()) + .options(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS) + .build(); + + String json = "{\"M\":[]}"; + + String result = JsonPath.using(configuration).parse(json).read("$.M[0].A[0]", String.class); + + assertThat(result).isNull(); + } + public static class FooBarBaz { public T gen; From d5acf25b9f9ee43e88fe76b7cdb866f963026e30 Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Thu, 29 Jun 2017 14:28:50 +0200 Subject: [PATCH 3/5] fix evaluation of `empty` with wrapping providers (fixes #366) --- .../jsonpath/internal/filter/ValueNode.java | 9 +++--- .../JacksonJsonNodeJsonProviderTest.java | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java index 08d257c4..9989dd95 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java @@ -359,20 +359,19 @@ public abstract class ValueNode { } public boolean isArray(Predicate.PredicateContext ctx) { - return parse(ctx) instanceof List; + return ctx.configuration().jsonProvider().isArray(parse(ctx)); } public boolean isMap(Predicate.PredicateContext ctx) { - return parse(ctx) instanceof Map; + return ctx.configuration().jsonProvider().isMap(parse(ctx)); } public int length(Predicate.PredicateContext ctx) { - return isArray(ctx) ? ((List) parse(ctx)).size() : -1; + return isArray(ctx) ? ctx.configuration().jsonProvider().length(parse(ctx)) : -1; } public boolean isEmpty(Predicate.PredicateContext ctx) { - if (isArray(ctx)) return ((List) parse(ctx)).size() == 0; - else if (isMap(ctx)) return ((Map) parse(ctx)).size() == 0; + if (isArray(ctx) || isMap(ctx)) return ctx.configuration().jsonProvider().length(parse(ctx)) == 0; else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0; return true; } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java b/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java index 09401682..d58117bd 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java @@ -109,6 +109,35 @@ public class JacksonJsonNodeJsonProviderTest extends BaseTest { using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef); } + + @Test + // https://github.com/json-path/JsonPath/issues/366 + public void empty_array_check_works() throws IOException { + String json = "[" + + " {" + + " \"name\": \"a\"," + + " \"groups\": [{" + + " \"type\": \"phase\"," + + " \"name\": \"alpha\"" + + " }, {" + + " \"type\": \"not_phase\"," + + " \"name\": \"beta\"" + + " }]" + + " }, {" + + " \"name\": \"b\"," + + " \"groups\": [{" + + " \"type\": \"phase\"," + + " \"name\": \"beta\"" + + " }, {" + + " \"type\": \"not_phase\"," + + " \"name\": \"alpha\"" + + " }]" + + " }" + + "]"; + ArrayNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(json).read("$[?(@.groups[?(@.type == 'phase' && @.name == 'alpha')] empty false)]"); + assertThat(node.size()).isEqualTo(1); + assertThat(node.get(0).get("name").asText()).isEqualTo("a"); + } public static class FooBarBaz { public T gen; From b4aec323381c8f6b1c63fa574392830326c3a323 Mon Sep 17 00:00:00 2001 From: kallestenflo Date: Thu, 29 Jun 2017 18:08:33 +0200 Subject: [PATCH 4/5] Fixed travis build icon --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 42d3cd47..d802f403 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Jayway JsonPath **A Java DSL for reading JSON documents.** -[![Build Status](https://travis-ci.org/json-path/JsonPath.svg?branch=master)](https://travis-ci.org/jayway/JsonPath) +[![Build Status](https://travis-ci.org/kallestenflo/json-path.svg?branch=master)](https://travis-ci.org/kallestenflo/json-path) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.jayway.jsonpath/json-path/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.jayway.jsonpath/json-path) [![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.jayway.jsonpath/json-path/badge.svg)](http://www.javadoc.io/doc/com.jayway.jsonpath/json-path) From 260ac60a9a3db3497bae9c6275b4a850bf1ae95c Mon Sep 17 00:00:00 2001 From: kallestenflo Date: Thu, 29 Jun 2017 18:15:55 +0200 Subject: [PATCH 5/5] Update README.md Fixed travis build icon. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d802f403..c7802c2c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Jayway JsonPath **A Java DSL for reading JSON documents.** -[![Build Status](https://travis-ci.org/kallestenflo/json-path.svg?branch=master)](https://travis-ci.org/kallestenflo/json-path) +[![Build Status](https://travis-ci.org/json-path/JsonPath.svg?branch=master)](https://travis-ci.org/json-path/JsonPath) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.jayway.jsonpath/json-path/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.jayway.jsonpath/json-path) [![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.jayway.jsonpath/json-path/badge.svg)](http://www.javadoc.io/doc/com.jayway.jsonpath/json-path)