2

I'm creating a custom Jackson deserializer class to map a JSON payload to an object. In my deserialize method I did some checks on the JSON payload to see whether any fields are missing or incongruent to the POJO.

I tried throwing exceptions in the deserialize method with something like this:

@Override
public MyObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
   // if some fields are missing throw error:
   throw new MissingFieldsException("name field is missing");
} 

However, I'm not allowed to to throw my own exception in the deserialize method because the method implements a interface which I can only throw IOException and JsonProcessingException:

public abstract T deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException;

In this case, how do I do validations of JSON payloads when doing deserialisations?

Carven
  • 14,988
  • 29
  • 118
  • 161
  • you can configure the `ObjectMapper` to throw that exception when fields are missing – Ryuzaki L Dec 24 '19 at 04:20
  • @Deadpool Do you have an example of how I could do that? I only knew I could register my deserialiser to the ObjectMapper. I suppose the logic to check the fields will have to be in the deserialiser class, wouldn’t it? – Carven Dec 24 '19 at 04:22
  • may be check this you don't need custom deserializer https://stackoverflow.com/questions/28355891/configure-jackson-to-throw-an-exception-when-a-field-is-missing – Ryuzaki L Dec 24 '19 at 04:24
  • @Deadpool The missing field check that I have needs a little more customisation. If the Json doesn’t have the field `name`, then it should have the field `fullname`. Otherwise, it will then throw exception. So this will require a small piece of logic. Would configuring the object mapper with DeserializationFeature be able to do this too? – Carven Dec 24 '19 at 04:43
  • Just curious, why not throw JsonProcessingException with your message? – Sunil Dabburi Dec 24 '19 at 04:49
  • @SunilDabburi I can't because JsonProcessingException is protected. So I can't throw it directly in my class. – Carven Dec 24 '19 at 05:10
  • 1
    You can throw `JsonMappingException` which is an accessible sub-class of `JsonProcessingException` or even better define your `MissingFieldsException` as the sub-class – Sunil Dabburi Dec 24 '19 at 05:38

1 Answers1

1

To be exact the class JsonProcessingException is not "protected". The constructors for that class are. So you can easily extend that class - as comments suggest - with some public constructor, for example:

@SuppressWarnings("serial")
public static class MyJsonProcessingException extends JsonProcessingException {
    protected MyJsonProcessingException(String msg) {
        super(msg);
    }
}

You can also declare it as anonymous inner class by providing the body with new operator. Like:

throw new JsonProcessingException("Error msg") {};

For this you might also want to add either something like:

private static final long serialVersionUID = 1L;

in the body of above anonymous inner class or add the annotation @SuppressWarnings("serial") to the method where this is thrown, to get rid of warnings about missing serial.

Generally you can also throw any unchecked exception, like RuntimeException itself or like:

@SuppressWarnings("serial")
public static class MyJsonProcessingRuntimeException extends RuntimeException {}

but in case of RuntimeException you need to be more careful in your coding - so be aware that it can be thrown without any declaration - so extending JsonProcessingException is prefereble, IMO.

pirho
  • 11,565
  • 12
  • 43
  • 70
  • What does that `@SuppressWarnings(“serial”)` do? – Carven Dec 24 '19 at 18:01
  • @Carven Because throwables implement `Serializable` you need to define `serialVersionUID` for them and if you do not IDE & perhaps compiler shows you warnings about that. Usually you do not want to declare such and that is to suppress that warning since it is not fatal and might only irritate. It is up top you if using those or not. I use. – pirho Dec 24 '19 at 18:20