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 generic type adapters #328

Merged
merged 6 commits into from
Nov 3, 2016
Merged

Support generic type adapters #328

merged 6 commits into from
Nov 3, 2016

Conversation

gfx
Copy link
Member

@gfx gfx commented Nov 2, 2016

Given an interface EnumDescription and enum EnumA and EnumB which implement EnumDescription, type adapter for EnumDescription can handle its descendants, EnumA and EnumB:

@StaticTypeAdapter(
        targetType = EnumDescription.class,
        serializedType = String.class
)
public class EnumTypeAdapter {

    public static <T extends Enum<T> & EnumDescription> String serialize(@NonNull T value) {
        return value.name();
    }

    @SuppressWarnings("unchecked")
    @NonNull
    public static <T extends Enum<T> & EnumDescription> T deserialize(@NonNull String serialized, @NonNull Class<T> type) {
        return Enum.valueOf(type, serialized);
    }
}

@gfx gfx merged commit 7109c1c into master Nov 3, 2016
@gfx gfx deleted the generic_type_adapters branch November 3, 2016 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant