JonB
JonB

Reputation: 814

Deserialising json, hoisting subclass objects to top level instead of nested classes

I'm deserialising json response from an api, I have a working code, but the only thing of my concern are the elements within the Object class, how can I eliminate the Object class entirely and just return the elements directly from the ListUsers class?

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class ListUsers{

    @SerializedName("meta")
    @Expose
    private Meta meta;
    @SerializedName("objects")
    @Expose
    private List<Objects> objects = null;

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public List<Objects> getObjects() {
        return objects;
    }

    public void setObjects(List<Objects> objects) {
        this.objects = objects;
    }

    private class Meta {
        @SerializedName("limit")
        @Expose
        private int limit;
        @SerializedName("offset")
        @Expose
        private int offset;
        @SerializedName("total_count")
        @Expose
        private int totalCount;

        public int getLimit() {
            return limit;
        }

        public void setLimit(int limit) {
            this.limit = limit;
        }

        public int getOffset() {
            return offset;
        }

        public void setOffset(int offset) {
            this.offset = offset;
        }

        public int getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Meta)) return false;
            Meta meta = (Meta) o;
            return getLimit() == meta.getLimit() &&
                    getOffset() == meta.getOffset() &&
                    getTotalCount() == meta.getTotalCount();
        }

        @Override
        public int hashCode() {
            return java.util.Objects.hash(getLimit(), getOffset(), getTotalCount());
        }

        @Override
        public String toString() {
            return "Meta{" +
                    "limit=" + limit +
                    ", offset=" + offset +
                    ", totalCount=" + totalCount +
                    '}';
        }
    }

    private class Objects {
        @SerializedName("address_one")
        @Expose
        public String addressOne;
        @SerializedName("address_three")
        @Expose
        public String addressThree;
        @SerializedName("address_two")
        @Expose
        public String addressTwo;
        @SerializedName("country_code")
        @Expose
        public String countryCode;
        @SerializedName("cs_domain_id")
        @Expose


        public String getAddressOne() {
            return addressOne;
        }

        public void setAddressOne(String addressOne) {
            this.addressOne = addressOne;
        }

        public String getAddressThree() {
            return addressThree;
        }

        public void setAddressThree(String addressThree) {
            this.addressThree = addressThree;
        }

        public String getAddressTwo() {
            return addressTwo;
        }

        public void setAddressTwo(String addressTwo) {
            this.addressTwo = addressTwo;
        }

        public String getCountryCode() {
            return countryCode;
        }

        public void setCountryCode(String countryCode) {
            this.countryCode = countryCode;
        }
    }
}

Upvotes: 0

Views: 62

Answers (1)

DwB
DwB

Reputation: 38328

Write an Adapter (this is a wikipedia link).

  1. Create a class hierarchy to represent the JSON. Success, this is your example source.
  2. Create an adapter class that wraps your JSON representation class. This will expose the blam0-blah stuff (in your case, the nicely named Objects List) is some fun and creative way (perhaps a getAddressLine1 method and such).

Depending on how the Adapter will be used, you might parse the List<Objects> into a Map<Objects> for easier use.

Upvotes: 1

Related Questions