Raja has Published 760 Articles

Importance of @JsonFilter annotation in Java?

raja

raja

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

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, ... Read More

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

raja

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 ... Read More

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

raja

raja

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

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 ... Read More

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

raja

raja

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

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 ... Read More

Importance of @JsonView annotation using Jackson in Java?

raja

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; ... Read More

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

raja

raja

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

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 ... Read More

How to convert bean to JSON object by excluding some properties using JsonConfig in Java?

raja

raja

Updated on 08-Jul-2020 09:05:50

945 Views

The JsonConfig class is a utility class that helps to configure the serialization process. We can convert a bean to a JSON object with few properties that can be excluded using the setExcludes() method of JsonConfig class and pass this JSON config instance to an argument of static method fromObject() of JSONObject.Syntaxpublic void setExcludes(String[] ... Read More

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

raja

raja

Updated on 08-Jul-2020 08:20:25

5K+ Views

A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value. We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format. Initially, we can create ... Read More

How to add elements to JSON Object using JSON-lib API in Java?

raja

raja

Updated on 08-Jul-2020 07:31:03

3K+ Views

The JSON-lib is a Java library for serializing and de-serializing java beans, maps, arrays, and collections in JSON format. We can add elements to the JSON object using the element() method of JSONObject class. We need to download all the dependent jars like json-lib.jar, ezmorph.jar, commons-lang.jar, commons-collections.jar, commons-beanutils.jar, and commons-logging.jar and can import net.sf.json ... Read More

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

raja

raja

Updated on 08-Jul-2020 07:30:31

690 Views

The net.sf.json.JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values and 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. The ... Read More

Advertisements