Gson configuration and serializers/deserializers for Date/Time in java.time.* package.
Gradle
implementation "io.goodforgod:gson-configuration:1.4.1"
Maven
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>gson-configuration</artifactId>
<version>1.4.1</version>
</dependency>
Library include serializers & deserializers for most java.time.* datetime objects, supported list:
- Instant
- LocalDate
- LocalTime
- LocalDateTime
- OffsetTime
- OffsetDateTime
- ZonedDateTime
- Year
- YearMonth
- Month
- MonthDay
- DayOfWeek
- ZoneId
- ZoneOffset
Gson Configuration by default comes with ISO8601 with millis precision (basically default Java ISO8601 formatters but with millis precision)
Here is list of such formatters:
- LocalDateTime - uuuu-MM-dd'T'HH:mm:ss[.SSS]
- LocalDate - uuuu-MM-dd
- LocalTime - HH:mm:ss[.SSS]
- OffsetDateTime - uuuu-MM-dd'T'HH:mm:ss[.SSS]XXX
- OffsetTime - HH:mm:ss[.SSS]XXX
- ZonedDateTime - uuuu-MM-dd'T'HH:mm:ss[.SSS]XXX['['VV']']
If you want to know more about why use such Java Date & Time formats, you can read more here
GsonConfiguration configuration = GsonConfiguration.of();
You can also use default Java ISO8601 formatters by:
GsonConfiguration configuration = GsonConfiguration.ofJavaISO();
Library provides configuration for configuring GsonBuilder for most properties:
GsonBuilder builder = new GsonConfiguration()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
.setInstantFormat("uuuu-MM-dd HH:mm:ss")
.setComplexMapKeySerialization(true)
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
.setLenient(true)
.setEscapeHtmlChars(false)
.setPrettyPrinting(true)
.setGenerateNonExecutableJson(true)
.setSerializeNulls(true)
.setSerializeSpecialFloatingPointValues(true)
.setExcludeFieldsWithoutExposeAnnotation(true)
.setExcludeFieldsWithModifiers(GsonConfiguration.FieldModifiers.TRANSIENT)
.builder();
You can configure DateTimeFormatters for provided adapters:
GsonBuilder builder = new GsonConfiguration()
.setInstantFormat("uuuu-MM-dd HH:mm:ss")
.builder();
By default Gson or GsonBuilder that is build via GsonConfiguration or GsonFactory doesn't serialize/deserialize transient, static, volatile, synchronized fields. You need to use configuration setters to configure it otherwise.
GsonConfiguration also can be filled from properties file.
How to build GsonConfiguration from Properties:
InputStream resource = getClass().getClassLoader().getResourceAsStream("gson.properties");
Properties properties = new Properties();
properties.load(resource);
GsonConfiguration configuration = GsonConfiguration.ofProperties(properties);
Full list of properties (check GsonProperties):
gson.format.instant=uuuu-MM-dd'T'HH:mm:ssX
gson.format.localDate=uuuu-MM-dd
gson.format.localTime=HH:mm:ss.SSS
gson.format.localDateTime=uuuu-MM-dd'T'HH:mm:ss.SSS
gson.format.offsetTime=HH:mm:ss.SSSXXX
gson.format.offsetDateTime=uuuu-MM-dd'T'HH:mm:ss.SSSXXX
gson.format.zonedDateTime=uuuu-MM-dd'T'HH:mm:ss.SSSXXX
gson.format.year=uuuu
gson.format.yearMonth=uuuu-MM
gson.format.monthDay=MM-dd
gson.format.date=yyyy-MM-dd'T'HH:mm:ss.SSSXXX
gson.forceIsoChronology=true
gson.forceResolverStrict=true
gson.lenient=true
gson.serializeNulls=true
gson.prettyPrinting=true
gson.escapeHtmlChars=false
gson.generateNonExecutableJson=true
gson.serializeComplexMapKey=true
gson.serializeSpecialFloatingPointValues=true
gson.excludeFieldsWithoutExposeAnnotation=false
gson.excludeFieldsWithModifiers=TRANSIENT,STATIC,SYNCHRONIZED,VOLATILE
gson.policy.fieldNaming=UPPER_CAMEL_CASE
gson.policy.longSerialization=STRING
Gson can also be instantiated via properties using GsonFactory.
GsonFactory is looking for property file in root resource: gson.properties
Gson gson = new GsonFactory().build();
There is respected method to build Gson with Java ISO8601 formatters as defaults:
Gson gson = new GsonFactory().buildJavaISO();
All adapters already registered via when using GsonConfiguration#builder.
If you want to register only adapters without configuration:
GsonBuilder builder = GsonAdapters.builder();
You can register them manually:
GsonBuilder builder = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, LocalDateSerializer.INSTANCE)
You can register with custom formatter also:
GsonBuilder builder = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateSerializer(DateTimeFormatters.ISO_LOCAL_DATE))
This project licensed under the MIT - see the LICENSE file for details.