wanglingsong / JsonSurfer

A streaming JsonPath processor in Java
MIT License
294 stars 55 forks source link

Fix NPE cause by calling collectOne() when using JsonSurferJackson instance #40

Closed fonoisrev closed 6 years ago

fonoisrev commented 6 years ago

When i searching something not found using the JsonSurferJackson instance as below.

JsonSurfer surfer = JsonSurferJackson.INSTANCE;
surfer.collectOne(read("sample.json"), "$..authors"); // no authors in sample.json

Then I get a NullPointerException as below.

java.lang.NullPointerException
    at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:2738)
    at org.jsfr.json.provider.JacksonProvider.cast(JacksonProvider.java:115)
    at org.jsfr.json.provider.JacksonProvider.cast(JacksonProvider.java:35)
    at org.jsfr.json.JsonSurfer.collectOne(JsonSurfer.java:418)
    at org.jsfr.json.JsonSurfer.collectOne(JsonSurfer.java:400)
    at org.jsfr.json.JsonSurfer.collectOne(JsonSurfer.java:389)
        ...

So I debug it, found that problem cause by calling org.jsfr.json.provider.JacksonProvider#cast with null JsonNode param,because there's no null value checking nethier in org.jsfr.json.provider.JacksonProvider#cast nor in com.fasterxml.jackson.databind.ObjectMapper#treeToValue.

This pull request to fix ahead problem.

The problen was not found when using the three other providers (Gson, JsonSimple, Fastjson ).