Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/augi/gson-scala
- Owner: augi
- License: mit
- Created: 2016-04-16T13:43:52.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-11T11:29:18.000Z (about 8 years ago)
- Last Synced: 2024-10-15T16:10:49.427Z (3 months ago)
- Language: Scala
- Homepage:
- Size: 115 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
```xmlcz.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 formatYou 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.