Found 2620 Articles for Java

Importance of @JsonIdentityInfo annotation using Jackson in Java?

raja
Updated on 09-Jul-2020 05:13:16

844 Views

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate the object identity during the serialization and deserialization process. The ObjectIdGenerators.PropertyGenerator is an abstract place-holder class to denote a case where Object Identifier to use comes from a POJO property.Syntax@Target(value={ANNOTATION_TYPE, TYPE, FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonIdentityInfoExampleimport java.util.*; import java.io.*; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonIdentityInfoTest {    public static void main(String[] args) throws IOException {       ObjectMapper mapper = new ObjectMapper();       User user = new User(115, "Raja", "Ramesh");       ... Read More

How to convert a JSON array to array using JSON-lib API in Java?

raja
Updated on 08-Jul-2020 12:53:38

1K+ Views

The JSONArray is a sequence of values, the external text is a string enclosed in square brackets with commas separating the values and internal text is an object having get() and opt() methods, we need to access those values by index. The element() method for adding or replacing those values. An array is an object that stores multiple values of the same type. It can hold both primitive types and object references. We can convert a JSON array to array by using the toArray() method of JSONArray class. This method produces an Object[] with the contents of JSONArray.Syntaxpublic Object[] toArray()Exampleimport java.util.Arrays; import net.sf.json.JSONArray; public ... Read More

How can we use @Since annotation using Gson in Java?

raja
Updated on 08-Jul-2020 12:38:47

252 Views

The @Since 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 same can apply to the deserialization process.Syntax@Documented @Retention(value=RUNTIME) @Target(value={FIELD, TYPE}) public @interface SinceExampleimport com.google.gson.annotations.Since; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonSinceAnnotationTest {    public static void main(String[] args) {       Employee emp = new Employee();       emp.setEmployeeName("Raja Ramesh");       emp.setEmployeeId(125);       emp.setEmployeeTechnology("Java");       emp.setEmploeeAddress("Hyderabad");       System.out.println("Since version ... Read More

How to use @Until annotation using the Gson library in Java?

raja
Updated on 08-Jul-2020 12:01:11

203 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

Importance of @JsonRootName annotation using Jackson in Java?

raja
Updated on 08-Jul-2020 11:40:50

790 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

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

raja
Updated on 08-Jul-2020 11:38:24

923 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

How to convert the JSON object to a bean using JSON-lib API in Java?

raja
Updated on 08-Jul-2020 11:34:52

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

Importance of @JsonFilter annotation in Java?

raja
Updated on 08-Jul-2020 11:35:40

622 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

When to use @JsonManagedReference and @JsonBackReference annotations using Jackson in Java?

raja
Updated on 08-Jul-2020 11:36:22

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

Importance of @JsonView annotation using Jackson in Java?

raja
Updated on 08-Jul-2020 11:19:19

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

Advertisements