34

I am trying a simple JSON to de-serialize in to java object. I am however, getting empty String values for java.lang.String property values. In rest of the properties, blank values are converting to null values(which is what I want).

My JSON and related Java class are listed below.

JSON string:

{
  "eventId" : 1,
  "title" : "sample event",
  "location" : "" 
}

EventBean class POJO:

public class EventBean {

    public Long eventId;
    public String title;
    public String location;

}

My main class code:

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

try {
    File file = new   File(JsonTest.class.getClassLoader().getResource("event.txt").getFile());

    JsonNode root = mapper.readTree(file);
    // find out the applicationId

    EventBean e = mapper.treeToValue(root, EventBean.class);
    System.out.println("It is " + e.location);
}

I was expecting print "It is null". Instead, I am getting "It is ". Obviously, Jackson is not treating blank String values as NULL while converting to my String object type.

I read somewhere that it is expected. However, this is something I want to avoid for java.lang.String too. Is there a simple way?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Mayur
  • 440
  • 1
  • 5
  • 13

5 Answers5

35

Jackson will give you null for other objects, but for String it will give empty String.

But you can use a Custom JsonDeserializer to do this:

class CustomDeserializer extends JsonDeserializer<String> {

    @Override
    public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
        JsonNode node = jsonParser.readValueAsTree();
        if (node.asText().isEmpty()) {
            return null;
        }
        return node.toString();
    }

}

In class you have to use it for location field:

class EventBean {
    public Long eventId;
    public String title;

    @JsonDeserialize(using = CustomDeserializer.class)
    public String location;
}
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45
  • 6
    This worked for me too, but using readValueAsTree() wrapped the string in extra quotes. So to avoid it I used instead jsonParser.readValueAs(String.class); – kopelitsa Mar 29 '16 at 14:57
  • 6
    @kopelitsa extra quotes issue could be resolved by "return node.asText();" – gce Apr 13 '17 at 12:06
  • 1
    Can we apply the JsonDeserialize annotation to class level also , so that all the string attributes with empty string under that will convert to null ? – Ansar Samad Dec 02 '20 at 18:12
  • @AnsarSamad Did you have an answer to your question? I would need to understand if it can be inserted at the class level – Jacket Mar 12 '21 at 09:43
  • @Jacket yes it also works on the class level for all String fields. – slindenau Jun 03 '22 at 14:13
  • @kopelitsa yes this works, but only if this deserializer is never globally registered as a String deserializer via modules (see other answers below). Then you would end up with an infinite loop, so beware. – slindenau Jun 05 '22 at 09:09
  • @SachinGupta your code contains a small error; the final return statement should also use `node.asText()` so you don't return the double quotes. Please update your answer, i've tried to improve it with an edit but the reviewers rejected it. – slindenau Jun 06 '22 at 07:28
23

It is possible to define a custom deserializer for the String type, overriding the standard String deserializer:

this.mapper = new ObjectMapper();

SimpleModule module = new SimpleModule();

module.addDeserializer(String.class, new StdDeserializer<String>(String.class) {

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String result = StringDeserializer.instance.deserialize(p, ctxt);
        if (StringUtils.isEmpty(result)) {
            return null;
        }
        return result;
    }
});

mapper.registerModule(module);

This way all String fields will behave the same way.

jgesser
  • 243
  • 2
  • 7
8

You might first like to see if there has been any progress on the Github issue requesting this exact feature.

For those using Spring Boot: The answer from jgesser was the most helpful to me, but I spent a while trying to work out the best way to configure it in Spring Boot.

Actually, the documentation says:

Any beans of type com.fasterxml.jackson.databind.Module are automatically registered with the auto-configured Jackson2ObjectMapperBuilder and are applied to any ObjectMapper instances that it creates.

So here's jgesser's answer expanded into something you can copy-paste into a new class in a Spring Boot application

@Configuration
public class EmptyStringAsNullJacksonConfiguration {

  @Bean
  SimpleModule emptyStringAsNullModule() {
    SimpleModule module = new SimpleModule();

    module.addDeserializer(
        String.class,
        new StdDeserializer<String>(String.class) {

          @Override
          public String deserialize(JsonParser parser, DeserializationContext context)
              throws IOException {
            String result = StringDeserializer.instance.deserialize(parser, context);
            if (StringUtils.isEmpty(result)) {
              return null;
            }
            return result;
          }
        });

    return module;
  }
}
Fletch
  • 4,829
  • 2
  • 41
  • 55
  • Alas, the issue you're referring to has been closed by (one of) the maintainer(s) due to seemingly apparent lack of interest... – maxxyme Dec 14 '21 at 17:54
3

I could get this by following configuration.

final ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
shashi ranjan
  • 380
  • 4
  • 8
  • 6
    This doesn't work - that deserialization feature is intended just for empty strings as the whole JSON expression (indicated by that "NULL_OBJECT") - not for object attributes. – Ján Halaša Aug 25 '20 at 13:56
0

it is possible to use JsonCreator annotation. It worked for me

public class Foo {
private String field;

 @JsonCreator
 public Foo(
   @JsonProrerty("field") String field) {
     this.field = StringUtils.EMPTY.equals(field) ? null : field ;
}
}
Maria
  • 1
  • 1