Skip to content

Support Nested Models #62

Open
Open

Description

@idoshamun

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>.

Activity

  1. bajtos commented on Jan 13, 2015

    @bajtos
    Contributor

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

  2. firaskafri commented on Dec 2, 2015

    @firaskafri

    Any updates on this?

  3. techscientist commented on Dec 30, 2015

    @techscientist

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

  4. jamesw6811 commented on Jan 16, 2016

    @jamesw6811

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

  5. azubieta commented on Apr 20, 2016

    @azubieta

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

  6. jjhesk commented on Jul 20, 2016

    @jjhesk

    +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

  7. JoshDavisMP commented on Jan 31, 2017

    @JoshDavisMP

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions