

- 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
How to control serialization through @JSON annotation using flexjson in Java?
The @JSON annotation is used by JSONSerializer class to exclude or include a field during the serialization process. We can use the serialize() method of JSONSerializer class to perform a shallow serialization of the target instance.
Syntax
@Retention(value=RUNTIME) @Target(value={FIELD,TYPE,METHOD}) public @interface JSON
Example
import flexjson.JSONSerializer; import flexjson.JSON; public class JSONAnnotationTest { public static void main(String[] args) { JSONSerializer serializer = new JSONSerializer().prettyPrint(true); Employee emp = new Employee("Raja", "Ramesh", 30, "Hyderabad"); String jsonStr = serializer.serialize(emp); System.out.println(jsonStr); } } // Employee class class Employee { private String firstName, lastName, address; private int age; public Employee(String firstName, String lastName, int age, String address) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; this.address = address; } public String getFirstName() { return firstName; } @JSON(include=false) public String getLastName() { return lastName; } public int getAge() { return age; } @JSON(include=false) public String getAddress() { return address; } }
Output
{ "age": 30, "class": "Employee", "firstName": "Raja" }
- Related Questions & Answers
- How to wrap a JSON using flexjson in Java?
- How to deserialize a JSON to Java object using the flexjson in Java?
- Pretty print JSON using the flexjson library in Java?
- How to deserialize a JSON string using @JsonCreator annotation in Java?
- How to exclude a field from JSON using @Expose annotation in Java?
- How to implement custom JSON serialization with Gson in Java?
- How to implement custom JSON de-serialization with Gson in Java?
- How can we ignore the fields during JSON serialization in Java?
- How to serialize a map using the flexjson library in Java?
- How to deserialize a Java object from Reader Stream using flexjson in Java?
- How to implement custom serializer using @JsonSerialize annotation in Java?
- How to implement custom deserializer using @JsonDeserialize annotation in Java?
- How to use @Until annotation using the Gson library in Java?
- How can we serialize a list of objects using flexjson in Java?
- How can we serialize an array of objects using flexjson in Java?
Advertisements