Git Product home page Git Product logo

Comments (12)

johncarl81 avatar johncarl81 commented on August 10, 2024

Parceler actually doesn't support Object as it is not a type represented within the Parcel (yes, there is the writeValue(Object) method, but internally it does an instanceof to determine what type to write).

It seems like your last example should work. I wonder if the ParcelClass converter parameter is not being picked up. I'll look into this.

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Found a couple of bugs with the approach, but I found one that worked. Try this:

@Parcel
public class ParcelableObject {

    @ParcelProperty("test")
    @ParcelPropertyConverter(ParcelableInnerIntegerConverter.class)
    ParcelableInner<Integer> parcelableInner;

    public static class ParcelableInner<T> {
        T i;
    }

    public static class ParcelableInnerIntegerConverter implements ParcelConverter<ParcelableInner<Integer>> {

        @Override
        public void toParcel(ParcelableInner<Integer> input, android.os.Parcel parcel) {

        }

        @Override
        public ParcelableInner<Integer> fromParcel(android.os.Parcel parcel) {
            return null;
        }
    }
}

Currently Parceler doesn't allow you to annotate a field with @ParcelPropertyConverter without an accompanying @ParcelProperty annotation. I will be pushing out a fix for this shortly.

from parceler.

fabioCollini avatar fabioCollini commented on August 10, 2024

Thanks for your answer, your example works but it's a bit verbose, two annotations every time you need to use the inner class.

Another working solution is this:

@Parcel
@ParcelClass(value = ParcelableObject.ParcelableInner.class, converter = ParcelableObject.ParcelableInnerIntegerConverter.class)
public class ParcelableObject {

    ParcelableInner parcelableInner;

    public static class ParcelableInner<T> {
        @Transient
        T i;
    }

    public static class ParcelableInnerIntegerConverter implements ParcelConverter<ParcelableInner<Integer>> {

        @Override
        public void toParcel(ParcelableInner<Integer> input, android.os.Parcel parcel) {

        }

        @Override
        public ParcelableInner<Integer> fromParcel(android.os.Parcel parcel) {
            return null;
        }
    }
}

But I don't like it because the field ParcelableInner is not defined as a generic (it should be ParcelableInner. Is this a bug that you already fixed?

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Yes, @fabioCollini, I fixed the requirement to have a @ParcelProperty with a @ParcelPropertyConverter (909b4ab). This should work with 0.2.14-SNAPSHOT:

@Parcel
public class ParcelableObject {

    @ParcelPropertyConverter(ParcelableInnerIntegerConverter.class)
    ParcelableInner<Integer> parcelableInner;

    public static class ParcelableInner<T> {
        T i;
    }

    public static class ParcelableInnerIntegerConverter implements ParcelConverter<ParcelableInner<Integer>> {

        @Override
        public void toParcel(ParcelableInner<Integer> input, android.os.Parcel parcel) {

        }

        @Override
        public ParcelableInner<Integer> fromParcel(android.os.Parcel parcel) {
            return null;
        }
    }
}

Give it a try and please let me know if it works for you.

from parceler.

fabioCollini avatar fabioCollini commented on August 10, 2024

Now using ParcelPropertyConverter it works correctly but there is still an error using ParcelClass:

@Parcel
@ParcelClass(value = ParcelableObject.ParcelableInner.class, converter = ParcelableObject.ParcelableInnerIntegerConverter.class)
public class ParcelableObject {

    ParcelableInner<Integer> parcelableInner;

    public static class ParcelableInner<T> {
        T i;
    }

    public static class ParcelableInnerIntegerConverter implements ParcelConverter<ParcelableInner<Integer>> {

        @Override
        public void toParcel(ParcelableInner<Integer> input, android.os.Parcel parcel) {

        }

        @Override
        public ParcelableInner<Integer> fromParcel(android.os.Parcel parcel) {
            return null;
        }
    }
}

Exception in thread "pool-556-thread-1" org.parceler.ParcelerRuntimeException: Unable to find appropriate Parcel method to write it.cosenonjaviste.ParcelableObject.ParcelableInner

Could you fix this example too?
Thanks again!

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

That example is unfortunately not going to work with some limitations of annotations. Parceler does a deep equality between classes, thus:

ParcelableInner.class != ParcelableInner<Integer>.class

This would work if we could define the annotation like so:

@ParcelClass(value = ParcelableObject.ParcelableInner<Integer>.class, converter = ParcelableObject.ParcelableInnerIntegerConverter.class)

For instance, this should work (with the latest SNAPSHOT... I had to fix a bug related to this):

@Parcel
@ParcelClass(value = ParcelableObject.ParcelableInnerInteger.class, converter = ParcelableObject.ParcelableInnerIntegerConverter.class)
public class ParcelableObject {

    ParcelableInnerInteger parcelableInner;

    public static class ParcelableInnerInteger extends ParcelableInner<Integer>{}

    public static class ParcelableInner<T> {
        T i;
    }

    public static class ParcelableInnerIntegerConverter implements ParcelConverter<ParcelableInnerInteger> {

        @Override
        public void toParcel(ParcelableInnerInteger input, android.os.Parcel parcel) {

        }

        @Override
        public ParcelableInnerInteger fromParcel(android.os.Parcel parcel) {
            return null;
        }
    }
}

from parceler.

fabioCollini avatar fabioCollini commented on August 10, 2024

Are you sure? Executing Parcels.wrap(new ParcelableObject.ParcelableInner()) works, Parcels.REPOSITORY contains the right data. Why don't you just ignore generics when a field is defined using generic types?

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

@fabioCollini, can you clarify? How did you annotate your ParcelableInner class?

from parceler.

fabioCollini avatar fabioCollini commented on August 10, 2024

This is an example where I am using it, this is the generic class:

https://github.com/fabioCollini/CoseNonJavisteAndroidApp/blob/master/javaLib/src/main/java/it/cosenonjaviste/mvp/base/optional/OptionalList.java

This is the converter:

https://github.com/fabioCollini/CoseNonJavisteAndroidApp/blob/master/androidMvp/src/main/java/it/cosenonjaviste/lib/mvp/parceler/OptionalListConverter.java

I am using it in this class (removing the generic works):

https://github.com/fabioCollini/CoseNonJavisteAndroidApp/blob/master/mvp/src/main/java/it/cosenonjaviste/mvp/post/PostListModel.java

Another attempt is to extends the class:

https://github.com/fabioCollini/CoseNonJavisteAndroidApp/blob/master/mvp/src/main/java/it/cosenonjaviste/mvp/twitter/TweetListModel.java

It works but it uses it as a serializable object instead of the converter (maybe is this a bug?).
And a question: is there a guide to configure parceler workspace?

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Using a Serializable object is not ideal, but if it's the only way to transfer your Throwable you may be out of luck. Could you annotate your Throwable with @Parcel?

What sort of guide are you looking for? I was thinking about putting together a proper website. Currently the github README is the best place for documentation.

from parceler.

fabioCollini avatar fabioCollini commented on August 10, 2024

You are right about the throwable, but I defined a converter for the base class. I am extending this class but it write the field of the base class in the parcel as a serializable (the converter on the base class is ignored).

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Looks like everything is in order... you good to close or did we not address this?

from parceler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.