Red Bit
Red Bit

Reputation: 485

JacksonMapper to deserialize null value

I am going to deserialize Json null value to Java Object empty string

I am able to make my custom deserializer, but when the Json value is null, it did not go into the deserializer.

How should I deserialize it?

Thanks in advance!

public class CustomStringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException,
        JsonProcessingException {

    String str = jsonparser.getText();

    try {
        return (str == null) ? "" : str;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

}

   public CustomObjectMapper() {
        SimpleModule _module = new SimpleModule("Module", new Version(1, 9, 10, "FINAL"));
        _module.addDeserializer(String.class, new CustomStringDeserializer());
}


Thanks @nutlike
I do this by

    @Override
public String getNullValue() {
    return "";
}

Upvotes: 4

Views: 3738

Answers (1)

nutlike
nutlike

Reputation: 4975

Maybe it would be sufficient to overwrite the method getNullValue()?

public class CustomStringDeserializer extends JsonDeserializer<String> {

@Override
public String deserialize(JsonParser jsonparser,
        DeserializationContext deserializationcontext) throws IOException,
        JsonProcessingException {

    return jsonparser.getText();

}

@Override
public String getNullValue() {
    return "";
}

}

Upvotes: 2

Related Questions