Skip to content

Commit 4fece40

Browse files
authored
Merge branch 'master' into fix-jackson-pojo
2 parents a71023c + 260ac60 commit 4fece40

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Jayway JsonPath
33

44
**A Java DSL for reading JSON documents.**
55

6-
[![Build Status](https://travis-ci.org/jayway/JsonPath.svg?branch=master)](https://travis-ci.org/jayway/JsonPath)
6+
[![Build Status](https://travis-ci.org/json-path/JsonPath.svg?branch=master)](https://travis-ci.org/json-path/JsonPath)
77
[![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)
88
[![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.jayway.jsonpath/json-path/badge.svg)](http://www.javadoc.io/doc/com.jayway.jsonpath/json-path)
99

json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,20 +359,19 @@ public Object getJson() {
359359
}
360360

361361
public boolean isArray(Predicate.PredicateContext ctx) {
362-
return parse(ctx) instanceof List;
362+
return ctx.configuration().jsonProvider().isArray(parse(ctx));
363363
}
364364

365365
public boolean isMap(Predicate.PredicateContext ctx) {
366-
return parse(ctx) instanceof Map;
366+
return ctx.configuration().jsonProvider().isMap(parse(ctx));
367367
}
368368

369369
public int length(Predicate.PredicateContext ctx) {
370-
return isArray(ctx) ? ((List) parse(ctx)).size() : -1;
370+
return isArray(ctx) ? ctx.configuration().jsonProvider().length(parse(ctx)) : -1;
371371
}
372372

373373
public boolean isEmpty(Predicate.PredicateContext ctx) {
374-
if (isArray(ctx)) return ((List) parse(ctx)).size() == 0;
375-
else if (isMap(ctx)) return ((Map) parse(ctx)).size() == 0;
374+
if (isArray(ctx) || isMap(ctx)) return ctx.configuration().jsonProvider().length(parse(ctx)) == 0;
376375
else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0;
377376
return true;
378377
}

json-path/src/main/java/com/jayway/jsonpath/spi/mapper/GsonMappingProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public Gson call() {
6262

6363
@Override
6464
public <T> T map(Object source, Class<T> targetType, Configuration configuration) {
65+
if(source == null){
66+
return null;
67+
}
6568
try {
6669
return factory.call().getAdapter(targetType).fromJsonTree((JsonElement) source);
6770
} catch (Exception e){
@@ -71,6 +74,9 @@ public <T> T map(Object source, Class<T> targetType, Configuration configuration
7174

7275
@Override
7376
public <T> T map(Object source, TypeRef<T> targetType, Configuration configuration) {
77+
if(source == null){
78+
return null;
79+
}
7480
try {
7581
return (T) factory.call().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source);
7682
} catch (Exception e){

json-path/src/test/java/com/jayway/jsonpath/GsonJsonProviderTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.google.gson.JsonArray;
44
import com.google.gson.JsonElement;
55
import com.google.gson.JsonObject;
6+
import com.jayway.jsonpath.spi.json.GsonJsonProvider;
7+
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
68
import com.jayway.jsonpath.spi.mapper.MappingException;
79
import org.junit.Test;
810

@@ -181,6 +183,25 @@ public void test_type_ref_fail() throws IOException {
181183

182184
using(GSON_CONFIGURATION).parse(JSON).read("$", typeRef);
183185
}
186+
187+
@Test
188+
// https://github.com/json-path/JsonPath/issues/351
189+
public void no_error_when_mapping_null() throws IOException {
190+
191+
Configuration configuration = Configuration
192+
.builder()
193+
.mappingProvider(new GsonMappingProvider())
194+
.jsonProvider(new GsonJsonProvider())
195+
.options(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS)
196+
.build();
197+
198+
String json = "{\"M\":[]}";
199+
200+
String result = JsonPath.using(configuration).parse(json).read("$.M[0].A[0]", String.class);
201+
202+
assertThat(result).isNull();
203+
}
204+
184205

185206
public static class FooBarBaz<T> {
186207
public T gen;

json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,33 @@ public void setPropertyWithPOJO() {
122122
String id = context.read("$.data.id", String.class);
123123
assertThat(id).isEqualTo(uuid.toString());
124124
}
125+
// https://github.com/json-path/JsonPath/issues/366
126+
public void empty_array_check_works() throws IOException {
127+
String json = "[" +
128+
" {" +
129+
" \"name\": \"a\"," +
130+
" \"groups\": [{" +
131+
" \"type\": \"phase\"," +
132+
" \"name\": \"alpha\"" +
133+
" }, {" +
134+
" \"type\": \"not_phase\"," +
135+
" \"name\": \"beta\"" +
136+
" }]" +
137+
" }, {" +
138+
" \"name\": \"b\"," +
139+
" \"groups\": [{" +
140+
" \"type\": \"phase\"," +
141+
" \"name\": \"beta\"" +
142+
" }, {" +
143+
" \"type\": \"not_phase\"," +
144+
" \"name\": \"alpha\"" +
145+
" }]" +
146+
" }" +
147+
"]";
148+
ArrayNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(json).read("$[?(@.groups[?(@.type == 'phase' && @.name == 'alpha')] empty false)]");
149+
assertThat(node.size()).isEqualTo(1);
150+
assertThat(node.get(0).get("name").asText()).isEqualTo("a");
151+
}
125152

126153
public static class FooBarBaz<T> {
127154
public T gen;

0 commit comments

Comments
 (0)