Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of @JsonUnwrapped annotation using Jackson in Java?
The @JsonUnwrapped annotation can be used to unwrap values during the serialization and deserialization process. It helps to render the values of a composed class as if it belongs to the parent class.
Syntax
@Target(value={ANNOTATION_TYPE,FIELD,METHOD,PARAMETER})
@Retention(value=RUNTIME)
public @interface JsonUnwrapped
Example
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
public class JsonUnwrappedAnnotationTest {
public static void main(String args[]) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Employee());
System.out.println(jsonString);
}
}
class Employee {
public int empId = 110;
public String empName = "Raja Ramesh";
@JsonUnwrapped
public Address address = new Address();
// Address class
public static class Address {
public String doorNumber = "1118";
public String street = "madhapur";
public String pinCode = "500081";
public String city = "Hyderabad";
}
}
Output
{
"empId" : 110,
"empName" : "Raja Ramesh",
"doorNumber" : "1118",
"street" : "madhapur",
"pinCode" : "500081",
"city" : "Hyderabad"
}Advertisements