belen
belen

Reputation: 193

Realm object and deserialize array containing arrays with integer and string for using with Realm

My question is how should I deserialize and how should look the realm object "specialties" that has inside arrays which contains a string and a integer each. I am using realm with gson. I receive a JSON that looks like this: `

{  
"status":200,
   "message":"",
   "data":{  
      "specialties":[  
         [  
            "allergist",
            1
         ],
         [  
            "anesthesiologist",
            1
         ],
         [  
            "cardiologist",
            1
         ],
         [  
            "dermatologist",
            0
         ],
         [  
            "gastroenterologist",
            1
         ],
         [  
            "hematologist",
            1
         ],
         [  
            "nephrologist",
            0
         ],
         ...
      ]
   }
}

`

Upvotes: 2

Views: 794

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81539

Technically your problem is that this

[  
     "allergist",
     1
],

Should totally be an object, something like

{
    "speciality": "allergist",
    "value": 1
}

In which case Realm could easily handle it, but as that's not the case, you'll need to map/convert these [["hello", 2], ["world", 3]] objects into a typed object that you can use as a RealmObject and then persist them. Because currently this is List<List<Object>>.

Upvotes: 2

Christian Melchior
Christian Melchior

Reputation: 20126

In that case, this is not something that Realm can support. Realm currently doesn't support Lists-of-lists unfortunately (I have created an issue for it here: https://github.com/realm/realm-java/issues/2549). But even if that was solved you would still need to find a common type for the lists. Realm does not allow you to save an arbitrary object. In your case you would probably have to convert the Integers to Strings.

A work-around for now could be to convert each of the sub-lists to a typed object. That will require you tow write a custom GSON deserializer for it. There is a guide on how to do that here: https://realm.io/docs/java/latest/#primitive-lists

Upvotes: 1

Related Questions