Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for java.util.Optional #1102

Open
prdoyle opened this issue Jun 14, 2017 · 11 comments
Open

Support for java.util.Optional #1102

prdoyle opened this issue Jun 14, 2017 · 11 comments
Labels
enhancement java8 Issues related to making Java 8 the minimum supported version

Comments

@prdoyle
Copy link

prdoyle commented Jun 14, 2017

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)?

@prdoyle prdoyle changed the title Support for Optional Support for java.util.Optional Jun 14, 2017
@JakeWharton
Copy link
Contributor

Gson can't depend on Java 8 types as it supports down to Java 6.

@kaqqao
Copy link

kaqqao commented Nov 30, 2018

@JakeWharton So? It could always be in a separate module.

@stevenschlansker
Copy link

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.

@marcblanchet
Copy link

really would like to see this. Coming from Swift, the use of Optional with JSON is very useful.

@Marcono1234
Copy link
Collaborator

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.

@mleonhard
Copy link

I need this.

@chakpongchung
Copy link

chakpongchung commented Jul 1, 2021

I need this to parse all the *.typed json files below

https://github.com/cs164berkeley/pa3-chocopy-code-generation/tree/master/src/test/data/pa3/sample

Is there a way to generate the POJO given a list of json files? Notice that some of the fields can be Optional

@kaqqao
Copy link

kaqqao commented Jul 1, 2021

@mleonhard @chakpongchung Did you read the thread? What you want is already there, use it: https://github.com/hsiafan/gson-java8-datatype

@piegamesde
Copy link

piegamesde commented Jul 10, 2021

@kaqqao gson-java8-datatype worked great, but sadly it broke with Java 16 and isn't maintained anymore (the linked repository is archived).

Gson can't depend on Java 8 types as it supports down to Java 6.

Java 6 as reached end of life 8(!) years ago, and Java 8 (and thus, Optional) exist for 7 years now. I think it's about time to move on.

@alx741
Copy link

alx741 commented Jul 12, 2021

I agree that keeping that kind of retro-compatibility over including new useful features for this long might be unreasonable. Java 8 has been around for a long time now

@prdoyle
Copy link
Author

prdoyle commented Jul 24, 2021

What is the value of supporting Java versions past their end of life?

lucamilanesio pushed a commit to GerritCodeReview/gerrit that referenced this issue Jan 17, 2022
ChangeIT is by far the biggest class in the code base:

  $ find javatests -name '*.java'|xargs wc -l|sort -nr|head -3
  7087 javatests/com/google/gerrit/acceptance/api/change/ChangeIT.java
  4448 javatests/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java
  4025 javatests/com/google/gerrit/server/notedb/ChangeNotesTest.java

Git history of this class reveals that the majority of recent activity
is related to submit requirements feature.

This change extracts all submit requirement related tests in own class:
SubmitRequirementIT.

Given that another class already exists with a similar name:
ChangeSubmitRequirementIT, rename it to SubmitRequirementCustomRuleIT to
avoid confusion. Note that these classes cannot be merged in one single
test class, because of the assumptions made based on existence of custom
rule.

Another reason to isolate submit requirement related tests in own class
is because at the time of this writing, current implementation of submit
requirement violates JEP 403: "Strongly Encapsulate JDK Internals", see
[1] and related Issue 15504. The problem is related to usage of Optional
class in submit requirement entities and missing Optional type adapter
in gson library: [2].

To rectify the usage of Optional in submit requirement serialization we
would like to isolate the tests in own Bazel rule and add jvm option:
"--add-opens java.base/java.util=ALL-UNNAMED" to this rule. Note that we
intentionally do not want to add --add-opens module option to the base
junit_tests rule definition to prevent that further violations for JEP
403 are inadvertently added to the code base.

[1] https://openjdk.java.net/jeps/403
[2] google/gson#1102
Change-Id: Ica24849bc343c47e6bca22291fc9b8bd282d148f
janScheible added a commit to janScheible/test-gap-analysis that referenced this issue Jul 10, 2022
- now there are also class members of type Optional<?> (instead of wrapping them on-the-fly in the getter)
- had to add OptionalTypeAdapter because Gson does not!!! support Optional<?> in 2022 (see google/gson#1102)
@eamonnmcmanus eamonnmcmanus added the java8 Issues related to making Java 8 the minimum supported version label Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement java8 Issues related to making Java 8 the minimum supported version
Projects
None yet
Development

No branches or pull requests