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

Support Nested Models #62

Open
idoshamun opened this issue Dec 22, 2014 · 7 comments
Open

Support Nested Models #62

idoshamun opened this issue Dec 22, 2014 · 7 comments

Comments

@idoshamun
Copy link

I'm trying to define a model with an array of models inside it with the Android SDK and I'm struggling with a casting problem from HashMap to my model.
Let's take for example a model called Feed that contains an array of Posts (which is a model also) and a link to the next page (a simple string).
When I try to get a Feed object with a custom static method using the JsonObjectParser, it throws an exception that it can't cast HashMap to Post.
Right now, the only solution I came up with is to overload the setter of Posts with ArrayList<HasMap<String, ? extends Object>> as a parameter instead of ArrayList<Post>.

@bajtos
Copy link
Contributor

bajtos commented Jan 13, 2015

We should hopefully get this for free once we replace our hand-written JSON parser with a proper library supporting custom annotations - see #61.

@firaskafri
Copy link

Any updates on this?

@techscientist
Copy link

Any updates on this? this would be a crucial feature for many apps

@jamesw6811
Copy link

+1 -- This is absolutely essential to our use-case.

@azubieta
Copy link

Any updates on this topic ? It's there another way of doing the same ?

@jjhesk
Copy link

jjhesk commented Jul 20, 2016

+1 when is this done?!!!!

Using gson ( "com.google.code.gson:gson:2.6.2") and it has done it.. Please let me know thxx

@JoshDavisMP
Copy link

Until the feature gets added, I've found the potential temporary workaround. I haven't thoroughly tested it yet, but it seems to get me at least part of the way there:

public void setMyProperty(Map<String,? extends Object> item) {
    RestAdapter adapter = this.getRepository().getAdapter();
    ChildTypeRepository repository = adapter.createRepository(ChildTypeRepository.class);
    this.myProperty = (ChildModelType) repository.createObject(item);
}

public void setMyListProperty(List<Map<String,? extends Object>> hashList) {
    for(Map<String,? extends Object> item : hashList) {
        this.myListProperty.add((ThisType) this.getRepository().createObject(item));
    }
}

By calling the repository createObject method it calls the same method stack used to initialize your model objects. For the first example I'm referencing a different type so I need to get an adapter (reusing the current repository's so don't use it to make additional API calls) and repository for that model type. For the list example I'm creating a recursive list of the same type as the parent model so I can just use its repository instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests