@JsonRawValue Jackson Annotation

Let’s first discuss what problem does @JsonRawValue solves?

Let’s consider a nested JSON as shown below.

{
  "firstName": "Codekru",
  "lastName": "Programming",
  "age": 20,
  "description": {
    "Profession": "Blogging",
    "BloggingType": "Programming"
  }
}

Now, what if I want to make this JSON from an object. There are multiple ways to do this but let’s say that we passed the nested JSON as a string to the description field, as shown below.

		person.setDescription("{\r\n"
				+ "    \"Profession\": \"Blogging\",\r\n"
				+ "    \"BloggingType\": \"Programming\"\r\n"
				+ "  }");

But if we try to serialize our object now, we will get the below JSON instead of the nice clean one.

{
  "firstName" : "Codekru",
  "lastName" : "Programming",
  "age" : 20,
  "description" : "{\r\n    \"Profession\": \"Blogging\",\r\n    \"BloggingType\": \"Programming\"\r\n  }"
}

So, what can you do so that these escaping characters do not appear in your final JSON? So, the answer is that you can use @JsonRawValue, which will serialize the string property as it is without escaping or quoting. It is helpful for text properties already in JSON format, which does not need escaping or quoting.

Now, let’s see an example with or without the @JsonRawValue annotation to see what magic this annotation does?

Without using the @JsonRawValue annotation

In the below example, we have not used JsonRawValue annotation. We are passing JSON value to the description field and then just checking the serialized JSON.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Person {

    private String firstName;
    private String lastName;
    private int age;
    private String description;

    public Person() {
        super();
    }

    public Person(String firstName, String lastName, int age) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public static void main(String[] args) throws JsonProcessingException {
        Person person = new Person();
        person.setFirstName("Codekru");
        person.setLastName("Programming");
        person.setAge(20);
		person.setDescription("{\r\n"
				+ "    \"Profession\": \"Blogging\",\r\n"
				+ "    \"BloggingType\": \"Programming\"\r\n"
				+ "  }");
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(person)); // serializing object
    }
}

Output:

{
  "firstName" : "Codekru",
  "lastName" : "Programming",
  "age" : 20,
  "description" : "{\r\n    \"Profession\": \"Blogging\",\r\n    \"BloggingType\": \"Programming\"\r\n  }"
}

As we can see here that the description field has escaping characters in the resulting JSON. So, let’s try to do the same but now with the @JsonRawValue annotation.

With @JsonRawValue

import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Person {

    private String firstName;
    private String lastName;
    private int age;
    
    @JsonRawValue
    private String description;

    public Person() {
        super();
    }

    public Person(String firstName, String lastName, int age) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public static void main(String[] args) throws JsonProcessingException {
        Person person = new Person();
        person.setFirstName("Codekru");
        person.setLastName("Programming");
        person.setAge(20);
		person.setDescription("{\r\n"
				+ "    \"Profession\": \"Blogging\",\r\n"
				+ "    \"BloggingType\": \"Programming\"\r\n"
				+ "  }");
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(person)); // serializing object
    }
}

Output:

{
  "firstName" : "Codekru",
  "lastName" : "Programming",
  "age" : 20,
  "description" : {
    "Profession": "Blogging",
    "BloggingType": "Programming"
  }
}

So, here we can see that we got a nice clean JSON.

References – https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonRawValue.html

We hope that you have liked this article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Other Jackson Annotations –

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *