Found 206 Articles for JSON

Pretty print JSON using the flexjson library in Java?

raja
Updated on 06-Jul-2020 05:53:33

218 Views

The Flexjson is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can pretty-print JSON using the prettyPrint(boolean prettyPrint) method of JSONSerializer class.Syntaxpublic JSONSerializer prettyPrint(boolean prettyPrint)In the below program, Pretty print JSON using flexjson libraryExampleimport flexjson.*; public class PrettyPrintJSONTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print       Employee emp = new Employee("Vamsi", "105", "Python Developer", "Python", "Pune");       String jsonStr = ... Read More

How to deserialize a JSON to Java object using the flexjson in Java?

raja
Updated on 04-Jul-2020 13:01:23

4K+ Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format allowing both deep and shallow copies of objects. In order to run a Java program with flexjon, we need to import a flexjson package. We can deserialize a JSON to Java object using the deserialize() method of JSONDeserializer class, it takes as input a json string and produces a static typed object graph from that json representation. By default, it uses the class property in the json data in order to map the untyped generic json data into a specific Java type.Syntaxpublic T deserialize(String input)In the below program, deserialize ... Read More

Custom instance creator using Gson in Java?

raja
Updated on 04-Jul-2020 12:48:43

1K+ Views

While parsing JSON String to or from Java object, By default Gson try to create an instance of Java class by calling the default constructor. In the case of Java class doesn’t contain default constructor or we want to do some initial configuration while creating Java objects, we need to create and register our own instance creator.We can create a custom instance creator in Gson using the InstanceCreator interface and need to implement the createInstance(Type type) method.SyntaxT createInstance(Type type)Exampleimport java.lang.reflect.Type; import com.google.gson.*; public class CustomInstanceCreatorTest {    public static void main(String args[]) {       GsonBuilder gsonBuilder = new GsonBuilder();   ... Read More

How to serialize a null field using Gson library in Java?

raja
Updated on 04-Jul-2020 12:34:19

6K+ Views

By default, the Gson object does not serialize the fields with null values to JSON. If a field in a Java object is null, Gson excludes it. We can force Gson to serialize null values via the GsonBuilder class. We need to call the serializeNulls() method on the GsonBuilder instance before creating the Gson object. Once serializeNulls() has been called the Gson instance created by the GsonBuilder can include null fields in the serialized JSON.Syntaxpublic GsonBuilder serializeNulls()Exampleimport com.google.gson.*; import com.google.gson.annotations.*; public class NullFieldTest {    public static void main(String args[]) {       GsonBuilder builder = new GsonBuilder();       builder.serializeNulls();       ... Read More

How to configure Gson to enable versioning support in Java?

raja
Updated on 04-Jul-2020 12:20:30

93 Views

The Gson library provides a simple versioning system for the Java objects that it reads and writes and also provides an annotation named @Since for the versioning concept @Since(versionnumber).We can create a Gson instance with versioning using the GsonBuilder().setVersion() method. If we mentioned like setVersion(2.0),  means that all the fields having 2.0 or less are eligible to parse.Syntaxpublic GsonBuilder setVersion(double ignoreVersionsAfter)Exampleimport com.google.gson.*; import com.google.gson.annotations.*; public class VersionSupportTest {    public static void main(String[] args) {       Person person = new Person();       person.firstName = "Raja";       person.lastName = "Ramesh";       Gson gson1 = new GsonBuilder().setVersion(1.0).setPrettyPrinting().create();   ... Read More

How to format a date using the Gson library in Java?

raja
Updated on 04-Jul-2020 12:13:14

5K+ Views

A Gson is a JSON library for Java, which is created by Google. By using Gson, we can generate JSON and convert JSON to java objects. We can create a Gson instance by creating a GsonBuilder instance and calling with the create() method. The GsonBuilder().setDateFormat() method configures Gson to serialize Date objects according to the pattern provided.Syntaxpublic GsonBuilder setDateFormat(java.lang.String pattern)Exampleimport java.util.Date; import com.google.gson.*; public class DateformatTest {    public static void main(String[] args) {       Employee emp = new Employee(115, "Surya", new Date(), 25000.00);       Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();       String result = gson.toJson(emp);       System.out.println(result);   ... Read More

Pretty print JSON using org.json library in Java?

raja
Updated on 04-Jul-2020 11:44:53

9K+ Views

The JSON is a lightweight, text-based and language-independent data exchange format. A.JSONObject can parse text from a string to produce a map-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant object serialization. The files in the org.json package implement JSON encoders/decoders in Java. It also includes the capability to convert between JSON,  XML, HTTP  headers, Cookies, and CDL.We can pretty-print a JSON using the toString(int indentFactor) method of org.json.JSONObject class,   where indentFactor is the number of spaces to add to each level of indentation.Syntaxpublic java.lang.String toString(int indentFactor) throws JSONExceptionExampleimport org.json.*; public class JSONPrettyPrintTest {    public static void main(String args[]) throws JSONException ... Read More

How to access the JSON fields, arrays and nested objects of JsonNode in Java?

raja
Updated on 25-Oct-2023 14:06:16

24K+ Views

A JsonNode is Jackson's tree model for JSON and it can read JSON into a JsonNode instance and write a JsonNode out to JSON. To read JSON into a JsonNode with Jackson by creating ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of JsonNode class. We can return a valid string representation using the asText() method and convert the value of the node to a Java int using the asInt() method of JsonNode class.In the below example, we can access JSON fields, arrays and nested objects of JsonNode.Exampleimport com.fasterxml.jackson.databind.*; import java.io.*; public class ... Read More

How to implement custom JSON de-serialization with Gson in Java?

raja
Updated on 13-Feb-2020 10:11:34

497 Views

A Gson library provides a way to specify custom de-serializers by registering a custom de-serializer with the GsonBuilder if we need a way to convert a java object to JSON. We can create a custom de-serializer by overriding the deserialize() method of com.google.gson.JsonDeserializer class.In the below example, the implementation of custom de-serialization of JSON.Exampleimport java.lang.reflect.Type; import com.google.gson.*; public class CustomJSONDeSerializerTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().registerTypeAdapter(Password.class, new          PasswordDeserializer()).setPrettyPrinting().create();       String jsonStr = "{" +                           "\"firstName\": ... Read More

Importance of a JSONTokener in Java?

raja
Updated on 13-Feb-2020 10:12:30

2K+ Views

The JSONTokener class allows an application to break a string into tokens. It can be used by the JSONObject and JSONArray constructors to parse JSON source strings. A few important methods of JSONTokener class are back() - moves cursor one position back, more() - returns true if the token has element or else returns false,  next() - returns a character next to current position and nextTo(character) - returns a string until the given character matches.Syntaxpublic class JSONTokener extends java.lang.ObjectExampleimport java.io.*; import org.json.*; public class JSONTokenerTest {    public static void main(String args[]) throws JSONException, Exception {       String jsonStr = "{" + " \"Technology\": ... Read More

Advertisements