grandstaish/paperparcel

ArrayList of items implementing Parcelable being treated as Serializable and causing error

percula opened this issue ยท 3 comments

I am getting this error:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = java.util.ArrayList)

Looking at the PaperParcel created classes, it appears my ArrayList<object that implements parcelable> is being treated as a serializable, instead of a TypedList. I created an alternative class that doesn't use PaperParcel and it works just fine. Either I'm missing something or this is a bug in PaperParcel. Either way, thanks for the great library!

Please see a sample I created on GitHub.

Hmm, this one is tricky because you're using ArrayList which implements Serializable except that it technically isn't because the items contained inside of it don't also implement Serializable ๐Ÿ˜•

If AObjectPaperParcel used List instead of ArrayList, then it'd work.

Alternatively you could add the following custom TypeAdapter which would handle any ArrayList of Parcelables:

public class ArrayListOfParcelablesTypeAdapter<T extends Parcelable> implements TypeAdapter<ArrayList<T>> {
  @Override public ArrayList<T> readFromParcel(@NonNull Parcel source) {
    ArrayList<T> outList = new ArrayList<>();
    source.readList(outList, ArrayListOfParcelablesTypeAdapter.class.getClassLoader());
    return outList;
  }

  @Override public void writeToParcel(ArrayList<T> value, @NonNull Parcel dest, int flags) {
    dest.writeList(value);
  }
}

Then add it like so:

@ProcessorConfig(adapters = {
    @Adapter(value = ArrayListOfParcelablesTypeAdapter.class, nullSafe = true)
})
public interface PaperParcelConfig {
}

I'm going to add a fix to PaperParcel to ensure that this sort of thing won't compile in the future.

Never mind, unfortunately this can't be fixed because there's no way to tell from the type system that this is not Serializable at runtime ๐Ÿ˜ข

The options for fixing are mentioned in the comment above.

To prevent compilation from working you can also disable support for Serializable like so:

@ProcessorConfig(options = @PaperParcel.Options(allowSerializable = false))
public interface PaperParcelConfig {
}

Sorry about that!

Thanks @grandstaish for the help! I tried both methods and ended up going with changing them to List (actually I'm using Kotlin so MutableList). Now they're treated as parcelables :-).