-
Notifications
You must be signed in to change notification settings - Fork 768
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 Optional<T>
#1329
Comments
Any reason you can't just write an adapter for it per the message? |
I don't want to write an adapter. Here are the reasons why:
Moshi supports I would also ask for |
List maps to a fundamental type in JSON: arrays. You can't just pick arbitrary types and ask why they aren't supported even though they're built in. We also don't support Set, for example. Supporting Optional means we force Android users to a fairly high minimum supported version or to enable coreLibraryDesugaring. We also could conditionally install an adapter for it based on whether classloading the type throws or not. We definitely aren't going to add support for Guava types. Someone can write a third-party library with adapters for its numerous collection APIs. |
No action to take on this. Writing your own public final class OptionalFactory implements JsonAdapter.Factory {
@Nullable
@Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
if (!annotations.isEmpty()) return null;
if (!(type instanceof ParameterizedType)) return null;
Class<?> rawType = Types.getRawType(type);
if (rawType != Optional.class) return null;
Type optionalType = ((ParameterizedType) type).getActualTypeArguments()[0];
JsonAdapter<?> optionalTypeAdapter = moshi.adapter(optionalType).nullSafe();
return new OptionalJsonAdapter<>(optionalTypeAdapter);
}
private static class OptionalJsonAdapter<T> extends JsonAdapter<Optional<T>> {
private final JsonAdapter<T> optionalTypeAdapter;
public OptionalJsonAdapter(JsonAdapter<T> optionalTypeAdapter) {
this.optionalTypeAdapter = optionalTypeAdapter;
}
@Nullable
@Override
public Optional<T> fromJson(JsonReader reader) throws IOException {
T instance = optionalTypeAdapter.fromJson(reader);
return Optional.ofNullable(instance);
}
@Override
public void toJson(JsonWriter writer, @Nullable Optional<T> value) throws IOException {
if (value.isPresent()) {
optionalTypeAdapter.toJson(writer, value.get());
} else {
writer.nullValue();
}
}
}
} |
Thank you both for your work on Moshi. It solves a problem for me and helps get me closer to launching my product. It also improves my opinion of Square. I have a friend who works there. I have eaten with him in the Square SF office. I consider working at Square each time I look for a job. @ZacSweers Thanks for providing an example. It is 45-lines and 1.3KB of code. With tests, it will be 2x or 3x bigger. In my opinion, that is not a trivial amount of code to maintain. @JakeWharton Thanks for telling me about the limitations of Android users.
To clarify, I asked only for I avoid using
I understand if you personally like using Sincerely, |
Your intent was clear. Perhaps you recall asking it as a question:
I merely obliged and answered. Anyway despite your tone I'm supportive of this. |
to anyone else in the future, if you want to use @ZacSweers's sample code (or really any other If you're trying to deserialize json that was serialized by somebody else, and they do not serialize nulls, I can't find any way ho have the adapter be invoked. It'd be nice if it was supported out of the box, because this is a bit of a gotcha. |
I try to avoid using
null
. I want to use Moshi to convert a class to/from JSON which containsOptional<?>
:Moshi complains:
java.lang.IllegalArgumentException: Platform class java.util.Optional in java.util.Optional<java.lang.String> requires explicit JsonAdapter to be registered
.How about adding support for it in Moshi?
The text was updated successfully, but these errors were encountered: