DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Jackson vs Gson: Edge Cases in JSON Parsing for Java Apps
  • Aggregating REST APIs Calls Using Apache Camel
  • The Configure() Method in Jackson in JSON
  • Securing Spring Boot Microservices with JSON Web Tokens (JWT)

Trending

  • Advancing Robot Vision and Control
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT
  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Serialize Java.util.Date with Jackson JSON Processor / Spring 3.0

How to Serialize Java.util.Date with Jackson JSON Processor / Spring 3.0

Learn how to serialize a java.util.Date object with Jackson JSON Processor – Spring MVC 3.

By 
Loiane Groner user avatar
Loiane Groner
·
Sep. 20, 10 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
277.2K Views

Join the DZone community and get the full member experience.

Join For Free

Quick tip: how to serialize a java.util.Date object with Jackson JSON Processor – Spring MVC 3.

package com.loiane.model;

import java.util.Date;

import org.codehaus.jackson.annotate.JsonAutoDetect;

@JsonAutoDetect
@Entity
public class Company {

private int id;
private double price;
private String company;
private Date date;
private String size;
private byte visible;
}

And I have the following method in my controller:

public @ResponseBody Map<String,List<Company>> view() throws Exception

Which returns a list of Company.

The @ResponseBody annotation instructs Spring MVC to serialize the hashmap to the client. Spring MVC automatically serializes to JSON because the client accepts that content type:

{"total":27,"data":[{"price":71.72,"company":"3m Co","visible":1,"id":1,"size":"large","date":1188615600000},{"price":29.01,"company":"Aloca
Inc","visible":0,"id":2,"size":"medium","date":1185937200000},{"price":83.81,"company":"Altria Group
Inc","visible":0,"id":3,"size":"large","date":1186110000000},{"price":52.55,"company":"American Express Company","visible":1,"id":4,"size":"extra
large","date":1199412000000},{"price":64.13,"company":"American International Group
Inc.","visible":1,"id":5,"size":"small","date":1204599600000},{"price":31.61,"company":"AT&T Inc.","visible":0,"id":6,"size":"extra
large","date":1201831200000},{"price":75.43,"company":"Boeing Co.","visible":1,"id":7,"size":"large","date":1199152800000},{"price":67.27,"company":"Caterpillar
Inc.","visible":1,"id":8,"size":"medium","date":1196647200000},{"price":49.37,"company":"Citigroup,
Inc.","visible":1,"id":9,"size":"large","date":1195869600000},{"price":40.48,"company":"E.I. du Pont de Nemours and Company","visible":0,"id":10,"size":"extra
large","date":1178679600000}],"success":true}

Take a look how the date attibute is being serialized: “date”:1188615600000. Jackson uses default strategy to determine date formatting in serialization.

One of the annotations Jackson has is @JsonSerialize. You basically use this annotation for configuring serialization aspects. In my case, I decorated by model objects date getter method with this annotation:

@JsonSerialize(using=JsonDateSerializer.class)
public Date getDate() {
return date;
}

JsonDateSerializer is a class I wrote that extends JsonSerializer that will handle the serialization for this field:

 

package com.loiane.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;

/**
 * Used to serialize Java.util.Date, which is not a common JSON
 * type, so we have to create a custom serialize method;.
 *
 * @author Loiane Groner
 * http://loianegroner.com (English)
 * http://loiane.com (Portuguese)
 */
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {

String formattedDate = dateFormat.format(date);

gen.writeString(formattedDate);
}

}

And this is how my object is being serialized now:

{"total":27,"data":[{"price":71.72,"company":"3m Co","visible":1,"id":1,"size":"large","date":"09-01-2007"},{"price":29.01,"company":"Aloca
Inc","visible":0,"id":2,"size":"medium","date":"08-01-2007"},{"price":83.81,"company":"Altria Group
Inc","visible":0,"id":3,"size":"large","date":"08-03-2007"},{"price":52.55,"company":"American Express Company","visible":1,"id":4,"size":"extra
large","date":"01-04-2008"},{"price":64.13,"company":"American International Group
Inc.","visible":1,"id":5,"size":"small","date":"03-04-2008"},{"price":31.61,"company":"AT&T Inc.","visible":0,"id":6,"size":"extra
large","date":"02-01-2008"},{"price":75.43,"company":"Boeing Co.","visible":1,"id":7,"size":"large","date":"01-01-2008"},{"price":67.27,"company":"Caterpillar
Inc.","visible":1,"id":8,"size":"medium","date":"12-03-2007"},{"price":49.37,"company":"Citigroup,
Inc.","visible":1,"id":9,"size":"large","date":"11-24-2007"},{"price":40.48,"company":"E.I. du Pont de Nemours and Company","visible":0,"id":10,"size":"extra
large","date":"05-09-2007"}],"success":true}

Happy coding!

From http://loianegroner.com/2010/09/how-to-serialize-java-util-date-with-jackson-json-processor-spring-3-0/

JSON Jackson (API) Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • Jackson vs Gson: Edge Cases in JSON Parsing for Java Apps
  • Aggregating REST APIs Calls Using Apache Camel
  • The Configure() Method in Jackson in JSON
  • Securing Spring Boot Microservices with JSON Web Tokens (JWT)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!