public static TypeAdapterFactory newEnumTypeHierarchyFactory() {
    return new TypeAdapterFactory() {
      @SuppressWarnings({"rawtypes", "unchecked"})
      public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
        Class<? super T> rawType = typeToken.getRawType();
        if (!Enum.class.isAssignableFrom(rawType) || rawType == Enum.class) {
          return null;
        }
        if (!rawType.isEnum()) {
          rawType = rawType.getSuperclass(); // handle anonymous subclasses
        }
        return (TypeAdapter<T>) new EnumTypeAdapter(rawType);
      }
    };
  }