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

Serializing Java objects to JSON

In this post under Gson. I will show how to serialize a java object to JSON using Gson framework. Below is the complete code. Main Code 1 import java.io.File; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 5 import com.google.gson.Gson; 6 7 public class GsonDemo1 { 8 public static void main(String[] args) { 9 Employee employee…… Continue reading Serializing Java objects to JSON

JsonRecordSeparatorPolicy example

In this post under Spring Batch, I will explain the purpose and how to use JsonRecordSeparatorPolicy class with an example. JsonRecordSeparatorPolicy class is a concrete implementation of org.springframework.batch.item.file.separator.RecordSeparatorPolicy interface. In one of my previous post I have explained the purpose of RecordSeparatorPolicy and how to use it. For recap, RecordSeparatorPolicy interface is used to tell…… Continue reading JsonRecordSeparatorPolicy example

JsonLineMapper Example

In this post under Spring Batch, I will explain the purpose of JsonLineMapper with an example. JsonLineMapper is used with reader bean for reading file containing JSON input as shown below JsonFileInput.txt {“id”:”id0″,”name”:”name0″,”status”:”status0″,”salary”:0}{“id”:”id1″,”name”:”name1″,”status”:”status1″,”salary”:1}{“id”:”id2″,”name”:”name2″,”status”:”status2″,”salary”:2}{“id”:”id3″,”name”:”name3″,”status”:”status3″,”salary”:3}{“id”:”id4″,”name”:”name4″,”status”:”status4″,”salary”:4}{“id”:”id5″,”name”:”name5″,”status”:”status5″,”salary”:5}{“id”:”id6″,”name”:”name6″,”status”:”status6″,”salary”:6}{“id”:”id7″,”name”:”name7″,”status”:”status7″,”salary”:7}{“id”:”id8″,”name”:”name8″,”status”:”status8″,”salary”:8}{“id”:”id9″,”name”:”name9″,”status”:”status9″,”salary”:9} Each line in the file is interpreted as a JSON object by JsonLineMapper. So each line must start with “{” and end…… Continue reading JsonLineMapper Example

Using JsonbCreator annotation

In this post under JSONB, I will explain the purpose of JsonbCreator annotation with an example. Whenever we deserialize a json data to java object, it is required that the Class of the object should have default/no argument constructor, so that JSONB runtime can create an instance of the class with the help of the…… Continue reading Using JsonbCreator annotation

Using Custom DateFormat

In this post, under JSONB, I will explain how to use custom date format when serializing/deserializing Java object containing date information to json format. By default JSONB when converting date information to json it prints in the format as shown below 2019-02-16T07:16:38.537Z[UTC] We can change this format globally, globally meaning the custom format will be…… Continue reading Using Custom DateFormat

Custom format for Property names using PropertyNamingStrategy Part 1

In this post under JSONB, I will explain how to create custom format for property names using PropertyNamingStrategy interface. Whenever we serialize a java object to JSON format, the property or field names in json are same as property names in its corresponding java class. For example when we serialize the below java class instance…… Continue reading Custom format for Property names using PropertyNamingStrategy Part 1

Serialization of null values when converting from java object to json format

In this post I will explain how serialization of null values feature can be turned on or off. Below is the class structure of the object to be serialized Student 1 import javax.json.bind.annotation.JsonbProperty; 2 3 public class Student { 4 private int id; 5 private String name; 6 private Integer rollnum; 7 8 public int…… Continue reading Serialization of null values when converting from java object to json format