Monday, 15 July 2013

android - Serializing a SparseArray with GSON -


I want to serial a custom Java object, so I SharedPreferences I can use it in another activity. I do not need constant storage, shared references , when my application stops, I erase them. I am currently using GSON for this, but it does not seem to work well with Android's Spurs Array type.

My items:

  Public class partprofile {Private int game ID; // Some more primitive personal sparsarer & lt; Part & gt; InstallParts = new SparseArray & lt; Part & gt; (); // ...} public square part {private string partName; // more primitives}   

Serialization:

 Type  genericType = new TypeToken & lt; PartProfile & gt; () {} .getType (); String Serialized Profiles = Helpers Cyrillous Withs (Installed Perts, Generic Type); Preferences.edit () PutString ("Parts", Cyrilized Profiles) .commit ();   

serializeWithJSON ():

  public static string serializedJason (object o, type generic type) {gsan gsn = new gsan (); Return gson.toJson (o, generic type); }   

Deserialization:

 Type  genericType = new TypeToken & lt; PartProfile & gt; () {} .getType (); Partprofile Parts = GSN.frame Jason (preferences.getString ("Parts", "PARTS_ERROR"), generic type); SparseArray & LT; Part & gt; RetreivedParts = parts.getInstalledParts (); Int key; For (int i = 0; i & lt; retreivedParts.size (); i ++) {key = retreivedParts.keyAt (i); // Exception here: Part part = retreivedParts.get (key); // ...}   

Exception:

  java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap should not be entered in com Could. Mypackage.objects.Part   

I do not understand why GSON wants to insert LinkedTreeMap on my object, I can always use one of my entire programs I do not use Send me the SparseArray & lt; Part & gt; Before switching to , and there was never a problem with it, I have hashmap and lieutenant; Integer, part & gt; Is SparseArrays not supported by GSO, or is there any error in my favor?

EDIT: It seems that SparseArray is correctly deserialized, but not inside the objects. Instead of LinkedTreeMaps , this type should be part . Enter it seems that SparseArray is correctly deserialized, but the object is not within I. Instead of LinkedTreeMaps , Should be part of these types. </ P> </ blockquote> <p> Your observation is correct, because <strong> in SparseArray object </ strong> (not <code> part </ code>), your object GSN will not have any clue to form part in the form, so it will list your list as your notorious internal type It does <code> LinkedTreeMap </ code>. </ P> <p> To resolve this, I think you will not be able to use <code> SparseArray </ code>. Or you can try <Code> retreivedParts.get (key) .toString () </ code>, then use GSN to rearrange the object again. But I do not think it is capable of doing </ p> </ div > </ Html><br /><br /> 
<div style='clear: both;'></div>
</div>
<div class='post-footer'>
<div class='post-footer-line post-footer-line-1'><span class='post-author vcard'>
Posted by
<span class='fn' itemprop='author' itemscope='itemscope' itemtype='http://schema.org/Person'>
<meta content='https://www.blogger.com/profile/05006124498600715544' itemprop='url'/>
<a class='g-profile' href='https://www.blogger.com/profile/05006124498600715544' rel='author' title='author profile'>
<span itemprop='name'>Unknown</span>
</a>
</span>
</span>
<span class='post-timestamp'>
at
<meta content='http://giuseppina-lsgtudhar.blogspot.com/2013/07/android-serializing-sparsearray-with.html' itemprop='url'/>
<a class='timestamp-link' href='https://giuseppina-lsgtudhar.blogspot.com/2013/07/android-serializing-sparsearray-with.html' rel='bookmark' title='permanent link'><abbr class='published' itemprop='datePublished' title='2013-07-15T03:22:00-07:00'>03:22</abbr></a>
</span>
<span class='post-comment-link'>
</span>
<span class='post-icons'>
<span class='item-control blog-admin pid-1004033436'>
<a href='https://www.blogger.com/post-edit.g?blogID=647212097946239830&postID=5740240444118911894&from=pencil' title='Edit Post'>
<img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/>
</a>
</span>
</span>
<div class='post-share-buttons goog-inline-block'>
<a class='goog-inline-block share-button sb-email' href='https://www.blogger.com/share-post.g?blogID=647212097946239830&postID=5740240444118911894&target=email' target='_blank' title='Email This'><span class='share-button-link-text'>Email This</span></a><a class='goog-inline-block share-button sb-blog' href='https://www.blogger.com/share-post.g?blogID=647212097946239830&postID=5740240444118911894&target=blog' onclick='window.open(this.href,

No comments:

Post a Comment