JavaRush /Java Blog /Random EN /Introduction to the Jackson Framework
Mikhail Fufaev
Level 35
Москва

Introduction to the Jackson Framework

Published in the Random EN group
On a cold spring night, I finally reached level 33. Rubbing my programmer hands, I was already preparing to embrace the entire field of JSON serialization and deserialization, but, unfortunately, I did not understand anything. I didn’t remember the text of the lecture, but the problems were solved somehow intuitively. In this regard, I decided to delve into the wilds of the Jackson Framework and figure out what this JSON is.
Jackson Framework
I will try to present all my knowledge in a practical and concise manner in a cheat sheet format (both for myself and for readers). Journey to Jackson Annotations. The first thing we encounter on the way to JSON is the @JsonAutoDetect annotation. At first glance, it’s an easy summary, but it took the author the longest time to figure it out. The abstract has 5 methods we need:
  • fieldVisibility () - Serializes only fields with the specified access modifier
  • getterVisibility()/setterVisibility() - serializes fields whose getter/setter has the specified access modifier
  • isGetterVisibility() - separate implementation for boolean getters
It is important to understand that the methods work disjunctively. Those. if the field matches at least one of the parameters specified in the annotation, then it will be included in JSON. Try to answer what this code will output if we create an instance using a parameterless constructor:
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
        getterVisibility        = JsonAutoDetect.Visibility.PUBLIC_ONLY,
        setterVisibility        = JsonAutoDetect.Visibility.PUBLIC_ONLY,
        isGetterVisibility      = JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC)
public class HeadClass {
    public String name;
    private Map<String, String> properties;
    public Queue<String> queue;
    protected List<String> list;
    private int age;
    private int number;
    private boolean isHead;

    protected HeadClass(int age) {
        this.age = age;
    }

    public HeadClass() {}

    Map<String, String> getProperties() {
        return properties;
    }

    protected boolean isHead() {
        return isHead;
    }

    protected void setProperties(Map<String, String> properties) {
        this.properties = properties;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}
What if we remove isGetterVisibility? The four methods listed configured the serialization process. The fifth, in turn, regulates the deserialization process:
  • creatorVisibility() is the most difficult method to understand. It works with constructors and with factory methods (methods that create an object when called). Let's look at an example:
@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC)
public class HeadClass {
    public String name;
    public int id;

    HeadClass(@JsonProperty(value = "name") String name, @JsonProperty(value = "id") int id) {
        this.name = name;
        this.id = id;
    }

    protected HeadClass(String name) {
        this.name = name;
        this.id = 123;
    }

    protected HeadClass(int id) {
        this.id = id;
        this.name = "Yes!";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public static void main(String[] args) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    String forDeserialize = "{\"name\":\"No!\",\"id\":123}";
    StringReader reader = new StringReader(forDeserialize);

    HeadClass headClass1 = (HeadClass) mapper.readValue(reader, HeadClass.class);
}
Important note on the deserialization mechanism! When we try to create an object from JSON, the constructor of the required class will be searched with the same set of parameters as in the JSON object. In the example above, our JSON object consists of two parameters: name, id. Guess which constructor it will call. And yes, if we compile this code, it will throw an error, why? Because we have limited the visibility of the constructor (only constructors with the protected, public modifier are visible). If you remove creatorVisibility it will work. The question arises. What is @JsonProperty in the constructor? I think I’ll talk about this in the next part of the article. PS Gentlemen, I would really like to get at least some response about the article. I wonder if this topic is in demand and whether it is worth continuing, because there are a lot of topics and they are all very interesting. I would also like to consider annotations such as @JsonView, @JsonManagedReference, @JsonBackReference, @JsonUnwrapped, etc. Thank you :)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION