
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
When to use @JsonAutoDetect annotation in Java?
The @JsonAutoDetect annotation can be used at the class level to override the visibility of the properties of a class during serialization and deserialization. We can set the visibility with the properties like "creatorVisibility", "fieldVisibility", "getterVisibility", "setterVisibility" and "isGetterVisibility". The JsonAutoDetect class can define public static constants that are similar to Java class visibility levels like "ANY", "DEFAULT", "NON_PRIVATE", "NONE", "PROTECTED_AND_PRIVATE" and "PUBLIC_ONLY".
Example
import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JsonAutoDetectTest { public static void main(String[] args) throws IOException { Address address = new Address("Madhapur", "Hyderabad", "Telangana"); Name name = new Name("Raja", "Ramesh"); Student student = new Student(address, name, true); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student); System.out.println("JSON: " + jsonString); } } // Address class class Address { private String firstLine; private String secondLine; private String thirdLine; public Address(String firstLine, String secondLine, String thirdLine) { this.firstLine = firstLine; this.secondLine = secondLine; this.thirdLine = thirdLine; } public String getFirstLine() { return firstLine; } public String getSecondLine() { return secondLine; } public String getThirdLine() { return thirdLine; } } // Name class class Name { private String firstName; private String secondName; public Name(String firstName, String secondName) { this.firstName = firstName; this.secondName = secondName; } public String getFirstName() { return firstName; } public String getSecondName() { return secondName; } } // Student class @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) class Student { private Address address; private Name name; private Boolean isActive; public Student(Address address, Name name, Boolean isActive) { this.address = address; this.name = name; this.isActive = isActive; } }
Output
{ "address" : { "firstLine" : "Madhapur", "secondLine" : "Hyderabad", "thirdLine" : "Telangana" }, "name" : { "firstName" : "Raja", "secondName" : "Ramesh" }, "isActive" : true }
- Related Questions & Answers
- When to use @JsonValue annotation using Jackson in Java?
- When to use @ConstructorProperties annotation with Jackson in Java?
- What to use @SerializedName annotation using Gson in Java?
- How to use @Until annotation using the Gson library in Java?
- When to use an abstract class and when to use an interface in Java?
- When to use static methods in Java?
- When to use vararg methods in Java?
- When to use fillInStackTrace() method in Java?
- How can we use @Since annotation using Gson in Java?
- What is the use of @JacksonInject annotation using Jackson in Java?
- When to use LinkedList over ArrayList in Java
- What is the use of @JsonRawValue annotation using Jackson API in Java?
- Importance of @Override annotation in Java?
- Importance of @JsonFilter annotation in Java?
- When can we use Synchronized blocks in Java?
Advertisements