Skip to content

Commit

Permalink
[1.2.0]
Browse files Browse the repository at this point in the history
README.md improved
  • Loading branch information
GoodforGod committed Jan 18, 2022
1 parent 4831478 commit eaf9021
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,36 @@ datetime objects, supported list:
- ZoneId
- ZoneOffset

All adapters register with **ISO8601** formatters by defaults, but you can register them manually with your formatter.
## Formatters

## Formats
Gson Configuration by default comes with [ISO8601 with millis precision](https://goodforgod.dev/posts/2/)
(basically default Java ISO8601 formatters but with millis precision)

Gson Configuration by default comes with [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) with millis precision for all APIs:

Here is list of default formatters for all *java.time.** APIs:
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]*
- 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](https://goodforgod.dev/posts/2/)

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

If you want to know more about Java Date & Time formats, you can [read more here](https://goodforgod.dev/posts/2/)
You can also use default Java ISO8601 formatters by:
```java
GsonConfiguration configuration = GsonConfiguration.ofJavaISO();
```

## Gson Configuration

Library provides configuration for configuring *GsonBuilder* for most properties:

```java
final GsonBuilder builder = new GsonConfiguration()
GsonBuilder builder = new GsonConfiguration()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
.setInstantFormat("uuuu-MM-dd HH:mm:ss")
.setComplexMapKeySerialization(true)
Expand All @@ -82,7 +90,7 @@ final GsonBuilder builder = new GsonConfiguration()

You can configure DateTimeFormatters for provided adapters:
```java
final GsonBuilder builder = new GsonConfiguration()
GsonBuilder builder = new GsonConfiguration()
.setInstantFormat("uuuu-MM-dd HH:mm:ss")
.builder();
```
Expand All @@ -93,11 +101,11 @@ GsonConfiguration also can be filled from *properties* file.

How to build GsonConfiguration from Properties:
```java
final InputStream resource = getClass().getClassLoader().getResourceAsStream("gson.properties");
final Properties properties = new Properties();
InputStream resource = getClass().getClassLoader().getResourceAsStream("gson.properties");
Properties properties = new Properties();
properties.load(resource);

final GsonConfiguration configuration = GsonConfiguration.ofProperties(properties);
GsonConfiguration configuration = GsonConfiguration.ofProperties(properties);
```

Full list of properties ([check GsonProperties](https://github.com/GoodforGod/gson-configuration/blob/master/src/main/java/io/gson/adapters/config/GsonProperties.java)):
Expand All @@ -114,6 +122,9 @@ 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
Expand All @@ -131,11 +142,15 @@ gson.policy.longSerialization=STRING
Gson can also be instantiated via properties using *GsonFactory*.

*GsonFactory* is looking for property file in root *resource*: **gson.properties**

```java
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*.
Expand All @@ -149,7 +164,7 @@ GsonBuilder builder = GsonAdapters.builder();
You can register them manually:
```java
GsonBuilder builder = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateSerializer())
.registerTypeAdapter(LocalDate.class, LocalDateSerializer.INSTANCE)
```

You can register with custom formatter also:
Expand Down

0 comments on commit eaf9021

Please sign in to comment.