android - Custom deserializer for RealmObject -


for learning purposes i'm creating android app using realm , edinburg festival api. it's going pretty except 1 problem.

i'm using following convert retrieved json realmobjects:

public void onresponse(final string response) {     realm.executetransactionasync(new realm.transaction(){         @override         public void execute(realm realm) {             // update our realm results             parseimages();             realm.createorupdateallfromjson(festival.class, response);         }     } } 

this works fine except 1 field, images. image part of json:

"images": {         "031da8b4bad1360eddea87e8820615016878b183": {         "hash": "031da8b4bad1360eddea87e8820615016878b183",         "orientation": "landscape",         "type": "hero",         "versions": {             "large-1024": {             "height": 213,             "mime": "image/png",             "type": "large-1024",         }         "width": 1024     } } 

the problem here hash inside image object. have no clue how handle this. hash different every festival. possible to make custom json deserializer in realmobject?

last code sample current model:

public class festival extends realmobject {     @primarykey     public string title;     realmlist<image> images;     public string description_teaser;     public string description;     public string genre;     public string age_category;     public string website;     public realmlist<performance> performances;     public int votes; } 

i'm aware pk not optimal still testing images working , needed set pk migrating.

any tips welcome, cheers :)

update

added image model:

public class image extends realmobject {     public string hash;     public string orientation;     public string type;     realmlist<version> versions; } 

update 2

my attempt parse images before calling realm.createorupdateallfromjson(festival.class, response);

private void parseimages(string jsonstring) throws jsonexception {     jsonarray jsonarr = new jsonarray(jsonstring);     for(int = 0; < jsonarr.length(); i++){         jsonobject jsonobj = jsonarr.getjsonobject(i);         jsonobject images = (jsonobject)jsonobj.get("images");         iterator<string> iter = images.keys();         while (iter.hasnext()) {             string key = iter.next();             try {                 jsonobject value = json.get(key);                 realm.createorupdateobjectfromjson(image.class,value);             } catch (jsonexception e) {                 // went wrong!             }         }     } } 

update 3

i created function cleans broken json api. ain't nice works now. removes hashes , wierd versions , places them both in array. i'm sure more efficiently written i'll go can move on rest of app now. see own answer.

my own temporary solution:

    /**      * function fix json coming festival api      * bit more complicated needs realm not yet support @serializedname      * removes "large-1024" (and simllar) object , places versions in json version array      * removes hashes , creates , images array. jsonarray can parsed :)      *      * @param jsonstring result string festival api      * @return jsonarray fixed json in form of jsonarray      * @throws jsonexception      */     private jsonarray cleanupjson(string jsonstring) throws jsonexception {         jsonarray json = new jsonarray(jsonstring);         for(int = 0; < json.length(); i++){             // store json image objects in here can remove hashes             map<string,jsonobject> images = new hashmap<>();             jsonobject festivaljson = json.getjsonobject(i);             jsonobject imagesjson = (jsonobject)festivaljson.get("images");             // iterate each hash inside images             iterator<string> hashiter = imagesjson.keys();             while (hashiter.hasnext()) {                 string key = hashiter.next();                 try {                     final jsonobject image = imagesjson.getjsonobject(key);                      // remove version parents , map them version                     map<string, jsonobject> versions = new hashmap<>();                     jsonobject versionsjsonobject = image.getjsonobject("versions");                      // iterate possible version , map add hashmap                     iterator<string> versioniter = versionsjsonobject.keys();                     while(versioniter.hasnext()){                         string currentversion = versioniter.next();                         versions.put(currentversion,versionsjsonobject.getjsonobject(currentversion));                     }                      // use hashmap modify json array of version                     // can't done in iterator because concurrent error                     image.remove("versions");                     iterator hashmapiter = versions.entryset().iterator();                     jsonarray versionjsonarray = new jsonarray();                     while( hashmapiter.hasnext() ){                         map.entry pair = (map.entry)hashmapiter.next();                         versionjsonarray.put(pair.getvalue());                     }                     image.put("versions",versionjsonarray);                     log.d(log_tag,image.tostring());                 } catch (jsonexception e) {                     e.printstacktrace();                 }                 images.put(key,imagesjson.getjsonobject(key));             }             // let's rid of hashes             iterator hashmapiter = images.entryset().iterator();             jsonarray imagesjsonarray = new jsonarray();             while( hashmapiter.hasnext() ){                 map.entry pair = (map.entry)hashmapiter.next();                 imagesjsonarray.put(pair.getvalue());             }             festivaljson.put("images", imagesjsonarray);         }         return json;     } 

hope helps :) sure ain't neat.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -