- Given the user defined object or POJO having date field, we would like to serialize the POJO to JSON.
- During serialization, we would like save date as a timestamp or ISO format.
- We will use the Jackson’s ObjectMapper to achieve the conversion.
- We will create Person class and we will perform the following operations with Person class.
- Convert Person Object to JSON
- Serialize date as time stamp (default serialization)
- Convert Person Object to JSON
- Serialize date parameter as ISO format
- Convert the JSON to person object
- DeSerialize date as timestamp
- Convert Person Object to JSON
Jackson ObjectMapper Maven Dependencies
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.1</version> </dependency>
Program – Convert Date to/from timestamp in java (POJO <->JSON)
1.) Person Class:
- The person class containing date data member.
- We have overloaded toString method to display content of person object.
package org.learn;
import java.util.Date;
public class Person {
public String firstName;
public String lastName;
public Date dob;
public Person() {
}
public Person(String firstName, String lastName,
Date dob) {
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}
public String toString() {
return "[" + firstName + " " + lastName +
" " + dob.getTime() +"]";
}
}
2.) JSONObjectConverter Class:
We are performing following operation in JSONObjectConverter class.
- Convert Person Object to JSON
- Serialize date as time stamp (default serialization)
- Convert Person Object to JSON
- Serialize date parameter as ISO format
- Convert the JSON to person object
- DeSerialize date as timestamp
package org.learn;
import java.io.IOException;
import java.util.Date;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JSONObjectConverter
{
public static void main( String[] args ) throws IOException
{
ObjectMapper objectMapper = new ObjectMapper();
//Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
Person objPerson = new Person("Mike", "harvey", new Date());
//Convert Person object to json
String json = objectMapper.writeValueAsString(objPerson);
System.out.println("1. Convert Person to JSON - Date as timestamp");
System.out.println(json);
//Disable the timestamp serialization
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = objectMapper.writeValueAsString(objPerson);
System.out.println("2. Convert Person to JSON - Date without timestamp");
System.out.println(json);
//Convert to json to person object
System.out.println("3. Deserialize JSON to person object");
Person objFromJson = objectMapper.readValue(json, Person.class);
System.out.println(objFromJson);
}
}
Download Example Code-Jackson Object (Date) to JSON
Output – Date serialization & deserialization in java (POJO <-> JSON)
1. Convert Person to JSON - Date as timestamp
{
"firstName" : "Mike",
"lastName" : "harvey",
"dob" : 1329142874258
}
2. Convert Person to JSON - Date without timestamp
{
"firstName" : "Mike",
"lastName" : "harvey",
"dob" : "2012-02-13T14:21:14.258+0000"
}
3. Deserialize JSON to person object
[Mike harvey 1329142874258]