Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public Gson call() {

@Override
public <T> T map(Object source, Class<T> targetType, Configuration configuration) {
if(source == null){
return null;
}
try {
return factory.call().getAdapter(targetType).fromJsonTree((JsonElement) source);
} catch (Exception e){
Expand All @@ -71,6 +74,9 @@ public <T> T map(Object source, Class<T> targetType, Configuration configuration

@Override
public <T> T map(Object source, TypeRef<T> targetType, Configuration configuration) {
if(source == null){
return null;
}
try {
return (T) factory.call().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source);
} catch (Exception e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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;

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

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<T> {
public T gen;
Expand Down