What is the use of @JsonRawValue annotation using Jackson API in Java?

The @JsonRawValue annotation can be used for both methods and fields to serialize field or property as declared. For instance, if we have a String field in our Java class, the JSON value is enclosed within quotes (“ "). But when we annotate the field with @JsonRawValue annotation, Jackson library omits the quotes.


public @interface JsonRawValue

In the below example, the empAddress field is a JSON string. This JSON string serialized as a part of the final JSON string of Employee object.


import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
public class JsonRawValueAnnotationTest {
   public static void main(String args[]) throws JsonProcessingException {
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Employee());
// Employee class
class Employee {
   public int empId = 115;
   public String empName = "Sai Chaitanya";
   public String empAddress = "{\"doorNumber\": 1118, \"street\": \"IDPL Colony\", " + "\"city\":          \"Hyderabad\"}";


 "empId" : 115,
  "empName" : "Sai Chaitanya",
  "empAddress" : {"doorNumber": 1118, "street": "IDPL Colony", "city": "Hyderabad"}

Updated on: 08-Jul-2020


Kickstart Your Career

Get certified by completing the course

Get Started