Skip to content

Commit

Permalink
GSON Datetime (de)serializers (#589)
Browse files Browse the repository at this point in the history
* Proposed serialization fix

* Bumping to a higher version to ensure version match
  • Loading branch information
kickroot authored Mar 20, 2024
1 parent 9527562 commit a2bbaf3
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion magpie-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion magpie-aws/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion magpie-azure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.openraven.magpie</groupId>
<artifactId>magpie-parent</artifactId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>

<artifactId>magpie-azure</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion magpie-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
4 changes: 2 additions & 2 deletions magpie-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>io.openraven.magpie</groupId>
<artifactId>magpie-persist</artifactId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

Expand Down
2 changes: 1 addition & 1 deletion magpie-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion magpie-gcp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.appengine.repackaged.com.google.common.base.Pair;
import com.google.appengine.repackaged.org.joda.time.LocalDate;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
Expand All @@ -38,7 +39,9 @@
import java.lang.reflect.Type;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class GCPUtils {

Expand All @@ -49,12 +52,34 @@ public JsonElement serialize(Duration duration, Type type, JsonSerializationCont
return new JsonPrimitive(duration.toString());
}
}

static class LocalDateTimeSerializer implements JsonSerializer <LocalDateTime> {
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");

@Override
public JsonElement serialize(LocalDateTime localDateTime, Type srcType, JsonSerializationContext context) {
return new JsonPrimitive(formatter.format(localDateTime));
}
}

static class OffsetDateTimeSerializer implements JsonSerializer <OffsetDateTime> {
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");

@Override
public JsonElement serialize(OffsetDateTime offsetDateTime, Type srcType, JsonSerializationContext context) {
return new JsonPrimitive(formatter.format(offsetDateTime));
}
}

private static final Logger logger = LoggerFactory.getLogger(GCPUtils.class);
private static final ObjectMapper mapper = createObjectMapper();

private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(OffsetDateTime.class, (JsonDeserializer<OffsetDateTime>) (json, type, context) -> OffsetDateTime.parse(json.getAsString()))
.registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, type, context) -> LocalDateTime.parse(json.getAsString()))
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer())
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeSerializer())
.registerTypeAdapter(Duration.class, new DurationSerializer())
.create();

Expand Down
2 changes: 1 addition & 1 deletion magpie-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion magpie-persist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion magpie-policy-output-csv/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion magpie-policy-output-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion magpie-policy-output-text/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>magpie-parent</artifactId>
<groupId>io.openraven.magpie</groupId>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>io.openraven.magpie</groupId>
<artifactId>magpie-parent</artifactId>
<packaging>pom</packaging>
<version>0.10.1-SNAPSHOT</version>
<version>0.10.2-SNAPSHOT</version>
<name>Open Raven Magpie</name>
<url>https://github.com/openraven/magpie</url>
<description>Open Raven's Magpie Project</description>
Expand Down

0 comments on commit a2bbaf3

Please sign in to comment.