1

@JsonDeserialize does not work for null json value in this case. it is throwing error

// I have the following json string.

            {
                "myCount": null
            }

//class

                public class Test {
                  // @JsonDeserialize does not work for null json value in this case. it is throwing error.
                 @JsonDeserialize (using = Custom.class)
                 private double myCount;
            }

//Custom deserializer

             public class Custom extends JsonDeserializer<Double> {
                //implements deserialize method
                @Override
                public Double deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
                String text = jp.getText();
                if (text == null || text.isEmpty()) {
                 return null;
                } else {
                return Double.valueOf(text);
                }
                    }
                }
            }

please let me know. Thanks

Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
tenzin
  • 47
  • 9

2 Answers2

1

If your problem is that you are using a primitive double, then Arashsoft has your answer. However I suspect you will run into other problems.

It looks to me like your deserializer does not do anything more than a default deserializer would do, so yours, as written, is unnecessary.

You are calling jp.getText() which will only return a String if your JSON value is in quotes, like `"myCount" : "123.45", but that's not what you want.

If you want to intercept the null that would normally be assigned to your myCount field, you have to override getNullValue() in your deserializer. The implementation will depend on the JSON library that your are using, but for me, this works:

package ca.mydomain.myapp.util;

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.deser.std.StdDeserializer;

public class JsonDefaultZeroDoubleDeserializer extends StdDeserializer<Double>
{

    protected JsonDefaultZeroDoubleDeserializer () { this (null); }

    protected JsonDefaultZeroDoubleDeserializer (Class<?> vc)
    {
        super (vc);
    }

    @Override
    public Double deserialize (JsonParser sp, DeserializationContext dCtxt) throws IOException, JsonProcessingException
    {
        return sp.getCodec().readTree (sp).asDouble ();
    }

    @Override
    public Double getNullValue ()
    {
        return 0.00;
    }
}
PMorganCA
  • 730
  • 6
  • 24
0

The issue is your field is primitive double instead of Double. Therefore, it cannot accept null.

Change private double myCount; to private Double myCount; and it should work fine.

For more information regarding their difference please see this link.

Arashsoft
  • 2,749
  • 5
  • 35
  • 58
  • is correct that the field has to be `Double` not `double`. However, if the value in the JSON is null, I do not believe the deserializer is called at all. – PMorganCA Aug 13 '18 at 18:24
  • @PMorganCA, It depends on your setting: https://stackoverflow.com/questions/20578846/ignore-missing-properties-during-jackson-json-deserialize-in-java – Arashsoft Aug 13 '18 at 19:21