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

Add support for Optional<T> in parsing instead of null #2644

Open
spacey-sooty opened this issue Mar 5, 2024 · 4 comments
Open

Add support for Optional<T> in parsing instead of null #2644

spacey-sooty opened this issue Mar 5, 2024 · 4 comments
Labels
enhancement java8 Issues related to making Java 8 the minimum supported version

Comments

@spacey-sooty
Copy link

Problem solved by the feature

Often when parsing their is the risk of null exceptions and more importantly it is hard to document a field being optional through the type signature.

Feature description

This feature would support using Optional for fields where if GSON cannot find the key it would give the none variant.

@eamonnmcmanus eamonnmcmanus added the java8 Issues related to making Java 8 the minimum supported version label Mar 5, 2024
@Marcono1234
Copy link
Collaborator

This is similar to #1005, or possibly a more specific duplicate of it. And related to #61.

Currently when a property is not present in the JSON data, Gson does not modify the field of the Java object and does not call the TypeAdapter. So if the field has type Optional but has an (implicit) default value of null, then Gson keeps that value.

As workaround you can initialize the field with a non-null default value, for example Optional<String> s = Optional.empty();. But you must make sure that the declaring class has a no-args constructor, otherwise this might not work, see GsonBuilder.disableJdkUnsafe() for details.

@spacey-sooty
Copy link
Author

spacey-sooty commented Mar 6, 2024

Thanks for providing this work around. I have had a go using it but this actually won't work in the Optional case. I can provide the stacktrace if you would like but I am reasonably confident from reading through it that when I specify Optional<String> because it is an object GSON does not recognise it expects an object as opposed to a string and will throw an exception.

I like the idea of providing a more general solution here, but I think there could be some advantages to implicitly using the Optional.empty() default for fields typed Optional<T>. I think this would be a good way of providing explicit support for something being optional within the type of a field.

@Marcono1234
Copy link
Collaborator

when I specify Optional<String> because it is an object GSON does not recognise it expects an object as opposed to a string and will throw an exception

Sorry, I should have mentioned that Gson has no built-in adapter for Optional yet (#1102) so you would have to write one yourself or use one from a third-party library, see also this Stack Overflow question.

@spacey-sooty
Copy link
Author

Ok I'll do that, thanks for the help!

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

3 participants