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'>
<span class='fn' itemprop='author' itemscope='itemscope' itemtype='http://schema.org/Person'>
<meta content='https://www.blogger.com/profile/05006124498600715544' itemprop='url'/>
<a 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?m=1' 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>
</div>
<div class='post-footer-line post-footer-line-2'>
<div class='mobile-link-button goog-inline-block' id='mobile-share-button'>
<a href='javascript:void(0);'>Share</a>
</div>
</div>
</div>
</div>
<div class='comments' id='comments'>
<a name='comments'></a>
<h4>No comments:</h4>
<div id='Blog1_comments-block-wrapper'>
<dl class='avatar-comment-indent' id='comments-block'>
</dl>
</div>
<p class='comment-footer'>
<div class='comment-form'>
<a name='comment-form'></a>
<h4 id='comment-post-message'>
<a href='javascript:void(0)' id='Blog1_comment-editor-toggle-link'>Post a Comment</a></h4>
<p>
</p>
<a href='https://www.blogger.com/comment/frame/647212097946239830?po=5740240444118911894&hl=en-GB&saa=85391&origin=https://giuseppina-lsgtudhar.blogspot.com&m=1' id='comment-editor-src'></a>
<iframe allowtransparency='true' class='blogger-iframe-colorize blogger-comment-from-post' frameborder='0' height='410px' id='comment-editor' name='comment-editor' src='' style='display: none' width='100%'></iframe>
<script src='https://www.blogger.com/static/v1/jsbin/1212307205-comment_from_post_iframe.js' type='text/javascript'></script>
<script type='text/javascript'>
      BLOG_CMT_createIframe('https://www.blogger.com/rpc_relay.html');
    </script>
</div>
</p>
</div>
</div>
</div>
</div>
</div>
<div class='blog-pager' id='blog-pager'>
<div class='mobile-link-button' id='blog-pager-newer-link'>
<a class='blog-pager-newer-link' href='https://giuseppina-lsgtudhar.blogspot.com/2013/07/how-to-split-doxygen-mainpage.html?m=1' id='Blog1_blog-pager-newer-link' title='Newer Post'>‹</a>
</div>
<div class='mobile-link-button' id='blog-pager-older-link'>
<a class='blog-pager-older-link' href='https://giuseppina-lsgtudhar.blogspot.com/2013/07/language-agnostic-how-to-learn-reading.html?m=1' id='Blog1_blog-pager-older-link' title='Older Post'>›</a>
</div>
<div class='mobile-link-button' id='blog-pager-home-link'>
<a class='home-link' href='https://giuseppina-lsgtudhar.blogspot.com/?m=1'>Home</a>
</div>
<div class='mobile-desktop-link'>
<a class='home-link' href='https://giuseppina-lsgtudhar.blogspot.com/2013/07/android-serializing-sparsearray-with.html?m=0'>View web version</a>
</div>
</div>
<div class='clear'></div>
</div></div>
</div>
</div>
<div class='column-left-outer'>
<div class='column-left-inner'>
<aside>
</aside>
</div>
</div>
<div class='column-right-outer'>
<div class='column-right-inner'>
<aside>
</aside>
</div>
</div>
</div>
<div style='clear: both'></div>
<!-- columns -->
</div>
<!-- main -->
</div>
</div>
<div class='main-cap-bottom cap-bottom'>
<div class='cap-left'></div>
<div class='cap-right'></div>
</div>
</div>
<footer>
<div class='footer-outer'>
<div class='footer-cap-top cap-top'>
<div class='cap-left'></div>
<div class='cap-right'></div>
</div>
<div class='fauxborder-left footer-fauxborder-left'>
<div class='fauxborder-right footer-fauxborder-right'></div>
<div class='region-inner footer-inner'>
<div class='foot no-items section' id='footer-1'></div>
<!-- outside of the include in order to lock Attribution widget -->
<div class='foot section' id='footer-3' name='Footer'><div class='widget Profile' data-version='1' id='Profile1'>
<h2>About Me</h2>
<div class='widget-content'>
<dl class='profile-datablock'>
<dt class='profile-data'>
<a class='profile-name-link g-profile' href='https://www.blogger.com/profile/05006124498600715544' rel='author' style='background-image: url(//www.blogger.com/img/logo-16.png);'>
Unknown
</a>
</dt>
</dl>
<a class='profile-link' href='https://www.blogger.com/profile/05006124498600715544' rel='author'>View my complete profile</a>
<div class='clear'></div>
</div>
</div><div class='widget Attribution' data-version='1' id='Attribution1'>
<div class='widget-content' style='text-align: center;'>
Powered by <a href='https://www.blogger.com' target='_blank'>Blogger</a>.
</div>
<div class='clear'></div>
</div></div>
</div>
</div>
<div class='footer-cap-bottom cap-bottom'>
<div class='cap-left'></div>
<div class='cap-right'></div>
</div>
</div>
</footer>
<!-- content -->
</div>
</div>
<div class='content-cap-bottom cap-bottom'>
<div class='cap-left'></div>
<div class='cap-right'></div>
</div>
</div>
</div>
<script type='text/javascript'>
    window.setTimeout(function() {
        document.body.className = document.body.className.replace('loading', '');
      }, 10);
  </script>

<script type=