2

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
         ],
         ...
      ]
   }
}

`

belen
  • 183
  • 2
  • 19
  • Possible duplicate of [Google Gson - deserialize list object? (generic type)](http://stackoverflow.com/questions/5554217/google-gson-deserialize-listclass-object-generic-type) – Rohit Arya Apr 01 '16 at 14:08
  • Does `specialties` really contains a List of Lists or are the inner lists Objects? If they are indeed Lists, the only supported type for both of them would be `List` since they contain both a String and an Integer. – Christian Melchior Apr 01 '16 at 14:20
  • It is really a list of list, for me it is the first time that I see different primitives in a list. I just copied and pasted the JSON from the response – belen Apr 01 '16 at 14:22

2 Answers2

2

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

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
1

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

Christian Melchior
  • 19,978
  • 5
  • 62
  • 53