Usually, when I use Gson, I need to add special logic so that a field of Optional<X> gets serialized as X if it's present, and gets omitted if it's empty; and conversely on deserialization. This seems like the only reasonable way to handle Optional. Would you consider offering this feature (or accepting a PR that implements it)?
Gson can't depend on Java 8 types as it supports down to Java 6.
@JakeWharton So? It could always be in a separate module.
It looks like such a module exists: https://github.com/hsiafan/gson-java8-datatype
The errors emitted if you forget to include it are very confusing and do not obviously lead you to determining that you must install this module, but once you do, Optional types work fine.
really would like to see this. Coming from Swift, the use of Optional with JSON is very useful.
Gson can't depend on Java 8 types as it supports down to Java 6.
With Multi-Release JARs this would now be possible, at least for JRE >= 9.
Most helpful comment
With Multi-Release JARs this would now be possible, at least for JRE >= 9.