Jackson JSON detecting null and throwing error

Viewed 1207

How do I detect null in the JSON payload and throw an error that null can't be set?

Example:

public class Foo {

        @JsonDeserialize(using = XMLGregorianCalendarDeserializer.class)
        private XMLGregorianCalendar messageDate;

        public XMLGregorianCalendar getMessageDate() {
            return messageDate;
        }

        public void setMessageDate(XMLGregorianCalendar messageDate) {
            this.messageDate = messageDate;
        }

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

Deserializer class for XMLGregorian Calendar:

class XMLGregorianCalendarDeserializer extends
            GregorianCalendarDeserializer {

        private static final long serialVersionUID = 7195196182149599616L;

        @Override
        public XMLGregorianCalendar deserialize(JsonParser jp,
                DeserializationContext ctxt) throws IOException {
            System.out.println("Deserializer called");
            try {

                if (jp.getText() != null && jp.getText().trim().length() == 0) {
                    throw new JsonParseException(
                            "The string:'"
                                    + jp.getText()
                                    + "' is not a valid XML Gregorian format date/time.",
                            jp.getCurrentLocation());
                }
                return super.deserialize(jp, ctxt);
            } catch (JsonProcessingException e) {
                throw new JsonParseException("The string:'" + jp.getText()
                        + "' is not a valid XML Gregorian format date/time.",
                        jp.getCurrentLocation());
            }
        }

        @Override
        public XMLGregorianCalendar getNullValue() {
            System.out.println("Get null value called >>>>>>>>>>>>>> ");
            return Utility.getXMLGregorianCal();
        }

    }

I have the following String (the JSON payload) and I like to validate when the messageDate is null.

String payload = "{\"messageDate\":null,\"name\":\"someName\"}";
ObjectMapper mapper = new ObjectMapper();
Foo f1 = mapper.readValue(payload, Foo.class);
System.out.println("Foo is " + f1.getMessageDate() + f1.getName());

When messageDate is null, I noticed that getNullValue() in the custom serializer class gets called. Right now I am sending an instance of XMLGregorianCalendar when it is null.

The following gets printed

Get null value called >>>>>>>>>>>>>> 
Foo is 2013-12-09T17:26:00.642-08:00someName

Instead of returning a non null value, I would like to throw an error.

Also I observed that after getNonValue() gets called, the deserialize(...) method is not called.

Looks like when it sees null, it calls getNullValue() and whatever value returned in the method is used.

When it is not null, the actual value in the payload is used and deserialize(...) method gets invoked.

Instead of this behavior, I would like to throw an error when it is null in the payload.

How do I do it?

0 Answers
Related