JSON Articles

Page 10 of 16

How to control serialization through @JSON annotation using flexjson in Java?\\n

raja
raja
Updated on 09-Jul-2020 340 Views

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 JSONExampleimport 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 ...

Read More

How to implement custom serializer using @JsonSerialize annotation in Java?

raja
raja
Updated on 09-Jul-2020 2K+ Views

The @JsonSerialize annotation is used to declare custom serializer during the serialization of a field. We can implement a custom serializer by extending the StdSeralizer class. and need to override the serialize() method of StdSerializer class.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD, TYPE, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonSerializeIn the below program, we can implement a custom serializer using @JsonSerialize annotationExampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.*; import com.fasterxml.jackson.databind.ser.std.*; public class JsonSerializeAnnotationTest {    public static void main (String[] args) throws JsonProcessingException, IOException {       Employee emp = new Employee(115, "Adithya", new String[] {"Java", "Python", "Scala"});       ObjectMapper mapper = new ...

Read More

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

raja
raja
Updated on 09-Jul-2020 2K+ Views

The JSON-lib API is a java library to serialize and de-serialize java beans, maps, arrays, and collections in the JSON format. We need to convert a JSON string to a bean by converting a string to JSON object first then convert this to a java bean.Syntaxpublic static Object toBean(JSONObject jsonObject, Class beanClass)In the below program, we can convert a JSON string to a bean.Exampleimport net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class ConvertJSONStringToBeanTest {    public static void main(String[] args) {       String jsonStr = "{\"firstName\": \"Adithya\", \"lastName\": \"Sai\", \"age\": 30, \"technology\": \"Java\"}";       JSONObject jsonObj = (JSONObject)JSONSerializer.toJSON(jsonStr); // convert String ...

Read More

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

raja
raja
Updated on 08-Jul-2020 492 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 convert a bean to XML without type hints using JSON-lib API in Java?

raja
raja
Updated on 08-Jul-2020 344 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
raja
Updated on 08-Jul-2020 415 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 bean to JSON object by excluding some properties using JsonConfig in Java?

raja
raja
Updated on 08-Jul-2020 1K+ 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[] excludes)In the below example, we can convert bean to a JSON object by excluding some of the properties.Exampleimport net.sf.json.JSONObject; import net.sf.json.JsonConfig; public class BeanToJsonExcludeTest {    public static void main(String[] args) {       Student student = new Student("Raja", "Ramesh", 35, "Madhapur");       JsonConfig jsonConfig = new ...

Read More

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

raja
raja
Updated on 08-Jul-2020 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 package in our java program to execute it.Syntaxpublic JSONObject element(String key, Object value) - put a key/value pair in the JSONObject Exampleimport java.util.Arrays; import net.sf.json.JSONObject; public class JsonAddElementTest {    public static void main(String[] args) {       JSONObject jsonObj = new JSONObject()          .element("name", "Raja ...

Read More

How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?

raja
raja
Updated on 08-Jul-2020 768 Views

The ExclusionStrategy interface can be used to exclude any field during serialization and deserialization. We can provide a custom implementation of the ExclusionStrategy interface and need to register it with GsonBuilder using the setExclusionStrategies() method. It configures Gson to apply a set of exclusion strategies during serialization and deserialization.Syntaxpublic GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies)Exampleimport com.google.gson.*; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; public class ExclusionStrategyTest {    public static void main(String args[]) throws Exception {       Gson gson = new GsonBuilder().setExclusionStrategies(new CustomExclusionStrategy()).create();          Person person = new Person();          person.setFirstName("Adithya");          person.setLastName("Sai");          person.setAddress("Hyderabad");     ...

Read More

How can we update an existing JSON data using javax.json API in Java?

raja
raja
Updated on 08-Jul-2020 8K+ Views

The JsonBuilderFactory interface is a factory to create JsonObjectBuilder instance and JsonObjectBuilder is a builder for creating JsonObject models from scratch. This interface initializes an empty JSON object model and provides methods to add name/value pairs to the object model and to return the resulting object. We can create a JsonObjectBuilder instance that can be used to build JsonObject using the createObjectBuilder() method.SyntaxJsonObjectBuilder createObjectBuilder()In the below example, We can update an existing JSON data with newly added data.Exampleimport java.io.*; import javax.json.*; public class UpdateExistingJsonTest {    public static void main(String[] args) throws Exception {       String jsonString = "{\"id\":\"115\", \"name\":\"Raja\", \"address\":[{\"area\":\"Madhapur\", \"city\":\"Hyderabad\"}]}";       ...

Read More
Showing 91–100 of 152 articles
« Prev 1 8 9 10 11 12 16 Next »
Advertisements