Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/augi/gson-scala

Allows to use Scala and Java 8 types with gson library.
https://github.com/augi/gson-scala

Last synced: about 2 months ago
JSON representation

Allows to use Scala and Java 8 types with gson library.

Awesome Lists containing this project

README

        

# This project is no longer maintained!
[gson](https://github.com/google/gson) library heavily depends on reflection and so it's hard to keep the support for Scala. Use [json4s](http://json4s.org/) or [circe](https://github.com/circe/circe) library instead.

# gson-scala

[![Build Status](https://travis-ci.org/augi/gson-scala.svg?branch=master)](https://travis-ci.org/augi/gson-scala) [![Download](https://api.bintray.com/packages/augi/maven/gson-scala_2.12/images/download.svg) ](https://bintray.com/augi/maven/gson-scala_2.12/_latestVersion)

Allows to use Scala and Java 8 types with [gson library](https://github.com/google/gson).

Supported types:
* `java.time.Instant`
* `java.time.Duration`
* `scala.concurrent.duration.Duration`
* `java.util.Optional`
* `scala.Option`
* `scala.Seq`

## How to use
```xml

cz.augi.gsonscala
gson-scala_2.12
$latestVersion

```

```gradle
compile "cz.augi.gsonscala:gson-scala_2.12:$latestVersion"
```

```scala
import cz.augi.gsonscala._
val gson = new GsonBuilder()
.registerMillisDurationConverters() // registers for Duration classes expecting millis in integer values
.registerUnixMillisInstantConverter() // registers for Instant expecting Unix millis in integer values
.registerBasicConverters() // registers for Optional, Option and Seq
.create()
```

The `registerBasicConverters``must be called as the last method before the `create` method.

Alternatively, these methods can be used for registrations:
* `registerSecondsDurationConverters` - expects durations in seconds
* `registerStringDurationConverters`
* `registerUnixSecondsInstantConverter` - expects time in Unix timestamp (seconds)
* `registerStringInstantConverter` - expects time in ISO format

You can also cherry-pick some of converters [as shown here](https://github.com/augi/gson-scala/blob/master/src/main/scala/cz/augi/gsonscala/package.scala).

> Please note that `registerBasicConverters` also registers [NonNullTypeAdapterFactory](https://github.com/augi/gson-scala/blob/master/src/main/scala/cz/augi/gsonscala/NonNullTypeAdapterFactory.scala) - it's required
because the default object deserializer doesn't call deserializer of a field if the value is not even present in the json.

## Why to use this library?
As gson library targets Java 6, it doesn't support Java 8 type out of the box. There are several libraries that add support
for `Optional` type but most of them is not able to handle missing value correctly.

For example [gson-java8-datatype](https://github.com/caoqianli/gson-java8-datatype) looked promising but
it has [too simple tests](https://github.com/caoqianli/gson-java8-datatype/blob/master/src/test/java/net/dongliu/gson/GsonJava8TypeAdapterFactoryTest.java).
E.g. if you have a class with `Optional` field then the deserialized object contains `null` instead of `Optional.empty()`.

The problem is that `gson` by default doesn't write `null` values. So the `read` method of `TypeAdapter` is not even called for missing value
and the field of deserialized object has `null` value. Tests of `gson-java8-datatype` are passing because `null` value is written if it's top-level object.