Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson @JsonProperty(required=true) doesn't throw an exception

Tags:

java

json

jackson

I am using jackson 2.2 annotation @JsonProperty with required set to true. While deserializing json file which doesn't contain that property via ObjectMapper readValue() method no exception is being thrown. Is it supposed to work in a different way or did I missed something?

My dto class:

public class User {     public enum Gender {MALE, FEMALE}      ;      public static class Name {         private String _first, _last;          public String getFirst() {             return _first;         }          public String getLast() {             return _last;         }          public void setFirst(String s) {             _first = s;         }          public void setLast(String s) {             _last = s;         }     }      private Gender _gender;     private Name _name;     private boolean _isVerified;     private byte[] _userImage;      @JsonProperty(value ="NAAME",required = true)     public Name getName() {         return _name;     }      @JsonProperty("VERIFIED")     public boolean isVerified() {         return _isVerified;     }      @JsonProperty("GENDER")     public Gender getGender() {         return _gender;     }     @JsonProperty("IMG")     public byte[] getUserImage() {         return _userImage;     }      @JsonProperty(value ="NAAME",required = true)     public void setName(Name n) {         _name = n;     }     @JsonProperty("VERIFIED")     public void setVerified(boolean b) {         _isVerified = b;     }     @JsonProperty("GENDER")     public void setGender(Gender g) {         _gender = g;     }     @JsonProperty("IMG")     public void setUserImage(byte[] b) {         _userImage = b;     } } 

This is how do I deserialize the class:

public class Serializer {     private ObjectMapper mapper;      public Serializer() {         mapper = new ObjectMapper();         SimpleModule sm = new SimpleModule("PIF deserialization");         mapper.registerModule(sm);     }      public void writeUser(File filename, User user) throws IOException {         mapper.writeValue(filename, user);     }      public User readUser(File filename) throws IOException {           return mapper.readValue(filename, User.class);       } } 

This is how it is actually called:

    Serializer serializer = new Serializer();     User result = serializer.readUser(new File("user.json")); 

Actuall json looks like:

{"GENDER":"FEMALE","VERIFIED":true,"IMG":"AQ8="} 

I would expect that since _name is not specified in json file and is required that the exception will be thrown.

like image 756
jaksky Avatar asked Aug 19 '13 18:08

jaksky


People also ask

Is @JsonProperty required?

You definitely don't need all those @jsonProperty . Jackson mapper can be initialized to sereliazie/deserialize according to getters or private members, you of course need only the one you are using.

What does @JsonProperty annotation do?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

How do I ignore JsonProperty?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do you tell Jackson to ignore a field during serialization?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.


2 Answers

With Jackson 2.6 you can use required, however you have to do it using JsonCreator

For example:

public class MyClass {      @JsonCreator     public MyClass(@JsonProperty(value = "x", required = true) Integer x, @JsonProperty(value = "value_y", required = true) Integer y) {         this.x = x;         this.y = y;     }      private Integer x;     private Integer y; } 

If x or y are not present an exception will be thrown when trying to deserialize it.

like image 85
Bojan Petkovic Avatar answered Sep 22 '22 21:09

Bojan Petkovic


As per Jackson annotations javadocs: "Note that as of 2.0, this property is NOT used by BeanDeserializer: support is expected to be added for a later minor version."

That is: no validation is performed using this settings. It is only (currently) used for generating JSON Schema, or by custom code.

like image 25
StaxMan Avatar answered Sep 21 '22 21:09

StaxMan