Java Articles - Page 214 of 745

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

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

360 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?

Manisha Chand
Updated on 20-May-2025 14:31:14

1K+ Views

In Java, Jackson is a library that is used to convert JSON to Java objects and vice versa. Jackson Annotations are used during serialization and deserialization. We use annotations with a particular field or method that is declared in Java.  So, Annotations make the JSON output clearer as we required. In this Article, we will learn about one of its annotations @JsonRootName. @JsonRootName Annotatation The @JsonRootName annotation is a Jackson annotation that is used on a class if we want to do wrapping in our JSON attributes. It is a class-level annotation. It wraps the object to be serialized with a ... Read More

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

Manisha Chand
Updated on 20-May-2025 14:39:57

2K+ Views

In Java, Jackson is a library that is used to convert JSON to Java objects and vice versa. Jackson Annotations are used during serialization and deserialization to denote a particular field that is declared in Java as an instance variable, is a JsonProperty, and should be ignored. We can also use annotations with methods. So, basically, Annotations make JSON output clearer as we required. In this Article, we will learn about one of its annotations is @JsonRawValue Annotation. @JsonRawValue This annotation can be used for methods and fields. The @JsonRawValue annotation is used to serialize methods or fields as it ... 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

4K+ 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?

Manisha Chand
Updated on 20-May-2025 19:02:42

1K+ Views

The @JsonFilter annotation belongs to Jackson Annotations. Jackson Annotations are used during serialization and deserialization to denote a particular field (or method) that is declared in Java as an instance variable, is a JsonProperty, and should be ignored, or what condition should be applied to it. It is used to define a dynamic filter for serializing/deserializing Java objects. @jsonfilter annotation Let's discuss the importance of jsonfilter annotation as given below - It is used to filter which fields or properties of an object should be included ... Read More

Importance of @JsonView annotation using Jackson in Java?

Manisha Chand
Updated on 05-Jun-2025 10:58:46

1K+ Views

Jackson Library: The @JsonView AnnotationIn Java, Jackson is a library that is used to convert JSON to Java objects and vice versa. Jackson Annotations are used during serialization and deserialization. We use these to denote or specify annotations before a particular field or method (that is declared in Java). Using an annotation before a field, we can denote whether a variable is a JsonProperty, should be ignored, or what condition should be applied to it. The JsonView annotation is used to include/exclude ... Read More

How to convert a bean to JSON object using Exclude Filter in Java?

raja
Updated on 08-Jul-2020 11:20:06

492 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

How to convert a bean to XML without type hints using JSON-lib API in Java?

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

306 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

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

raja
Updated on 08-Jul-2020 11:16:03

363 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

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

raja
Updated on 19-Feb-2020 07:46:28

539 Views

A Java array is an object which stores multiple variables of the same type, it can hold primitive types and object references whereas JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values, an internal form is an object having get() and opt() methods for accessing the values by index and element() method for adding or replacing values. In the first step, we can create an Object[] array and pass this parameter as an argument to the toJSON() of JSONSerializer class and typecasting it to get the JSON array.We can convert Object[] array to JSONArray in the below exampleExampleimport ... Read More

Advertisements