Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add iso 8601 timestamp serialization #308

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import io.americanexpress.synapse.utilities.common.serialization.DateIsoDeserializer;
import io.americanexpress.synapse.utilities.common.serialization.DateIsoSerializer;
import io.americanexpress.synapse.utilities.common.serialization.DateTimeDeserializer;
import io.americanexpress.synapse.utilities.common.serialization.InstantDeserializer;
import io.americanexpress.synapse.utilities.common.serialization.InstantSerializer;
import io.americanexpress.synapse.utilities.common.serialization.DateTimeSerializer;
import io.americanexpress.synapse.utilities.common.serialization.DecimalSerializer;
import io.americanexpress.synapse.utilities.common.serialization.MoneyDeserializer;
Expand All @@ -39,6 +41,7 @@
import org.springframework.context.annotation.Primary;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;

Expand Down Expand Up @@ -69,14 +72,13 @@ public class UtilitiesCommonConfig {
* @return the initial ObjectMapper
*/
private ObjectMapper getInitialObjectMapper() {
JavaTimeModule javaTimeDeserializerModule = new JavaTimeModule();
javaTimeDeserializerModule.addDeserializer(LocalDate.class, new DateIsoDeserializer());
javaTimeDeserializerModule.addDeserializer(LocalDateTime.class, new DateTimeDeserializer());

JavaTimeModule javaTimeSerializerModule = new JavaTimeModule();
javaTimeDeserializerModule.addSerializer(LocalDate.class, new DateIsoSerializer());
javaTimeDeserializerModule.addSerializer(LocalDateTime.class, new DateTimeSerializer());

JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDate.class, new DateIsoDeserializer());
javaTimeModule.addDeserializer(LocalDateTime.class, new DateTimeDeserializer());
javaTimeModule.addDeserializer(Instant.class, new InstantDeserializer());
javaTimeModule.addSerializer(LocalDate.class, new DateIsoSerializer());
javaTimeModule.addSerializer(LocalDateTime.class, new DateTimeSerializer());
javaTimeModule.addSerializer(Instant.class, new InstantSerializer());

SimpleModule moneySerializer = new SimpleModule();
moneySerializer.addSerializer(BigDecimal.class, new CurrencySerializer());
Expand All @@ -93,8 +95,7 @@ private ObjectMapper getInitialObjectMapper() {
mapper.registerModule(moneyDeserializer);//This one could be avoided if used the USE_BIG_...
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);

mapper.registerModule(javaTimeDeserializerModule);//This is to register the local date deserializer
mapper.registerModule(javaTimeSerializerModule);//Not needed with the below serialization feature.
mapper.registerModule(javaTimeModule);//This is to register the local date deserializer
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.registerModule(decimalSerializer); //This is to add commas and decimals to all Doubles
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Expand Down
tisla1080 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.americanexpress.synapse.utilities.common.serialization;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

/**
* {@code InstantDeserializer} deserializes a string in ISO-8601 format including any offset into an {@link Instant} representing a single point
* on the timeline in UTC. The resulting instant looks is in the format "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'". The seconds and fraction of seconds are optional.
*/
public class InstantDeserializer extends JsonDeserializer<Instant> {

private static final DateTimeFormatter ISO_INSTANT_OPTIONAL_SECOND_DATE_TIME_FORMATTER = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.appendOffsetId()
.toFormatter();
jmartinez5120 marked this conversation as resolved.
Show resolved Hide resolved

@Override
public Instant deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return Instant.from(ISO_INSTANT_OPTIONAL_SECOND_DATE_TIME_FORMATTER.parse(jsonParser.getValueAsString()));
}
}
tisla1080 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.americanexpress.synapse.utilities.common.serialization;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

/**
* {@code InstantSerializer} serializes an {@link Instant} object into a string representing that single point on the timeline in ISO-8601 format in UTC.
*/
public class InstantSerializer extends JsonSerializer<Instant> {

private static final DateTimeFormatter ISO_INSTANT_OPTIONAL_SECOND_DATE_TIME_FORMATTER = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.appendLiteral('Z')
.toFormatter()
.withZone(ZoneOffset.UTC);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we set the Zone, do we need to append the Literal Z?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we do otherwise, we end up getting the string back as "2024-08-30T14:48:34.857239" even though the java Instant object value was actually 2024-08-30T14:48:34.857239Z. The numbers all look correct but we miss the Z if we remove the appendLiteral. If we remove the withZone then we get a parsing error entirely


@Override
public void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(ISO_INSTANT_OPTIONAL_SECOND_DATE_TIME_FORMATTER.format(instant));
}
}