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

annotate which field can be serialize? #73

Closed
jiqimaogou opened this issue Mar 9, 2015 · 17 comments
Closed

annotate which field can be serialize? #73

jiqimaogou opened this issue Mar 9, 2015 · 17 comments

Comments

@jiqimaogou
Copy link

hi
I just want to parcel just on field of my class, how can I do?

or
auto ignore unparcel field.

I am using dbflow, and every bean class extends basemodel, but basemodel has a modeladapter, it can not be parceled.

@johncarl81
Copy link
Owner

Simply mark what fields you don't want included in the Parcelable with @Transient. This goes for base classes as well.

@jiqimaogou
Copy link
Author

thank you.

but baseModel is in other library which can not be modified, how can I exclude super class field in subClass?

@jiqimaogou
Copy link
Author

but baseModel is in other library which can not be modified, how can I exclude super class field in subClass?

@johncarl81 johncarl81 reopened this Mar 9, 2015
@johncarl81
Copy link
Owner

The best solution currently is to write a ParcelConverter by hand (See http://parceler.org/#custom_serialization). We have a similar issue identified here: #58, with a fix in the works here: #62. Do these describe your problem accurately and do you have any input on the proposed PR?

@jiqimaogou
Copy link
Author

super class(which is can not be modified)

public abstract class BaseModel implements Model {
    private ModelAdapter mModelAdapter;

    public ModelAdapter getModelAdapter() {
        return mModelAdapter;
    }
}

subclass

public class Model extends BaseModel {
     public string name;
}

How can I do exclude BaseModel mModelAdapter field.

@johncarl81
Copy link
Owner

Annotate Model:

@Parcel(converter = ModelConverter.class)
public class Model extends BaseModel {
     public String name;
}

And define ModelConverter:

public static class ModelConverter implements ParcelConverter<Model>{

    @Override
    public void toParcel(Model input, android.os.Parcel parcel) {
        parcel.writeString(input.name);
    }

    @Override
    public Model fromParcel(android.os.Parcel parcel) {
        Model model = new Model();
        model.name = parcel.readString();
        return model;
    }
}

(Until #62 is finished)

@bryant1410
Copy link
Contributor

I think a better approach is to make the model inherit this class instead of BaseModel:

@ParcelClass(value = ModelAdapter.class, annotation = @Parcel(converter = ModelAdapterConverter.class))
public abstract class ParcelableBaseModel extends BaseModel {

}

And having this:

public class ModelAdapterConverter implements ParcelConverter<ModelAdapter> {
    @Override
    public void toParcel(ModelAdapter input, Parcel parcel) {

    }

    @Override
    public ModelAdapter fromParcel(Parcel parcel) {
        return null;
    }
}

Otherwise, it would be nice if BaseModel could have it field as transient.

@Alireza-Farahani
Copy link

@bryant1410 I'm getting "an enclosing instance that contains blahblah.ModelAdapterConverter is required" error in an auto generated dbflow class in. Error lines are marked with a "//HERE" comment in front of them.

public class ModelAdapter$$Parcelable
    implements Parcelable, ParcelWrapper<ModelAdapter>
{

    private ModelAdapter modelAdapter$$0;
    @SuppressWarnings("UnusedDeclaration")
    public final static ModelAdapter$$Parcelable.Creator$$0 CREATOR = new ModelAdapter$$Parcelable.Creator$$0();

    public ModelAdapter$$Parcelable(android.os.Parcel parcel$$0) {
        modelAdapter$$0 = new com.shaya.poinila.android.data.database.PoinilaDataBase.ModelAdapterConverter().fromParcel(parcel$$0); // HERE!
    }
    public ModelAdapter$$Parcelable(ModelAdapter modelAdapter$$1) {
        modelAdapter$$0 = modelAdapter$$1;
    }
    @Override
    public void writeToParcel(android.os.Parcel parcel$$1, int flags) {
        new com.shaya.poinila.android.data.database.PoinilaDataBase.ModelAdapterConverter().toParcel(modelAdapter$$0, parcel$$1);  // HERE!
    }

    @Override
    public int describeContents() {
        return  0;
    }

    @Override
    public ModelAdapter getParcel() {
        return modelAdapter$$0;
    }

    public final static class Creator$$0 implements Creator<ModelAdapter$$Parcelable> {
        @Override
        public ModelAdapter$$Parcelable createFromParcel(android.os.Parcel parcel$$2) {
            return new ModelAdapter$$Parcelable(parcel$$2);
        }

        @Override
        public ModelAdapter$$Parcelable[] newArray(int size) {
            return new ModelAdapter$$Parcelable[size] ;
        }
    }
}

Any idea how to resolve this?

@bryant1410
Copy link
Contributor

DBFlow 3.0.0-beta1 BaseModel is now Parcelable. You can just use this version.

@Alireza-Farahani
Copy link

@bryant1410 Tnx but I'm not very sure about using a beta version.
Did your way really work for you? That seems far better than writing lots of boilerplate parcelConverter code

@bryant1410
Copy link
Contributor

@khafan I haven't used it. The other option that comes to my mind is to fork the last stable version and make BaseModel Parcelable. Just apply this change

@johncarl81
Copy link
Owner

@bryant1410, I'm not seeing BaseModel implementing Parcelable, but with that change you linked it does mark fields with transient that Parceler should respect and would make the given class extending BaseModel compatible with Parceler out of the box (without a converter as you and I mentioned).

@bryant1410
Copy link
Contributor

Yeah! Sorry, I got confused! I meant that, to make it work with Parceler out of the box

@johncarl81
Copy link
Owner

👍

@johncarl81
Copy link
Owner

@khafan, we do have another option if you want to ignore the base class entirely, if that is what you want. You can use the analyze field to specify which specific classes to analyze. Classes not mentioned will not be included in the generated Parcelable in the inheritance hierarchy:

@Parcel(analyze=Model.class)  // Will not analyze BaseModel
public class Model extends BaseModel {
     public String name;
}

http://parceler.org/#skipping_analysis

@bryant1410
Copy link
Contributor

@johncarl81 good tip!

@Alireza-Farahani
Copy link

@johncarl81 Awesome! Completely satisfy my conditions. Will try that 👍

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

No branches or pull requests

4 participants