{"id":4939,"date":"2014-03-30T17:09:22","date_gmt":"2014-03-30T17:09:22","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/json-jackson-deserialization-without-inner-classes-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:09:22","modified_gmt":"2014-03-30T17:09:22","slug":"json-jackson-deserialization-without-inner-classes-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/json-jackson-deserialization-without-inner-classes-collection-of-common-programming-errors\/","title":{"rendered":"Json Jackson deserialization without inner classes-Collection of common programming errors"},"content":{"rendered":"<p>I have a question concerning Json deserialization using Jackson. I would like to deserialize a Json file using a class like this one: (taken from http:\/\/wiki.fasterxml.com\/JacksonInFiveMinutes)<\/p>\n<pre><code>public class User \n{\n    public enum Gender { MALE, FEMALE };\n    public static class Name {\n      private String _first, _last;\n      public String getFirst() { return _first; }\n      public String getLast() { return _last; }\n      public void setFirst(String s) { _first = s; }\n      public void setLast(String s) { _last = s; }\n    }\n    private Gender _gender;\n    private Name _name;\n    private boolean _isVerified;\n    private byte[] _userImage;\n    public Name getName() { return _name; }\n    public boolean isVerified() { return _isVerified; }\n    public Gender getGender() { return _gender; }\n    public byte[] getUserImage() { return _userImage; }\n    public void setName(Name n) { _name = n; }\n    public void setVerified(boolean b) { _isVerified = b; }\n    public void setGender(Gender g) { _gender = g; }\n    public void setUserImage(byte[] b) { _userImage = b; }\n}\n<\/code><\/pre>\n<p>A Json file can be deserialized using the so called &#8220;Full Data Binding&#8221; in this way:<\/p>\n<pre><code>ObjectMapper mapper = new ObjectMapper();\nUser user = mapper.readValue(new File(\"user.json\"), User.class);\n<\/code><\/pre>\n<p>My problem is the usage of the inner class &#8220;Name&#8221;. I would like to do the same thing without using inner classes. The &#8220;User&#8221; class would became like that:<\/p>\n<pre><code>import Name;\nimport Gender;\npublic class User \n{\n    private Gender _gender;\n    private Name _name;\n    private boolean _isVerified;\n    private byte[] _userImage;\n    public Name getName() { return _name; }\n    public boolean isVerified() { return _isVerified; }\n    public Gender getGender() { return _gender; }\n    public byte[] getUserImage() { return _userImage; }\n    public void setName(Name n) { _name = n; }\n    public void setVerified(boolean b) { _isVerified = b; }\n    public void setGender(Gender g) { _gender = g; }\n    public void setUserImage(byte[] b) { _userImage = b; }\n}\n<\/code><\/pre>\n<p>This means to find a way to specify to the mapper all the required classes in order to perform the deserialization.<\/p>\n<p>Is this possible? I looked at the documentation but I cannot find any solution.<\/p>\n<p>My need comes from the fact that I use the Javassist library to create such classes, and it does not support inner or anonymous classes.<\/p>\n<p>Thank you in advance<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a question concerning Json deserialization using Jackson. I would like to deserialize a Json file using a class like this one: (taken from http:\/\/wiki.fasterxml.com\/JacksonInFiveMinutes) public class User { public enum Gender { MALE, FEMALE }; public static class Name { private String _first, _last; public String getFirst() { return _first; } public String [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4939","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4939"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4939\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}