Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing using JsonCreator that returns null causes NullPointerException #597

Closed
seppyk opened this issue Oct 31, 2014 · 1 comment
Closed
Milestone

Comments

@seppyk
Copy link

seppyk commented Oct 31, 2014

Using jackson-databind-2.3.3...

Created a Jackson annotated POJO that uses a JsonCreator to deserialize from JSON to the Java object.

private class JsonEntity {
  private final String type;
  private final UUID id;

  private JsonEntity(String type, UUID id) {
    this.type = type;
    this.id = id;
  }

  @JsonCreator
  public static JsonEntity create(@JsonProperty("type") String type, @JsonProperty("id") UUID id) {
    if (StringUtils.isNotBlank(type) && (id != null)) {
      return new JsonEntity(type, id);
    }

    return null;
  }
}

Next, I attempted to deserialize a JSON string using the creator above.

ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"type\" : \"     \", \"id\" : \"000c0ffb-a0d6-4d2e-a379-4aeaaf283599\" }";
objectMapper.readValue(json, JsonEntity.class);

A NullPointerException occurs because the JsonCreator is returning null. This should be handled more gracefully by throwing a Jackson-specific exception that details the deserialized bean is being returned as null.

java.lang.NullPointerException
  at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:342)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1071)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:268)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2098)
at com.test.JsonTest.testJsonEntityWithBlankType(JsonTest.java:19)
@cowtowncoder
Copy link
Member

Sounds reasonable, yes.

herau added a commit to herau/jackson-databind that referenced this issue Nov 2, 2014
@herau herau mentioned this issue Nov 2, 2014
cowtowncoder added a commit that referenced this issue Nov 4, 2014
cowtowncoder added a commit that referenced this issue Nov 4, 2014
@cowtowncoder cowtowncoder added this to the 2.5.0 milestone Nov 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants