-
Notifications
You must be signed in to change notification settings - Fork 81
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
Mixed type/ type wildcard/ polymorphism #78
Comments
Hey @ericn37, error you got is related to retrofit. Why not use You will get a collection of different objects depending on the actual |
I don't have much to add to this conversation, except to add a "me too". For example, an object may have a relationship named A different object may express a relationship named The semantics of the relationship named This becomes problematic when you are dereferencing
This class can't be used to deserialize One option is to have
However, that does not work either. You must deserialize a concrete type I am not sure if this is a Retrofit problem or a jsonapi-converter problem, On Thu, Oct 6, 2016 at 11:51 AM, Jasmin Begic notifications@github.com
|
@emetsger It's an jsonapi-converter problem. Other libraries like moshi-jsonapi are able to convert different types. |
https://github.com/faogustavo/JSONApi is also able to parse multiple types. However, is it a tradeoff? |
@jasminb I've tried what you suggested, however, I only got
My Retrofit code: Call<JSONAPIDocument<BaseModel>> obj;
obj = MyService.service.fetchLandingPage(objectId);
obj.enqueue(new Callback<JSONAPIDocument<BaseModel>>() {
@Override
public void onResponse(Call<JSONAPIDocument<BaseModel>> call,
Response<JSONAPIDocument<BaseModel>> response) {
if (response.isSuccessful()) {
MyModel tmp = (MyModel) response.body().get();
listener.handleLandingPage(tmp);
} else {
listener.handleError(response.errorBody().toString());
}
}
@Override
public void onFailure(Call<JSONAPIDocument<BaseModel>> call, Throwable t) {
Log.e(TAG, "onFailure", t);
listener.handleError(null);
}
}); BaseModel: @JsonIgnoreProperties(ignoreUnknown = true)
@Type("base")
public class BaseModel {
@Id
protected String id;
public BaseModel() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
} MyModel: @JsonIgnoreProperties(ignoreUnknown = true)
@Type("mymodel")
public class MyModel extends BaseModel implements Parcelable {
@Relationship("objectbs")
private ArrayList<ObjectB> promos;
@JsonProperty("slug-url")
private String slug;
public MyModel() {
}
public ArrayList<ObjectB> getPromos() {
return promos;
}
public void setPromos(ArrayList<ObjectB> promos) {
this.promos = promos;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(this.promos);
dest.writeString(this.slug);
}
protected MyModel(Parcel in) {
this.promos = in.createTypedArrayList(ObjectB.CREATOR);
this.slug = in.readString();
}
public static final Creator<MyModel> CREATOR = new Creator<MyModel>() {
@Override
public MyModel createFromParcel(Parcel source) {
return new MyModel(source);
}
@Override
public MyModel[] newArray(int size) {
return new MyModel[size];
}
};
} |
Hey, What version of the lib you are using. Support for providing base type is SNAPSHOT only and not yet part of any release. Until new release is out, please use latest snapshot (example in the README). |
Hi @jasminb , I'm using 0.4. Will try the snapshot. Thanks :) |
Hey @ericn37, did 0.5 solve the issue for you? |
Yes, indeed 0.5 did solve the problem, thanks @jasminb |
My server search API returns a list of object of various types e.g. "products", "brands" etc.
I have Product and Brand model both extending BaseModel:
I've tried the following:
but I got the following exception:
The text was updated successfully, but these errors were encountered: