Added Type Adapter for Instant in new Module gson.

This commit is contained in:
Konrad Neitzel 2025-04-04 08:32:05 +02:00
parent 9f2463b94c
commit f37d943cba
3 changed files with 102 additions and 0 deletions

49
gson/pom.xml Normal file
View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.neitzel.lib</groupId>
<artifactId>neitzellib</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>gson</artifactId>
<properties>
<!-- Application Properties -->
<link.name>${project.artifactId}</link.name>
<launcher>${project.artifactId}</launcher>
<appName>${project.artifactId}</appName>
<jar.filename>${project.artifactId}-${project.version}</jar.filename>
<!-- Dependency Versions -->
<gson.version>2.11.0</gson.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${jar.filename}</finalName>
<plugins>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,52 @@
package de.neitzel.gson.adapter;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.format.DateTimeParseException;
/**
* A custom Gson type adapter for serializing and deserializing {@link Instant} objects. This adapter handles conversion between
* {@code Instant} and its ISO-8601 formatted string representation.
* <p>
* Implementation details: - Serialization: Converts {@link Instant} to its ISO-8601 string representation. - Deserialization: Parses a
* valid ISO-8601 string to an {@link Instant} instance.
* <p>
* The adapter ensures compliance with the ISO-8601 standard for interoperability. A {@link JsonParseException} is thrown if the provided
* JSON element during deserialization does not conform to the supported {@code Instant} format.
*/
public class InstantTypeAdapter implements JsonSerializer<Instant>, JsonDeserializer<Instant> {
/**
* Serializes an {@link Instant} object into its ISO-8601 string representation as a {@link JsonElement}.
*
* @param src the {@link Instant} object to be serialized
* @param typeOfSrc the specific genericized type of {@code src} (typically ignored in this implementation)
* @param context the context of the serialization process to provide custom serialization logic
* @return a {@link JsonPrimitive} containing the ISO-8601 formatted string representation of the {@link Instant}
*/
@Override
public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.toString()); // ISO-8601: "2025-04-02T10:15:30Z"
}
/**
* Deserializes a JSON element into an {@link Instant} object.
*
* @param json the JSON data being deserialized
* @param typeOfT the specific genericized type of the object to deserialize
* @param context the deserialization context
* @return the deserialized {@link Instant} object
* @throws JsonParseException if the JSON element does not represent a valid ISO-8601 instant format
*/
@Override
public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
return Instant.parse(json.getAsString());
} catch (DateTimeParseException e) {
throw new JsonParseException("Invalid Instant format", e);
}
}
}

View File

@ -14,6 +14,7 @@
<module>core</module> <module>core</module>
<module>encryption</module> <module>encryption</module>
<module>fx</module> <module>fx</module>
<module>gson</module>
<module>net</module> <module>net</module>
<module>log4j</module> <module>log4j</module>
<module>fx-example</module> <module>fx-example</module>