|
| 1 | +/* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ |
| 2 | + |
| 3 | +package io.quarkus.amazon.lambda.runtime; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Date; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 9 | +import com.fasterxml.jackson.core.JsonParser; |
| 10 | +import com.fasterxml.jackson.core.json.PackageVersion; |
| 11 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 12 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 13 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 14 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 15 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 16 | + |
| 17 | +/** |
| 18 | + * Copied from: <a href= |
| 19 | + * "https://raw.githubusercontent.com/aws/aws-lambda-java-libs/95dc035b2a3200be04eaa48cef6053404250e547/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/modules/DateModule.java">here</a> |
| 20 | + * |
| 21 | + * The AWS API represents a date as a double, which specifies the fractional |
| 22 | + * number of seconds since the epoch. Java's Date, however, represents a date as |
| 23 | + * a long, which specifies the number of milliseconds since the epoch. This |
| 24 | + * class is used to translate between these two formats. |
| 25 | + * |
| 26 | + * This class is copied from LambdaEventBridgeservice |
| 27 | + * com.amazon.aws.lambda.stream.ddb.DateModule |
| 28 | + */ |
| 29 | +class DateModule extends SimpleModule { |
| 30 | + private static final long serialVersionUID = 1L; |
| 31 | + |
| 32 | + public static final class Serializer extends JsonSerializer<Date> { |
| 33 | + @Override |
| 34 | + public void serialize(Date date, JsonGenerator generator, SerializerProvider serializers) throws IOException { |
| 35 | + if (date != null) { |
| 36 | + generator.writeNumber(millisToSeconds(date.getTime())); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public static final class Deserializer extends JsonDeserializer<Date> { |
| 42 | + @Override |
| 43 | + public Date deserialize(JsonParser parser, DeserializationContext context) throws IOException { |
| 44 | + double dateSeconds = parser.getValueAsDouble(); |
| 45 | + if (dateSeconds == 0.0) { |
| 46 | + return null; |
| 47 | + } else { |
| 48 | + return new Date((long) secondsToMillis(dateSeconds)); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static double millisToSeconds(double millis) { |
| 54 | + return millis / 1000.0; |
| 55 | + } |
| 56 | + |
| 57 | + private static double secondsToMillis(double seconds) { |
| 58 | + return seconds * 1000.0; |
| 59 | + } |
| 60 | + |
| 61 | + public DateModule() { |
| 62 | + super(PackageVersion.VERSION); |
| 63 | + addSerializer(Date.class, new Serializer()); |
| 64 | + addDeserializer(Date.class, new Deserializer()); |
| 65 | + } |
| 66 | +} |
0 commit comments