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

[Android] Writing to /Reading from Parcelable ignores superclass #602

Closed
yomik opened this issue Aug 2, 2016 · 0 comments
Closed

[Android] Writing to /Reading from Parcelable ignores superclass #602

yomik opened this issue Aug 2, 2016 · 0 comments
Milestone

Comments

@yomik
Copy link
Contributor

yomik commented Aug 2, 2016

With the following sample json files :
a.json :

{
  "type": "object",
  "properties": {
      "foo": {
          "type": "string"
      }   
}

and b.json :

{
  "type": "object",
  "extends" : {
    "$ref": "a.json"
  },
  "properties": {
    "bar": {
      "type": "integer"
    }
  }
}

The generated classes will look like :

A.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "foo"
})
public class A implements Parcelable
{
    @JsonProperty("foo")
    private String foo;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    public final static Parcelable.Creator<A> CREATOR = new Creator<A>() {

        @SuppressWarnings({
            "unchecked"
        })
        public A createFromParcel(Parcel in) {
            A instance = new A();
            instance.foo = ((String) in.readValue((String.class.getClassLoader())));
            instance.additionalProperties = ((Map<String, Object> ) in.readValue((Map.class.getClassLoader())));
            return instance;
        }

        public A[] newArray(int size) {
            return (new A[size]);
        }

    }
    ;

    /**
     * 
     * @return
     *     The foo
     */
    @JsonProperty("foo")
    public String getFoo() {
        return foo;
    }

    /**
     * 
     * @param foo
     *     The foo
     */
    @JsonProperty("foo")
    public void setFoo(String foo) {
        this.foo = foo;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(foo);
        dest.writeValue(additionalProperties);
    }

    public int describeContents() {
        return  0;
    }

}

and B.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "bar"
})
public class B
    extends A
    implements Parcelable
{

    @JsonProperty("bar")
    private long bar;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    public final static Parcelable.Creator<B> CREATOR = new Creator<B>() {


        @SuppressWarnings({
            "unchecked"
        })
        public B createFromParcel(Parcel in) {
            B instance = new B();
            instance.bar = ((long) in.readValue((long.class.getClassLoader())));
            instance.additionalProperties = ((Map<String, Object> ) in.readValue((Map.class.getClassLoader())));
            return instance;
        }

        public B[] newArray(int size) {
            return (new B[size]);
        }

    }
    ;

    /**
     * 
     * @return
     *     The bar
     */
    @JsonProperty("bar")
    public long getBar() {
        return bar;
    }

    /**
     * 
     * @param bar
     *     The bar
     */
    @JsonProperty("bar")
    public void setBar(long bar) {
        this.bar = bar;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(bar);
        dest.writeValue(additionalProperties);
    }

    public int describeContents() {
        return  0;
    }

}

which means that when trying to Parcelable a B instance, the information contained in A is lost in the process since writeToParcel does not call its parent, neither does createFromParcel read the superclass values.

For such cases, I currently need to create a dummy A object filled in with B info, then marshalling my dummy A then B. Unmarshalling is the opposite process, with unmarshalling of A then B, then put back the properties from A into B.

It would be nice if the generated Parcelable code could handle inherited classes, though it doesn't look like and easy task

yomik added a commit to yomik/jsonschema2pojo that referenced this issue Jun 1, 2017
yomik added a commit to yomik/jsonschema2pojo that referenced this issue Jun 5, 2017
yomik added a commit to yomik/jsonschema2pojo that referenced this issue Jun 6, 2017
@joelittlejohn joelittlejohn added this to the 0.4.36 milestone Jun 6, 2017
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

2 participants