tenzin
tenzin

Reputation: 47

@jsondeserialize does not work when json value is null

@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

Upvotes: 1

Views: 1929

Answers (2)

PMorganCA
PMorganCA

Reputation: 760

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;
    }
}

Upvotes: 1

Arashsoft
Arashsoft

Reputation: 2767

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.

Upvotes: 0

Related Questions