public class MarvelResult<T> {

    private int mOffset;
    private int mTotal;
    private List<T> mEntries;
    private String mAttribution;

    public MarvelResult() {
        mOffset = 0;
        mTotal = 0;
        mEntries = new ArrayList<>();
        mAttribution = "";
    }

    public int getOffset() {
        return mOffset;
    }

    public void setOffset(int offset) {
        mOffset = offset;
    }

    public int getTotal() {
        return mTotal;
    }

    public void setTotal(int total) {
        mTotal = total;
    }

    public int getCount() {
        return mEntries.size();
    }

    public List<T> getEntries() {
        return mEntries;
    }

    public void setEntries(@NonNull List<T> entries) {
        mEntries = entries;
    }

    public String getAttribution() {
        return mAttribution;
    }

    public void setAttribution(@NonNull String attribution) {
        mAttribution = attribution;
    }
} public class CharacterVO implements Serializable {

    private long mId;
    private String mName;
    private String mDescription;
    private String mThumbnail;
    private String mImage;
    private List<SectionVO> mComics;
    private List<SectionVO> mSeries;
    private List<SectionVO> mStories;
    private List<SectionVO> mEvents;
    private String mDetail;
    private String mWiki;
    private String mComicLink;

    public CharacterVO() {
    }

    public long getId() {
        return mId;
    }

    public void setId(long id) {
        mId = id;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public String getDescription() {
        return mDescription;
    }

    public void setDescription(String description) {
        mDescription = description;
    }

    public String getThumbnail() {
        return mThumbnail;
    }

    public void setThumbnail(String thumbnail) {
        mThumbnail = thumbnail;
    }

    public String getImage() {
        return mImage;
    }

    public void setImage(String image) {
        mImage = image;
    }

    public List<SectionVO> getComics() {
        return mComics;
    }

    public void setComics(List<SectionVO> comics) {
        mComics = comics;
    }

    public List<SectionVO> getSeries() {
        return mSeries;
    }

    public void setSeries(List<SectionVO> series) {
        mSeries = series;
    }

    public List<SectionVO> getStories() {
        return mStories;
    }

    public void setStories(List<SectionVO> stories) {
        mStories = stories;
    }

    public List<SectionVO> getEvents() {
        return mEvents;
    }

    public void setEvents(List<SectionVO> events) {
        mEvents = events;
    }

    public String getDetail() {
        return mDetail;
    }

    public void setDetail(String detail) {
        mDetail = detail;
    }

    public String getWiki() {
        return mWiki;
    }

    public void setWiki(String wiki) {
        mWiki = wiki;
    }

    public String getComicLink() {
        return mComicLink;
    }

    public void setComicLink(String comicLink) {
        mComicLink = comicLink;
    }

    @Override
    public String toString() {
        return "Character{" + mId + ", '" + mName + "'}";
    }
} public class SectionVO implements Serializable {

    public static final int TYPE_COMIC = 0;
    public static final int TYPE_SERIES = 1;
    public static final int TYPE_STORY = 2;
    public static final int TYPE_EVENT = 3;

    @IntDef({TYPE_COMIC, TYPE_SERIES, TYPE_STORY, TYPE_EVENT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Type {
    }

    private long mId;
    private String mTitle;
    private String mThumbnail;
    private String mImage;

    public SectionVO() {
    }

    public long getId() {
        return mId;
    }

    public void setId(long id) {
        mId = id;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public String getThumbnail() {
        return mThumbnail;
    }

    public void setThumbnail(String thumbnail) {
        mThumbnail = thumbnail;
    }

    public String getImage() {
        return mImage;
    }

    public void setImage(String image) {
        mImage = image;
    }

    @Override
    public String toString() {
        return "Section{" + mId + ", '" + mTitle + "'}";
    }
} public class CharacterData {

    public long id;
    public String name;
    public String description;
    public Image thumbnail;
    public SectionList comics;
    public SectionList series;
    public SectionList stories;
    public SectionList events;
    public Url[] urls;

    public String getThumbnail() {
        return Image.getUrl(thumbnail.path, Image.SIZE_STANDARD_LARGE, thumbnail.extension);
    }

    public String getImage() {
        return Image.getUrl(thumbnail.path, Image.SIZE_STANDARD_INCREDIBLE, thumbnail.extension);
    }

} public class CharacterDataContainer {
    public int offset;
    public int total;
    public CharacterData[] results;
} public class CharacterDataWrapper {
    public String attributionText;
    public CharacterDataContainer data;
} public class SectionData {

    public long id;
    public String title;
    public Image thumbnail;

    public String getThumbnail() {
        if (thumbnail == null) {
            return null;
        }
        return Image.getUrl(thumbnail.path, Image.SIZE_PORTRAIT_XLARGE, thumbnail.extension);
    }

    public String getImage() {
        if (thumbnail == null) {
            return null;
        }
        return Image.getUrl(thumbnail.path, Image.SIZE_DETAIL, thumbnail.extension);
    }

} public class SectionDataContainer {
    public int offset;
    public int total;
    public SectionData[] results;
} public class SectionDataWrapper {
    public String attributionText;
    public SectionDataContainer data;
} public class SectionSummary {

    public String resourceURI;
    public String name;

    public long getId() {
        String id = getLastPathSegment(resourceURI);
        try {
            return Long.parseLong(id);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    private String getLastPathSegment(String uri) {
        if (uri == null) {
            return "";
        }
        int indexOf = uri.lastIndexOf("/");
        return uri.substring(indexOf + 1);
    }

} public class Url {
    public static final String TYPE_DETAIL = "detail";
    public static final String TYPE_WIKI = "wiki";
    public static final String TYPE_COMICLINK = "comiclink";
    public String type;
    public String url;
}
import com.andremion.heroes.api.MarvelResult;
import com.andremion.heroes.api.data.CharacterVO;
import com.andremion.heroes.api.data.SectionVO;
import com.andremion.heroes.api.json.CharacterData;
import com.andremion.heroes.api.json.CharacterDataContainer;
import com.andremion.heroes.api.json.CharacterDataWrapper;
import com.andremion.heroes.api.json.SectionData;
import com.andremion.heroes.api.json.SectionDataContainer;
import com.andremion.heroes.api.json.SectionDataWrapper;
import com.andremion.heroes.api.json.SectionSummary;
import com.andremion.heroes.api.json.Url;
import java.util.ArrayList;
import java.util.List;
/*
 * Copyright (c) 2017. André Mion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.heroes.api.util;



// TODO: 06/10/2016 Parse json to model in serialization phase.
public class DataParser {

    public static MarvelResult<CharacterVO> parse(CharacterDataWrapper dataWrapper) {
        MarvelResult<CharacterVO> result = new MarvelResult<>();
        CharacterDataContainer dataContainer = dataWrapper.data;
        if (dataContainer != null) {
            result.setOffset(dataContainer.offset);
            result.setTotal(dataContainer.total);
            CharacterData[] results = dataContainer.results;
            if (results != null) {
                List<CharacterVO> characterList = new ArrayList<>(results.length);
                for (CharacterData characterData : results) {
                    CharacterVO character = new CharacterVO();
                    character.setId(characterData.id);
                    character.setName(characterData.name);
                    character.setDescription(characterData.description);
                    character.setThumbnail(characterData.getThumbnail());
                    character.setImage(characterData.getImage());
                    for (Url url : characterData.urls) {
                        if (Url.TYPE_DETAIL.equals(url.type)) {
                            character.setDetail(url.url);
                        } else if (Url.TYPE_WIKI.equals(url.type)) {
                            character.setWiki(url.url);
                        } else if (Url.TYPE_COMICLINK.equals(url.type)) {
                            character.setComicLink(url.url);
                        }
                    }
                    character.setComics(parseSection(characterData.comics.items));
                    character.setSeries(parseSection(characterData.series.items));
                    character.setStories(parseSection(characterData.stories.items));
                    character.setEvents(parseSection(characterData.events.items));
                    characterList.add(character);
                }
                result.setEntries(characterList);
            }
        }
        result.setAttribution(dataWrapper.attributionText);
        return result;
    }

    private static List<SectionVO> parseSection(SectionSummary[] items) {