Skip to content

Commit

Permalink
Merge pull request #11 from GoodforGod/dev
Browse files Browse the repository at this point in the history
[2.0.0]
  • Loading branch information
GoodforGod authored Feb 20, 2023
2 parents 2bc3761 + 2dd232c commit 857c1dd
Show file tree
Hide file tree
Showing 49 changed files with 269 additions and 267 deletions.
22 changes: 3 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Gson configuration and serializers/deserializers for Date/Time in [java.time.*](
## Dependency :rocket:
**Gradle**
```groovy
implementation "io.goodforgod:gson-configuration:1.4.2"
implementation "io.goodforgod:gson-configuration:2.0.0"
```

**Maven**
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>gson-configuration</artifactId>
<version>1.4.2</version>
<version>2.0.0</version>
</dependency>
```

Expand Down Expand Up @@ -58,12 +58,7 @@ Here is list of such formatters:
If you want to know more about why use such Java Date & Time formats, you can [read more here](https://goodforgod.dev/posts/2/)

```java
GsonConfiguration configuration = GsonConfiguration.of();
```

You can also use default Java ISO8601 formatters by:
```java
GsonConfiguration configuration = GsonConfiguration.ofJavaISO();
GsonConfiguration configuration = new GsonConfiguration();
```

## Gson Configuration
Expand Down Expand Up @@ -151,21 +146,10 @@ Gson can also be instantiated via properties using *GsonFactory*.
Gson gson = new GsonFactory().build();
```

There is respected method to build Gson with Java ISO8601 formatters as defaults:
```java
Gson gson = new GsonFactory().buildJavaISO();
```

## Gson Builder

All adapters already registered via when using *GsonConfiguration#builder*.

If you want to register only adapters without configuration:

```java
GsonBuilder builder = GsonAdapters.builder();
```

You can register them manually:
```java
GsonBuilder builder = new GsonBuilder()
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id "maven-publish"

id "org.sonarqube" version "3.3"
id "com.diffplug.spotless" version "6.1.0"
id "com.diffplug.spotless" version "6.12.0"
}

repositories {
Expand All @@ -20,11 +20,11 @@ sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
api "com.google.code.gson:gson:2.9.1"
api "com.google.code.gson:gson:2.10.1"

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.8.2"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.8.2"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.8.2"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.9.1"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.9.1"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.9.1"
}

test {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
groupId=io.goodforgod
artifactId=gson-configuration
artifactVersion=1.4.2
artifactVersion=2.0.0


##### GRADLE #####
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
22 changes: 22 additions & 0 deletions src/main/java/io/goodforgod/gson/configuration/DateFormatters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.goodforgod.gson.configuration;

/**
* ISO8601 formats and patterns
*
* @author Anton Kurako (GoodforGod)
* @since 18.02.2023
*/
final class DateFormatters {

/**
* ISO8601 for {@link java.util.Date} and {@link java.sql.Timestamp}
*/
static final String ISO_DATE = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

/**
* ISO8601 for {@link java.util.Date} and {@link java.sql.Timestamp} as default Java ISO
*/
static final String ISO_DATE_JAVA = "yyyy-MM-dd'T'HH:mm:ssXXX";

private DateFormatters() {}
}
113 changes: 64 additions & 49 deletions src/main/java/io/goodforgod/gson/configuration/GsonAdapterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
import java.time.*;

/**
* @see com.google.gson.GsonBuilder
* @author Anton Kurako (GoodforGod)
* @see com.google.gson.GsonBuilder
* @since 28.04.2021
*/
public final class GsonAdapterBuilder {
final class GsonAdapterBuilder {

private GsonAdapterBuilder() {}

private static GsonBuilder getCommonBuilder() {
return new GsonBuilder()
.setDateFormat(DateTimeFormatters.ISO_DATE)
.setDateFormat(DateFormatters.ISO_DATE)
.registerTypeAdapter(DayOfWeek.class, DayOfWeekDeserializer.INSTANCE)
.registerTypeAdapter(DayOfWeek.class, DayOfWeekSerializer.INSTANCE)
.registerTypeAdapter(Month.class, MonthDeserializer.INSTANCE)
Expand All @@ -27,53 +27,68 @@ private static GsonBuilder getCommonBuilder() {
.registerTypeAdapter(ZoneOffset.class, ZoneOffsetSerializer.INSTANCE);
}

public static GsonBuilder builder() {
return getCommonBuilder()
.registerTypeAdapter(Year.class, YearDeserializer.INSTANCE)
.registerTypeAdapter(Year.class, YearSerializer.INSTANCE)
.registerTypeAdapter(YearMonth.class, YearMonthDeserializer.INSTANCE)
.registerTypeAdapter(YearMonth.class, YearMonthSerializer.INSTANCE)
.registerTypeAdapter(MonthDay.class, MonthDayDeserializer.INSTANCE)
.registerTypeAdapter(MonthDay.class, MonthDaySerializer.INSTANCE)
.registerTypeAdapter(Instant.class, InstantDeserializer.INSTANCE)
.registerTypeAdapter(Instant.class, InstantSerializer.INSTANCE)
.registerTypeAdapter(LocalDate.class, LocalDateDeserializer.INSTANCE)
.registerTypeAdapter(LocalDate.class, LocalDateSerializer.INSTANCE)
.registerTypeAdapter(LocalTime.class, LocalTimeDeserializer.INSTANCE)
.registerTypeAdapter(LocalTime.class, LocalTimeSerializer.INSTANCE)
.registerTypeAdapter(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE)
.registerTypeAdapter(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE)
.registerTypeAdapter(OffsetTime.class, OffsetTimeDeserializer.INSTANCE)
.registerTypeAdapter(OffsetTime.class, OffsetTimeSerializer.INSTANCE)
.registerTypeAdapter(OffsetDateTime.class, OffsetDateTimeDeserializer.INSTANCE)
.registerTypeAdapter(OffsetDateTime.class, OffsetDateTimeSerializer.INSTANCE)
.registerTypeAdapter(ZonedDateTime.class, ZonedDateTimeDeserializer.INSTANCE)
.registerTypeAdapter(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE);
}

public static GsonBuilder builder(GsonConfiguration configuration) {
static GsonBuilder builder(GsonConfiguration configuration) {
return getCommonBuilder()
.setDateFormat(configuration.getDateFormat())
.registerTypeAdapter(Year.class, new YearDeserializer(configuration.getYearFormat()))
.registerTypeAdapter(Year.class, new YearSerializer(configuration.getYearFormat()))
.registerTypeAdapter(YearMonth.class, new YearMonthDeserializer(configuration.getYearMonthFormat()))
.registerTypeAdapter(YearMonth.class, new YearMonthSerializer(configuration.getYearMonthFormat()))
.registerTypeAdapter(MonthDay.class, new MonthDayDeserializer(configuration.getMonthDayFormat()))
.registerTypeAdapter(MonthDay.class, new MonthDaySerializer(configuration.getMonthDayFormat()))
.registerTypeAdapter(Instant.class, new InstantDeserializer(configuration.getInstantFormat()))
.registerTypeAdapter(Instant.class, new InstantSerializer(configuration.getInstantFormat()))
.registerTypeAdapter(LocalDate.class, new LocalDateDeserializer(configuration.getLocalDateFormat()))
.registerTypeAdapter(LocalDate.class, new LocalDateSerializer(configuration.getLocalDateFormat()))
.registerTypeAdapter(LocalTime.class, new LocalTimeDeserializer(configuration.getLocalTimeFormat()))
.registerTypeAdapter(LocalTime.class, new LocalTimeSerializer(configuration.getLocalTimeFormat()))
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer(configuration.getLocalDateTimeFormat()))
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer(configuration.getLocalDateTimeFormat()))
.registerTypeAdapter(OffsetTime.class, new OffsetTimeDeserializer(configuration.getOffsetTimeFormat()))
.registerTypeAdapter(OffsetTime.class, new OffsetTimeSerializer(configuration.getOffsetTimeFormat()))
.registerTypeAdapter(OffsetDateTime.class,
new OffsetDateTimeDeserializer(configuration.getOffsetDateTimeFormat()))
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeSerializer(configuration.getOffsetDateTimeFormat()))
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeDeserializer(configuration.getZonedDateTimeFormat()))
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer(configuration.getZonedDateTimeFormat()));
.registerTypeAdapter(Year.class, (configuration.getYearFormat() == null)
? YearDeserializer.INSTANCE
: new YearDeserializer(configuration.getYearFormat()))
.registerTypeAdapter(Year.class, (configuration.getYearFormat() == null)
? YearSerializer.INSTANCE
: new YearSerializer(configuration.getYearFormat()))
.registerTypeAdapter(YearMonth.class, (configuration.getYearMonthFormat() == null)
? YearMonthDeserializer.INSTANCE
: new YearMonthDeserializer(configuration.getYearMonthFormat()))
.registerTypeAdapter(YearMonth.class, (configuration.getYearMonthFormat() == null)
? YearMonthSerializer.INSTANCE
: new YearMonthSerializer(configuration.getYearMonthFormat()))
.registerTypeAdapter(MonthDay.class, (configuration.getMonthDayFormat() == null)
? MonthDayDeserializer.INSTANCE
: new MonthDayDeserializer(configuration.getMonthDayFormat()))
.registerTypeAdapter(MonthDay.class, (configuration.getMonthDayFormat() == null)
? MonthDaySerializer.INSTANCE
: new MonthDaySerializer(configuration.getMonthDayFormat()))
.registerTypeAdapter(Instant.class, (configuration.getInstantFormat() == null)
? InstantDeserializer.INSTANCE
: new InstantDeserializer(configuration.getInstantFormat()))
.registerTypeAdapter(Instant.class, (configuration.getInstantFormat() == null)
? InstantSerializer.INSTANCE
: new InstantSerializer(configuration.getInstantFormat()))
.registerTypeAdapter(LocalDate.class, (configuration.getLocalDateFormat() == null)
? LocalDateDeserializer.INSTANCE
: new LocalDateDeserializer(configuration.getLocalDateFormat()))
.registerTypeAdapter(LocalDate.class, (configuration.getLocalDateFormat() == null)
? LocalDateSerializer.INSTANCE
: new LocalDateSerializer(configuration.getLocalDateFormat()))
.registerTypeAdapter(LocalTime.class, (configuration.getLocalTimeFormat() == null)
? LocalTimeDeserializer.INSTANCE
: new LocalTimeDeserializer(configuration.getLocalTimeFormat()))
.registerTypeAdapter(LocalTime.class, (configuration.getLocalTimeFormat() == null)
? LocalTimeSerializer.INSTANCE
: new LocalTimeSerializer(configuration.getLocalTimeFormat()))
.registerTypeAdapter(LocalDateTime.class, (configuration.getLocalDateTimeFormat() == null)
? LocalDateTimeDeserializer.INSTANCE
: new LocalDateTimeDeserializer(configuration.getLocalDateTimeFormat()))
.registerTypeAdapter(LocalDateTime.class, (configuration.getLocalDateTimeFormat() == null)
? LocalDateTimeSerializer.INSTANCE
: new LocalDateTimeSerializer(configuration.getLocalDateTimeFormat()))
.registerTypeAdapter(OffsetTime.class, (configuration.getOffsetTimeFormat() == null)
? OffsetTimeDeserializer.INSTANCE
: new OffsetTimeDeserializer(configuration.getOffsetTimeFormat()))
.registerTypeAdapter(OffsetTime.class, (configuration.getOffsetTimeFormat() == null)
? OffsetTimeSerializer.INSTANCE
: new OffsetTimeSerializer(configuration.getOffsetTimeFormat()))
.registerTypeAdapter(OffsetDateTime.class, (configuration.getOffsetDateTimeFormat() == null)
? OffsetDateTimeDeserializer.INSTANCE
: new OffsetDateTimeDeserializer(configuration.getOffsetDateTimeFormat()))
.registerTypeAdapter(OffsetDateTime.class, (configuration.getOffsetDateTimeFormat() == null)
? OffsetDateTimeSerializer.INSTANCE
: new OffsetDateTimeSerializer(configuration.getOffsetDateTimeFormat()))
.registerTypeAdapter(ZonedDateTime.class, (configuration.getZonedDateTimeFormat() == null)
? ZonedDateTimeDeserializer.INSTANCE
: new ZonedDateTimeDeserializer(configuration.getZonedDateTimeFormat()))
.registerTypeAdapter(ZonedDateTime.class, (configuration.getZonedDateTimeFormat() == null)
? ZonedDateTimeSerializer.INSTANCE
: new ZonedDateTimeSerializer(configuration.getZonedDateTimeFormat()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public int modifier() {
}
}

private DateTimeFormatter instantFormat = DateTimeFormatters.ISO_INSTANT;
private DateTimeFormatter localDateFormat = DateTimeFormatters.ISO_LOCAL_DATE;
private DateTimeFormatter localTimeFormat = DateTimeFormatters.ISO_LOCAL_TIME;
private DateTimeFormatter localDateTimeFormat = DateTimeFormatters.ISO_LOCAL_DATE_TIME;
private DateTimeFormatter offsetTimeFormat = DateTimeFormatters.ISO_OFFSET_TIME;
private DateTimeFormatter offsetDateTimeFormat = DateTimeFormatters.ISO_OFFSET_DATE_TIME;
private DateTimeFormatter zonedDateTimeFormat = DateTimeFormatters.ISO_ZONED_DATE_TIME;
private DateTimeFormatter yearFormat = DateTimeFormatters.ISO_YEAR;
private DateTimeFormatter yearMonthFormat = DateTimeFormatters.ISO_YEAR_MONTH;
private DateTimeFormatter monthDayFormat = DateTimeFormatters.ISO_MONTH_DAY;
private String dateFormat = DateTimeFormatters.ISO_DATE;
private DateTimeFormatter instantFormat;
private DateTimeFormatter localDateFormat;
private DateTimeFormatter localTimeFormat;
private DateTimeFormatter localDateTimeFormat;
private DateTimeFormatter offsetTimeFormat;
private DateTimeFormatter offsetDateTimeFormat;
private DateTimeFormatter zonedDateTimeFormat;
private DateTimeFormatter yearFormat;
private DateTimeFormatter yearMonthFormat;
private DateTimeFormatter monthDayFormat;
private String dateFormat = DateFormatters.ISO_DATE;

/**
* Forces {@link java.time.format.ResolverStyle#STRICT} for all formatters setters
Expand Down Expand Up @@ -162,37 +162,8 @@ public int modifier() {
*/
private boolean serializeSpecialFloatingPointValues = false;

/**
* @return configuration with formatters {@link DateTimeFormatters}
*/
public static GsonConfiguration of() {
return new GsonConfiguration();
}

/**
* @return configuration with Java default formatters {@link DateTimeFormatter}
*/
public static GsonConfiguration ofJavaISO() {
final GsonConfiguration configuration = new GsonConfiguration();

configuration.setDateFormat(DateTimeFormatters.JAVA_ISO_DATE);
configuration.setInstantFormat(DateTimeFormatter.ISO_INSTANT);
configuration.setLocalDateFormat(DateTimeFormatter.ISO_LOCAL_DATE);
configuration.setLocalTimeFormat(DateTimeFormatter.ISO_LOCAL_TIME);
configuration.setLocalDateTimeFormat(DateTimeFormatter.ISO_DATE_TIME);
configuration.setOffsetTimeFormat(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
configuration.setOffsetDateTimeFormat(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
configuration.setZonedDateTimeFormat(DateTimeFormatter.ISO_ZONED_DATE_TIME);

return configuration;
}

public static GsonConfiguration ofPropertiesJavaISO(Properties properties) {
return ofProperties(ofJavaISO(), properties);
}

public static GsonConfiguration ofProperties(Properties properties) {
return ofProperties(of(), properties);
return ofProperties(new GsonConfiguration(), properties);
}

private static GsonConfiguration ofProperties(GsonConfiguration configuration, Properties properties) {
Expand Down
25 changes: 2 additions & 23 deletions src/main/java/io/goodforgod/gson/configuration/GsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ public class GsonFactory {

private Properties properties;
private GsonConfiguration configuration;
private GsonConfiguration configurationJavaISO;

/**
* @return Gson built with {@link GsonConfiguration#of()} as base
* @return Gson built with {@link GsonConfiguration} as base
*/
public Gson build() {
return builder().create();
}

/**
* @return Gson built with {@link GsonConfiguration#of()} as base
* @return Gson built with {@link GsonConfiguration} as base
*/
public GsonBuilder builder() {
if (properties == null)
Expand All @@ -40,26 +39,6 @@ public GsonBuilder builder() {
return configuration.builder();
}

/**
* @return Gson built with {@link GsonConfiguration#ofJavaISO()} as base
*/
public Gson buildJavaISO() {
return builderJavaISO().create();
}

/**
* @return Gson built with {@link GsonConfiguration#ofJavaISO()} as base
*/
public GsonBuilder builderJavaISO() {
if (properties == null)
this.properties = getProperties();

if (configurationJavaISO == null)
this.configurationJavaISO = GsonConfiguration.ofPropertiesJavaISO(properties);

return configurationJavaISO.builder();
}

private Properties getProperties() {
try (InputStream resource = getClass().getClassLoader().getResourceAsStream(PROPERTY_FILE)) {
if (resource != null) {
Expand Down
Loading

0 comments on commit 857c1dd

Please sign in to comment.