In my previous post “FieldNamingPolicy Example”, I explained with an example how to change the naming convention using out of the box provided standard naming conventions. In this post, I will explain with an example how to create your own custom naming conventions and configure the Gson to use it. To create our own custom…… Continue reading FieldNamingStrategy Example
FieldNamingPolicy Example
In this post under Gson, I will show how to change the naming convention of field names when serialized to JSON format. I will show how to change the naming convention from out of the box provided standard naming conventions. Gson out of the box provides the below naming conventions as part of the enum…… Continue reading FieldNamingPolicy Example
Configuring Gson to serialize objects with fields having null value
Whenever we serialize a java object to JSON using Gson framework, by default fields with null values are ignored. As shown in the below code Main code import com.google.gson.Gson; public class GsonDemo7 { public static void main(String[] args) { Student student = new Student(); student.setId(1); student.setName(“name1”); student.setRollnum(100); student.setDate(null); Gson gson = new Gson(); String result…… Continue reading Configuring Gson to serialize objects with fields having null value
Using Custom DateFormat
In this post, under GSON, I will explain how to use custom date format when serializing Java object containing date information to json format. We can change this format globally, globally meaning the custom format will be applied to all objects that are serialized using Gson instance, created using the custom date format. Below is…… Continue reading Using Custom DateFormat
Configuring Gson object
In this post under GSON, I will explain how to configure the Gson instance. In all my previous post under GSON, I used to create a Gson instance by using the below code snippet. Gson gson = new Gson(); With this instance we used convert java object to and from JSON format. This instance comes…… Continue reading Configuring Gson object
Writing JSON Data using JsonWriter
This post explains how to write json data to a file using JsonWriter. With JsonWriter we dont need to build the entire object in memory. The code explains how to write the below json data [ { “id”: 1, “text”: “text1”, “array”: null }, { “id”: 2, “text”: “text2!”, “array”: [ 50.454722, -104.606667 ] }…… Continue reading Writing JSON Data using JsonWriter
Deserializing JSON to Java Object
In this post under Gson. I will show how to deserialize a JSON data back to Java object using Gson framework. For our example I will use the below json data. The json data contains employee information. employee.json {“id”:1,”name”:”employee1″,”ssn”:1234} The class structure of Employee is as shown below Employee class public class Employee { private…… Continue reading Deserializing JSON to Java Object
Chaining multiple JobParametersValidator implementations
In this post under Spring Batch, I will show how to chain multiple JobParametersValidator interface implementations. For our example I will create two JobParametersValidator interface implementations as shown below Validator1 package xml.package21; import java.util.Map; import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersInvalidException; import org.springframework.batch.core.JobParametersValidator; public class Validator1 implements JobParametersValidator { @Override public void validate(JobParameters jobParameters) throws JobParametersInvalidException…… Continue reading Chaining multiple JobParametersValidator implementations
Accessing cache provider’s Cache implementation
This post explains how to access the cache provider’s Cache implementation through java cache api. In all my previous post related to Caching, I gave examples where I was accessing cache provider for caching functionality through the Java Cache API. The java cache api is set of specifications, which are implemented by cache providers and…… Continue reading Accessing cache provider’s Cache implementation
Parsing JSON data in a streaming way
In this post, I will explain how to parse JSON data using Gson framework. Each element whether start and end of the object, start and end of array, field name and field values are considered as tokens and are represented by JsonToken enums. The Gson framework parses the json data as a stream of tokens.…… Continue reading Parsing JSON data in a streaming way