- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2616 Articles for Java
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
227 Views
The @Until annotation can use with the setVersion() method of the GsonBuilder class. This annotation can apply to a field in java class and accepts float as an argument. This argument represents the version number in which the field has serialized. The @Until annotation can manage the versioning of JSON classes in web-services.Syntax@Documented @Retention(value=RUNTIME) @Target(value={FIELD, TYPE}) public @interface UntilExampleimport com.google.gson.annotations.Until; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonUntilAnnotationTest { public static void main(String[] args) { Employee emp = new Employee(); emp.setEmployeeName("Adithya"); emp.setEmployeeId(115); emp.setEmployeeTechnology("Python"); emp.setEmploeeAddress("Pune"); ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
911 Views
The @JsonRootName annotation can be used to wrap an object to serialize with a top-level element. We can pass the name as a parameter to @JsonRootName annotation. We can use the "WRAP_ROOT_VALUE" feature of SerializationFeature enum that can be enabled to make root value wrapped within a single property JSON object where the key is a root name.Exampleimport com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.SerializationFeature; public class JsonRootNameAnnotationTest { public static void main(String args[]) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.enable(SerializationFeature.WRAP_ROOT_VALUE).writeValueAsString(new Employee()); System.out.println(jsonString); } } @JsonRootName(value ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
1K+ Views
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.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD}) @Retention(value=RUNTIME) public @interface JsonRawValueIn 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.Exampleimport 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 { ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
3K+ Views
The JSONObject class is a collection of name/value pairs (unordered) where the bean is a class with setter and getter methods for its member fields. We can convert a JSON object to a bean using the toBean() method of JSONObject class.Syntaxpublic static Object toBean(JSONObject jsonObject, Class beanClass)Exampleimport net.sf.json.JSONObject; public class ConvertJSONObjToBeanTest { public static void main(String[] args) { mployee emp = new Employee("Sai", "Ram", 30, "Bangalore"); JSONObject jsonObj = JSONObject.fromObject(emp); System.out.println(jsonObj.toString(3)); // pretty print JSON emp = (Employee)JSONObject.toBean(jsonObj, Employee.class); System.out.println(emp.toString()); } // Employee class public static ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
712 Views
The @JsonFilter annotation used to define a custom filter to serialize the Java objects. We need to use the FilterProvider class to define a filter and get the actual filter instance. Now the filter configured by assigning the FilterProvider to ObjectMapper class.Syntax@Target(value={ANNOTATION_TYPE, TYPE, METHOD, FIELD, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonFilterIn the below example, the customFilter can be declared as an argument to @JsonFilter annotation extracts only the name and filtering out the other properties of a bean.Exampleimport com.fasterxml.jackson.annotation.JsonFilter; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; public class JsonFilterAnnotationTest { public static void main(String args[]) throws JsonProcessingException { ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
4K+ Views
The @JsonManagedReference and @JsonBackReference annotations can be used to create a JSON structure in a bidirectional way. The @JsonManagedReference annotation is a forward reference that includes during the serialization process whereas @JsonBackReference annotation is a backreference that omits during the serialization process.In the below example, we can implement @JsonManagedReference and @JsonBackReference annotations.Exampleimport java.util.*; import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class ManagedReferenceBackReferenceTest { public static void main(String args[]) throws JsonProcessingException { BackReferenceBeanTest testBean = new BackReferenceBeanTest(110, "Sai Chaitanya"); ManagedReferenceBeanTest bean = new ManagedReferenceBeanTest(135, "Adithya Ram", testBean); testBean.addEmployees(bean); ObjectMapper mapper = ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
1K+ Views
The JsonView annotation can be used to include/exclude a property during the serialization and deserialization process dynamically. We need to configure an ObjectMapper class to include the type of view used for writing a JSON from Java object using the writerWithView() method.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD}) @Retention(value=RUNTIME) public @interface JsonViewExampleimport com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonView; import com.fasterxml.jackson.core.JsonProcessingException; public class JsonViewAnnotationTest { public static void main(String args[]) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writerWithView(Views.Public.class).writeValueAsString(new Person()); String jsonStringInternal = objectMapper.writerWithView(Views.Internal.class).writeValueAsString(new Person()); System.out.println(jsonString); System.out.println(jsonStringInternal); } } ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
374 Views
The JsonConfig class can be used to configure the serialization process. We can use the setJsonPropertyFilter() method of JsonConfig to set the property filter when serializing to JSON. We need to implement a custom PropertyFilter class by overriding the apply() method of the PropertyFilter interface. It returns true if the property will be filtered out or false otherwise.Syntaxpublic void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter)Exampleimport net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.PropertyFilter; public class ConvertBeanToJsonExcludeFilterTest { public static void main(String[] args) { Student student = new Student("Sai", "Chaitanya", 20, "Hyderabad"); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new CustomPropertyFilter()); ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
200 Views
The JSON-lib is a Java library for serializing and de-serializing java beans, maps, arrays, and collections in JSON format. We can convert a bean to XML without type hints using the setTypeHintsEnabled() method of XMLSerializer class, this method sets whether JSON types can be included as attributes. We can pass false as an argument to this method to disable the type hints in XML.Syntaxpublic void setTypeHintsEnabled(boolean typeHintsEnabled)Exampleimport net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLNoHintsTest { public static void main(String[] args) { Employee emp = new Employee("Krishna Vamsi", 115, 30, "Java"); JSONObject jsonObj = JSONObject.fromObject(emp); ... Read More
![raja](https://www.tutorialspoint.com/assets/profiles/107595/profile/60_161286-1564642329.jpg)
232 Views
The net.sf.json.xml.XMLSerializer class is a utility class for transforming JSON to XML. When transforming JSONObject instance to XML, this class can add hints for converting back to JSON. We can use the write() method of XMLSerializer class to write a JSON value into an XML string with UTF-8 encoding and it can return a string representation of a well-formed XML document.Syntaxpublic String write(JSON json)Exampleimport net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLTest { public static void main(String[] args) { Student student = new Student("Sai", "Adithya", 25, "Pune"); JSONObject jsonObj = JSONObject.fromObject(student); System.out.println(jsonObj.toString(3)); //pretty print JSON ... Read More