0

I am working with Jackson version 2.8.7

I have a Person object that is printed in the following way:

object: Persona [id=087, nombre=Leonardo, apellido=Jordan, fecha=Sun Jul 05 00:00:00 PET 1981] 

Observe the date part Sun Jul 05 00:00:00 PET 1981

I did a research from these two valuable posts about how serialize an object (entity) and a Date object into JSON format:

When I use:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.getSerializationConfig().with(dateFormat);

ObjectWriter ow = objectMapper.writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);

Observe two things:

  • objectMapper.getSerializationConfig().with(dateFormat);
  • writer()

I always get:

json: {
  "id" : "087",
  "nombre" : "Leonardo",
  "apellido" : "Jordan",
  "fecha" : 363157200000
} 

Observe the 363157200000 value and without quotes. Therefore it did not work.

But If use:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
ObjectMapper objectMapper = new ObjectMapper();
//objectMapper.getSerializationConfig().with(dateFormat);

ObjectWriter ow = objectMapper.writer(dateFormat).withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);

Observe two things:

  • //objectMapper.getSerializationConfig().with(dateFormat); commented
  • writer(dateFormat) the argument

I always get:

json: {
  "id" : "087",
  "nombre" : "Leonardo",
  "apellido" : "Jordan",
  "fecha" : "1981-07-05"
} 

Now works.

  1. Why the first approach did not work?
  2. What is the correct configuration for the first approach to get the expected behaviour?

I am more interested in the first approach in case I need apply more features through the getSerializationConfig().with(...) methods.

Community
  • 1
  • 1
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

1 Answers1

1

The first approach does not work because with() creates and returns a new SerializationConfig, so it is not applied to the objectMapper instance that you are using. This is the javadocs for that method:

/**
 * Fluent factory method that will construct and return a new configuration
 * object instance with specified features enabled.
 */

This method can be used when you want to reuse the configuration of one ObjectMapper instance for another one. With the with() method you can specify any difference you want to apply to the copy of the config.

For example, here we are using the configuration of the mapper 1, with a different date format, for the mapper2. The mapper 1 is not changed.

    ObjectMapper mapper2 = new ObjectMapper();
    mapper2.setConfig(mapper1.getSerializationConfig().with(otherDateFormat));

In order to apply the dateformat to every case you can use setDateFormat(DateFormat):

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(dateFormat);
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Franjavi
  • 647
  • 4
  • 14
  • Thank you, it works. Just curious when is mandatory or is useful use the `objectMapper.getSerializationConfig().with(...)` approach. If you can add that answer to your current answer would be great. Thanks gain. – Manuel Jordan Mar 12 '17 at 15:19
  • 1
    Basically it is used to reuse the configuration from a mapper into another one, being able to change this configuration at the same time. I added a simple example. Hope it helps – Franjavi Mar 14 '17 at 09:33
  • Thanks a lot by the explanation. Now has more sense. – Manuel Jordan Mar 14 '17 at 12:05
  • Just curious how that `mapper2` should be used later. I mean, your code explain how create it, but not how and where use it. I understand `mapper1` is reused. – Manuel Jordan Mar 14 '17 at 12:11
  • 1
    `mapper2` would be used like any other different ObjectMapper, totally independent to `mapper1`. The use of it could be when you need different mappers depending on the situation (different points of integration with different JSON structures coming, for example), and this is a simple way to share the same base configuration. Anyway, this is a capability I never needed to use. – Franjavi Mar 14 '17 at 13:23