Friday, 19 May 2023

Jackson: @JsonValue: Customize the serialized data to be returned

Jackson uses the field names as keys and the field value as the value during serialization. However, using the @JsonValue annotation we can customize the serialization process by specifying which method or field should be used as the serialized value.

 

Example 1: Applying @JsonValue annotation on a method.

 


Employee1.java

package com.sample.app.model;

import com.fasterxml.jackson.annotation.JsonValue;

public class Employee1 {

    private int id;
    private String name;
    private int age;

    public Employee1() {
    }

    public Employee1(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @JsonValue
    public String serializedInfo() {
        return "[ id = " + id + ", name = " + name + ", age = " + age + "]";
    }

}

 

As you see above snippet, I annotated ‘serializedInfo’ method with @JsonValue annotation. When you serialize the Employee1 object using Jackson library, it return the value returned from serializedInfo() method.

 

JsonValueDemo1.java

package com.sample.app;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.Employee1;

public class JsonValueDemo1 {
    
    public static void main(String[] args) throws JsonProcessingException {
        Employee1 emp = new Employee1();
        emp.setId(1);
        emp.setName("Krishna");
        emp.setAge(34);
        
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(emp);

        System.out.println(json);
    }

}

 

Output

"[ id = 1, name = Krishna, age = 34]"

 

Example 2: Applying @JsonValue annotation on a field.

 

Employee2.java

 

package com.sample.app.model;

import com.fasterxml.jackson.annotation.JsonValue;

public class Employee2 {

    private int id;
    private String name;
    private int age;

    @JsonValue
    private String details;

    public Employee2(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.details = "[ id = " + id + ", name = " + name + ", age = " + age + "]";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getDetails() {
        return details;
    }

}

As you see above snippet, the property 'details' is annotated with @JsonValue annotation. Now whenever you serialize the Employee2 object using Jackson, the value of the details property is returned.

 

JsonValueDemo2.java

package com.sample.app;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.Employee2;

public class JsonValueDemo2 {

    public static void main(String[] args) throws JsonProcessingException {
        Employee2 emp = new Employee2(1, "Krishna", 34);


        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(emp);

        System.out.println(json);
    }

}

Output

"[ id = 1, name = Krishna, age = 34]"


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment