<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Dima Stopel's blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/dimast/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/dimast/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:50:10 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Serializing (de-serializing) object to (from) JSON string on Android OS</title>
      <link>https://docs.microsoft.com/archive/blogs/dimast/serializing-de-serializing-object-to-from-json-string-on-android-os</link>
      <pubDate>Mon, 04 Jul 2011 12:50:00 GMT</pubDate>
      <dc:creator><![CDATA[Dima Stopel]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/dimast/2011/07/04/serializing-de-serializing-object-to-from-json-string-on-android-os/</guid>
      <description><![CDATA[Hi guys and girls,
During my last project on Android I had a task of serializing / de-serializing...]]></description>
      <content:encoded><![CDATA[<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">Hi guys and girls,</span></span></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">During my last project on Android I had a task of serializing / de-serializing an object to / from text file. I could choose either XML or JSON formats, so I couldn&rsquo;t use <i><a href="https://download.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html"><span style="color: #0000ff;" color="#0000ff">ObjectInputStream</span></a></i> and <i><a href="https://download.oracle.com/javase/6/docs/api/java/io/ObjectOutputStream.html"><span style="color: #0000ff;" color="#0000ff">ObjectOutputStream</span></a></i> pair. &nbsp;It was obvious to me that the Android framework should contain something that can do such a generic task. However, after digging some more I&rsquo;ve discovered that nothing similar exists. Regular Java SDK has the <i><a href="https://download.oracle.com/javase/1.4.2/docs/api/java/beans/XMLEncoder.html"><span style="color: #0000ff;" color="#0000ff">XMLEncoder</span></a></i> class that could do the job, but it was not ported to Android. Consequently I had no choice, but to write one myself. I chose JSON format since we have server infrastructure that talks JSON and since Android framework contains a handy class called <i><a href="https://developer.android.com/reference/org/json/JSONObject.html"><span style="color: #0000ff;" color="#0000ff">JSONObject</span></a></i> that can parse JSON to / from dictionary object. The purpose of this post is to share this code and some explanation on how it works with you. Please find the code below. You are welcome to use it in your own projects.</span></span></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">Few words on how it works: </span></span></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri"><i>JSONObjectInputStream</i> receives JSON data as an <i><a href="https://developer.android.com/reference/java/io/InputStream.html"><span style="color: #0000ff;" color="#0000ff">InputStream</span></a></i> or a <i>String</i> in the constructor. It provides <i>readObject </i>method for retrieving the actual object. It is templated and receives class type and optionally initial object instance of this type. It then traverses the class fields using reflection and searches for the value in the parsed JSON data. If values are found they are overridden in the instance. If not, the existing values are kept.</span></span></p>
<p></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri"><i>JSONObjectOutputStream </i>is simpler<i>. </i>It receives <i><a href="https://developer.android.com/reference/java/io/OutputStream.html"><span style="color: #0000ff;" color="#0000ff">OutputStream</span></a></i> in the constructor. Its <i>writeObject </i>method traverses the class fields using reflection and writes the values in the <i>JSONObject</i> dictionary. Then <i>JSONObject</i> is converted to string and written to the received <i>OutputStream</i>.</span></span><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">&nbsp;</span></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">Some of the known limitations of the presented code: it skips static fields, but this is very easy to fix (just remove the if-clause). It doesn&rsquo;t work on non-flat classes (only classes that contain primitives and strings). This functionality can be added on demand as well.</span></span></p>
<p></p>
<p><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">You can find all the code, including Android demo project that uses it </span><a href="https://github.com/dimastopel/JSONObjectSerialization"><span style="font-family: Calibri; color: #0000ff; font-size: small;" size="3" face="Calibri" color="#0000ff">here</span></a></p>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">JSONInputStream code:</span></span></p>
<!-- CODESTART -->
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">package</span><span style="color: #004a43;"> com</span><span style="color: #808030;">.</span><span style="color: #004a43;">JSONObjectSerialization</span><span style="color: #800080;">;</span>

<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">io</span><span style="color: #808030;">.</span><span style="color: #004a43;">BufferedReader</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">io</span><span style="color: #808030;">.</span><span style="color: #004a43;">IOException</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">io</span><span style="color: #808030;">.</span><span style="color: #004a43;">InputStream</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">io</span><span style="color: #808030;">.</span><span style="color: #004a43;">InputStreamReader</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">reflect</span><span style="color: #808030;">.</span><span style="color: #004a43;">Field</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">reflect</span><span style="color: #808030;">.</span><span style="color: #004a43;">Modifier</span><span style="color: #800080;">;</span>

<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> org</span><span style="color: #808030;">.</span><span style="color: #004a43;">json</span><span style="color: #808030;">.</span><span style="color: #004a43;">JSONException</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> org</span><span style="color: #808030;">.</span><span style="color: #004a43;">json</span><span style="color: #808030;">.</span><span style="color: #004a43;">JSONObject</span><span style="color: #800080;">;</span>

<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> android</span><span style="color: #808030;">.</span><span style="color: #004a43;">util</span><span style="color: #808030;">.</span><span style="color: #004a43;">Log</span><span style="color: #800080;">;</span>


<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@author</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">dimast</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> This class can deserialize classes from JSON strings.</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> This works for classes that contain only primitive fields.</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;">*/</span>
<span style="color: #800000; font-weight: bold;">public</span> <span style="color: #800000; font-weight: bold;">class</span> JSONInputStream <span style="color: #800000; font-weight: bold;">extends</span> InputStream 
<span style="color: #800080;">{</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Creates the object.</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> is Input stream that contains the JSON content</span>
<span style="color: #3f5fbf;">*/</span>
    <span style="color: #800000; font-weight: bold;">public</span> JSONInputStream<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977; font-weight: bold;">InputStream</span> is<span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        _is <span style="color: #808030;">=</span> is<span style="color: #800080;">;</span>
        _json <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>

<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Creates the object.</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> json Input string that contains the JSON content</span>
<span style="color: #3f5fbf;">*/</span>
    <span style="color: #800000; font-weight: bold;">public</span> JSONInputStream<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977; font-weight: bold;">String</span> json<span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        _json <span style="color: #808030;">=</span> json<span style="color: #800080;">;</span>
        _is <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>

<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Creates the object from the stream content. </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">&lt;T&gt;</span><span style="color: #3f5fbf;"> The type of the object </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> objClass The class of the object</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@throws</span><span style="color: #3f5fbf;"> JSONStreamException in case parsing error is occurred</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"> the instance of the object created from the stream content</span>
<span style="color: #3f5fbf;">*/</span>
    <span style="color: #800000; font-weight: bold;">public</span> <span style="color: #800000; font-weight: bold;">final</span> <span style="color: #808030;">&lt;</span>T<span style="color: #808030;">&gt;</span> T readObject<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977; font-weight: bold;">Class</span><span style="color: #808030;">&lt;</span>T<span style="color: #808030;">&gt;</span> objClass<span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throws</span> JSONStreamException
    <span style="color: #800080;">{</span>
        <span style="color: #800000; font-weight: bold;">return</span> readObject<span style="color: #808030;">(</span>objClass<span style="color: #808030;">,</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>        
    <span style="color: #800080;">}</span>

<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Creates the object from the stream content. </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">&lt;T&gt;</span><span style="color: #3f5fbf;"> The type of the object </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> objClass The class of the object</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> initialObj Initial object version</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@throws</span><span style="color: #3f5fbf;"> JSONStreamException in case parsing error is occurred</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"> the instance of the object created from the stream content</span>
<span style="color: #3f5fbf;">*/</span>
    <span style="color: #800000; font-weight: bold;">public</span> <span style="color: #800000; font-weight: bold;">final</span> <span style="color: #808030;">&lt;</span>T<span style="color: #808030;">&gt;</span> T readObject<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977; font-weight: bold;">Class</span><span style="color: #808030;">&lt;</span>T<span style="color: #808030;">&gt;</span> objClass<span style="color: #808030;">,</span> <span style="color: #800000; font-weight: bold;">final</span> T initialObj<span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throws</span> JSONStreamException
    <span style="color: #800080;">{</span>
        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>_is <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span> <span style="color: #808030;">&amp;</span><span style="color: #808030;">&amp;</span> _json <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">throw</span> <span style="color: #800000; font-weight: bold;">new</span> JSONStreamException<span style="color: #808030;">(</span><span style="color: #0000e6;">"Can't read object, the input is null."</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>

        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>_json <span style="color: #808030;">=</span><span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">try</span> 
            <span style="color: #800080;">{</span>
                <span style="color: #696969;">// the below uses platform default char set which is UTF-8</span>
                <span style="color: #696969;">// </span><span style="color: #5555dd;">https://developer.android.com/reference/java/nio/charset/Charset.html</span>
                _json <span style="color: #808030;">=</span> readStreamTillEnd<span style="color: #808030;">(</span>_is<span style="color: #808030;">,</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">Exception</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                <span style="color: #800000; font-weight: bold;">throw</span> <span style="color: #800000; font-weight: bold;">new</span> JSONStreamException<span style="color: #808030;">(</span><span style="color: #0000e6;">"Can't read input stream."</span><span style="color: #808030;">,</span> e<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>
        <span style="color: #800080;">}</span>

        JSONObject jsonObj <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">try</span> 
        <span style="color: #800080;">{</span>
            jsonObj <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">new</span> JSONObject<span style="color: #808030;">(</span>_json<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span> 
        <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span>JSONException e<span style="color: #808030;">)</span> 
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">throw</span> <span style="color: #800000; font-weight: bold;">new</span> JSONStreamException<span style="color: #808030;">(</span><span style="color: #0000e6;">"Can't deserialize the object. Failed to create JSON object."</span><span style="color: #808030;">,</span> e<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>

        T object <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>initialObj <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            object <span style="color: #808030;">=</span> initialObj<span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>
        <span style="color: #800000; font-weight: bold;">else</span>
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">try</span> 
            <span style="color: #800080;">{</span>
                object <span style="color: #808030;">=</span> objClass<span style="color: #808030;">.</span>newInstance<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">InstantiationException</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                <span style="color: #800000; font-weight: bold;">throw</span> <span style="color: #800000; font-weight: bold;">new</span> JSONStreamException<span style="color: #808030;">(</span><span style="color: #0000e6;">"Can't deserialize the object. Failed to instantiate object."</span><span style="color: #808030;">,</span> e<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">IllegalAccessException</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                <span style="color: #800000; font-weight: bold;">throw</span> <span style="color: #800000; font-weight: bold;">new</span> JSONStreamException<span style="color: #808030;">(</span><span style="color: #0000e6;">"Can't deserialize the object. Failed to instantiate object."</span><span style="color: #808030;">,</span> e<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>
        <span style="color: #800080;">}</span>        

        <span style="color: #bb7977; font-weight: bold;">Field</span><span style="color: #808030;">[</span><span style="color: #808030;">]</span> fields <span style="color: #808030;">=</span> objClass<span style="color: #808030;">.</span>getDeclaredFields<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">for</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">Field</span> field <span style="color: #808030;">:</span> fields<span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">Modifier</span><span style="color: #808030;">.</span>isStatic<span style="color: #808030;">(</span>field<span style="color: #808030;">.</span>getModifiers<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span>
            <span style="color: #800080;">{</span>
                <span style="color: #696969;">// skip static</span>
                <span style="color: #800000; font-weight: bold;">continue</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>

            <span style="color: #800000; font-weight: bold;">try</span> 
            <span style="color: #800080;">{</span>
                <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>jsonObj<span style="color: #808030;">.</span>has<span style="color: #808030;">(</span>field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span>
                <span style="color: #800080;">{</span>
                    Log<span style="color: #808030;">.</span>i<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONInputStream"</span><span style="color: #808030;">,</span> 
                            <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Updating field [%s]"</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                    field<span style="color: #808030;">.</span>setAccessible<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">true</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                    field<span style="color: #808030;">.</span>set<span style="color: #808030;">(</span>object<span style="color: #808030;">,</span> jsonObj<span style="color: #808030;">.</span>get<span style="color: #808030;">(</span>field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800080;">}</span>
                <span style="color: #800000; font-weight: bold;">else</span>
                <span style="color: #800080;">{</span>
                    Log<span style="color: #808030;">.</span>i<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONInputStream"</span><span style="color: #808030;">,</span> 
                            <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Field [%s] doesn't exist. Keeping default value."</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800080;">}</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">IllegalArgumentException</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONInputStream"</span><span style="color: #808030;">,</span> 
                        <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Failed to get field [%s] %s"</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800000; font-weight: bold;">continue</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span>JSONException e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONInputStream"</span><span style="color: #808030;">,</span> 
                        <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Failed to get field [%s] %s"</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800000; font-weight: bold;">continue</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">IllegalAccessException</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONInputStream"</span><span style="color: #808030;">,</span> 
                        <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Failed to get field [%s] %s"</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800000; font-weight: bold;">continue</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>
        <span style="color: #800080;">}</span>

        <span style="color: #800000; font-weight: bold;">return</span> object<span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>

    <span style="color: #808030;">@</span>Override
    <span style="color: #808030;">@</span>Deprecated
    <span style="color: #800000; font-weight: bold;">public</span> <span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977;">int</span> read<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throws</span> <span style="color: #bb7977; font-weight: bold;">IOException</span> 
    <span style="color: #800080;">{</span>
        <span style="color: #800000; font-weight: bold;">return</span> <span style="color: #008c00;">0</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>

<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Reads the input stream until and and produces String.</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> is the input stream to read from</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> enc the encoding</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"> in case of null default encoding is used</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"> the string that represents stream's content</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@throws</span><span style="color: #3f5fbf;"> Exception on error</span>
<span style="color: #3f5fbf;">*/</span>
    <span style="color: #800000; font-weight: bold;">private</span> <span style="color: #800000; font-weight: bold;">static</span> <span style="color: #bb7977; font-weight: bold;">String</span> readStreamTillEnd<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977; font-weight: bold;">InputStream</span> is<span style="color: #808030;">,</span> <span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977; font-weight: bold;">String</span> enc<span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throws</span> <span style="color: #bb7977; font-weight: bold;">Exception</span>
    <span style="color: #800080;">{</span>
        StringBuilder sb <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">new</span> StringBuilder<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #bb7977; font-weight: bold;">BufferedReader</span> br <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #800080;">;</span>

        <span style="color: #800000; font-weight: bold;">try</span> 
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>enc <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span>
            <span style="color: #800080;">{</span>
                br <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">new</span> <span style="color: #bb7977; font-weight: bold;">BufferedReader</span><span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">new</span> <span style="color: #bb7977; font-weight: bold;">InputStreamReader</span><span style="color: #808030;">(</span>is<span style="color: #808030;">,</span> enc<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>
            <span style="color: #800000; font-weight: bold;">else</span>
            <span style="color: #800080;">{</span>
                <span style="color: #696969;">// the below uses platform default char set which is UTF-8</span>
                <span style="color: #696969;">// </span><span style="color: #5555dd;">https://developer.android.com/reference/java/nio/charset/Charset.html</span>
                br <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">new</span> <span style="color: #bb7977; font-weight: bold;">BufferedReader</span><span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">new</span> <span style="color: #bb7977; font-weight: bold;">InputStreamReader</span><span style="color: #808030;">(</span>is<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>
        <span style="color: #800080;">}</span> 
        <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">Exception</span> e<span style="color: #808030;">)</span> 
        <span style="color: #800080;">{</span>
            Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONInputStream"</span><span style="color: #808030;">,</span> 
                    <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Can't create BufferedReader [%s]"</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800000; font-weight: bold;">throw</span> e<span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>

        <span style="color: #696969;">// Read the stream until end</span>
        <span style="color: #bb7977; font-weight: bold;">String</span> line <span style="color: #808030;">=</span> <span style="color: #0000e6;">""</span><span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">try</span> 
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">while</span> <span style="color: #808030;">(</span><span style="color: #808030;">(</span>line <span style="color: #808030;">=</span> br<span style="color: #808030;">.</span>readLine<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span> <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span> 
                sb<span style="color: #808030;">.</span>append<span style="color: #808030;">(</span>line<span style="color: #808030;">)</span><span style="color: #800080;">;</span> 
            <span style="color: #800080;">}</span>
        <span style="color: #800080;">}</span> 
        <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">IOException</span> e<span style="color: #808030;">)</span> 
        <span style="color: #800080;">{</span>
            Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONInputStream"</span><span style="color: #808030;">,</span> 
                    <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Can't Can't read input stream [%s]"</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800000; font-weight: bold;">throw</span> e<span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>


        <span style="color: #800000; font-weight: bold;">return</span> sb<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>


    <span style="color: #800000; font-weight: bold;">private</span> <span style="color: #bb7977; font-weight: bold;">InputStream</span> _is<span style="color: #800080;">;</span>
    <span style="color: #800000; font-weight: bold;">private</span> <span style="color: #bb7977; font-weight: bold;">String</span> _json<span style="color: #800080;">;</span>
<span style="color: #800080;">}</span>
</pre>
<pre style="color: #000000; background: #ffffff;"><span style="color: #696969;">.</span>

<!-- OUTPUT STARTS HERE --></pre>
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">JSONOutputStream code:</span></span></p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #800000; font-weight: bold;">package</span><span style="color: #004a43;"> com</span><span style="color: #808030;">.</span><span style="color: #004a43;">JSONObjectSerialization</span><span style="color: #800080;">;</span>

<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">io</span><span style="color: #808030;">.</span><span style="color: #004a43;">IOException</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">io</span><span style="color: #808030;">.</span><span style="color: #004a43;">OutputStream</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">reflect</span><span style="color: #808030;">.</span><span style="color: #004a43;">Field</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">reflect</span><span style="color: #808030;">.</span><span style="color: #004a43;">Modifier</span><span style="color: #800080;">;</span>

<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> org</span><span style="color: #808030;">.</span><span style="color: #004a43;">json</span><span style="color: #808030;">.</span><span style="color: #004a43;">JSONException</span><span style="color: #800080;">;</span>
<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> org</span><span style="color: #808030;">.</span><span style="color: #004a43;">json</span><span style="color: #808030;">.</span><span style="color: #004a43;">JSONObject</span><span style="color: #800080;">;</span>

<span style="color: #800000; font-weight: bold;">import</span><span style="color: #004a43;"> android</span><span style="color: #808030;">.</span><span style="color: #004a43;">util</span><span style="color: #808030;">.</span><span style="color: #004a43;">Log</span><span style="color: #800080;">;</span>


<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@author</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">dimast</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> This class can serialize classes to JSON strings.</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> This works for classes that contain only primitive fields.</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;">*/</span>
<span style="color: #800000; font-weight: bold;">public</span> <span style="color: #800000; font-weight: bold;">class</span> JSONOutputStream <span style="color: #800000; font-weight: bold;">extends</span> OutputStream
<span style="color: #800080;">{</span>
    <span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Creates the stream.</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> os The output stream that receives the JSON output</span>
<span style="color: #3f5fbf;">*/</span>
    <span style="color: #800000; font-weight: bold;">public</span> JSONOutputStream<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977; font-weight: bold;">OutputStream</span> os<span style="color: #808030;">)</span> 
    <span style="color: #800080;">{</span>
        _os <span style="color: #808030;">=</span> os<span style="color: #800080;">;</span>
    <span style="color: #800080;">}</span>

    <span style="color: #808030;">@</span>Override
    <span style="color: #800000; font-weight: bold;">public</span> <span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977;">void</span> close<span style="color: #808030;">(</span><span style="color: #808030;">)</span> 
    <span style="color: #800080;">{</span>
        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>_os <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span> 
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">try</span> 
            <span style="color: #800080;">{</span>
                _os<span style="color: #808030;">.</span>close<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">IOException</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONOutputStream"</span><span style="color: #808030;">,</span>
                        <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Can't close output stream: %s"</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>
        <span style="color: #800080;">}</span>
    <span style="color: #800080;">}</span>

    <span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Writes the JSON object representation to the output stream. </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"></span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> object The object to that will be converted to JSON stream.</span>
<span style="color: #3f5fbf;">*/</span>
    <span style="color: #800000; font-weight: bold;">public</span> <span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977;">void</span> writeObject<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977; font-weight: bold;">Object</span> object<span style="color: #808030;">)</span>
    <span style="color: #800080;">{</span>
        <span style="color: #bb7977; font-weight: bold;">Field</span><span style="color: #808030;">[</span><span style="color: #808030;">]</span> fields <span style="color: #808030;">=</span> object<span style="color: #808030;">.</span>getClass<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">.</span>getDeclaredFields<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        JSONObject jsonObj <span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">new</span> JSONObject<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800000; font-weight: bold;">for</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">Field</span> field <span style="color: #808030;">:</span> fields<span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            <span style="color: #bb7977;">int</span> modifiers <span style="color: #808030;">=</span> field<span style="color: #808030;">.</span>getModifiers<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">Modifier</span><span style="color: #808030;">.</span>isStatic<span style="color: #808030;">(</span>modifiers<span style="color: #808030;">)</span><span style="color: #808030;">)</span>
            <span style="color: #800080;">{</span>
                <span style="color: #696969;">// skip statics</span>
                <span style="color: #800000; font-weight: bold;">continue</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>

            <span style="color: #800000; font-weight: bold;">try</span> 
            <span style="color: #800080;">{</span>
                field<span style="color: #808030;">.</span>setAccessible<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">true</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                jsonObj<span style="color: #808030;">.</span>put<span style="color: #808030;">(</span>field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>get<span style="color: #808030;">(</span>object<span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">IllegalArgumentException</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONOutputStream"</span><span style="color: #808030;">,</span>
                        <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Failed serializing field [%s] %s"</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800000; font-weight: bold;">continue</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span>JSONException e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONOutputStream"</span><span style="color: #808030;">,</span>
                        <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Failed serializing field [%s] %s"</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800000; font-weight: bold;">continue</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">IllegalAccessException</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONOutputStream"</span><span style="color: #808030;">,</span>
                        <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Failed serializing field [%s] %s"</span><span style="color: #808030;">,</span> field<span style="color: #808030;">.</span>getName<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
                <span style="color: #800000; font-weight: bold;">continue</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>
        <span style="color: #800080;">}</span>

        <span style="color: #800000; font-weight: bold;">if</span> <span style="color: #808030;">(</span>_os <span style="color: #808030;">!</span><span style="color: #808030;">=</span> <span style="color: #800000; font-weight: bold;">null</span><span style="color: #808030;">)</span>
        <span style="color: #800080;">{</span>
            <span style="color: #800000; font-weight: bold;">try</span> 
            <span style="color: #800080;">{</span>
                <span style="color: #696969;">// the below uses platform default char set which is UTF-8</span>
                <span style="color: #696969;">// </span><span style="color: #5555dd;">https://developer.android.com/reference/java/nio/charset/Charset.html</span>
                _os<span style="color: #808030;">.</span>write<span style="color: #808030;">(</span>jsonObj<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">.</span>getBytes<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span> 
            <span style="color: #800000; font-weight: bold;">catch</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">IOException</span> e<span style="color: #808030;">)</span> 
            <span style="color: #800080;">{</span>
                Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONOutputStream"</span><span style="color: #808030;">,</span>
                        <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>format<span style="color: #808030;">(</span><span style="color: #0000e6;">"Failed writing to output stream: %s"</span><span style="color: #808030;">,</span> e<span style="color: #808030;">.</span>toString<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800080;">}</span>
        <span style="color: #800080;">}</span>
        <span style="color: #800000; font-weight: bold;">else</span>
        <span style="color: #800080;">{</span>
            Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONOutputStream"</span><span style="color: #808030;">,</span> <span style="color: #0000e6;">"Can't write to output stream. _os is null."</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>
    <span style="color: #800080;">}</span>

    <span style="color: #808030;">@</span>Override
    <span style="color: #808030;">@</span>Deprecated
    <span style="color: #800000; font-weight: bold;">public</span> <span style="color: #bb7977;">void</span> write<span style="color: #808030;">(</span><span style="color: #800000; font-weight: bold;">final</span> <span style="color: #bb7977;">int</span> oneByte<span style="color: #808030;">)</span> <span style="color: #800000; font-weight: bold;">throws</span> <span style="color: #bb7977; font-weight: bold;">IOException</span> 
    <span style="color: #800080;">{</span>
        <span style="color: #696969;">// do nothing</span>
    <span style="color: #800080;">}</span> 

    <span style="color: #800000; font-weight: bold;">private</span> <span style="color: #bb7977; font-weight: bold;">OutputStream</span> _os<span style="color: #800080;">;</span>
<span style="color: #800080;">}</span>
</pre>
<!-- USAGE EXAMPLE -->
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">Usage example:</span></span></p>
<pre style="color: #000000; background: #ffffff;"><span style="color: #696969;"> // Creating the initial object</span>
        appendLog("Welcome!")<span style="color: #808030;">;</span>
        SimpleClass sc = new SimpleClass(2.4, 17, true, "thisisit!")<span style="color: #808030;">;</span>
        appendLog("This is the initial object:")<span style="color: #808030;">;</span>
        appendLog(sc.toString())<span style="color: #808030;">;</span>
        
        <span style="color: #696969;">// Serializing it to JSON string</span>
        ByteArrayOutputStream baos = new ByteArrayOutputStream()<span style="color: #808030;">;</span>
        JSONOutputStream jos = new JSONOutputStream(baos)<span style="color: #808030;">;</span>
        jos.writeObject(sc)<span style="color: #808030;">;</span>
        jos.close()<span style="color: #808030;">;</span>
        
        <span style="color: #696969;">// Get the string form the output stream and print it</span>
        String jsonString = baos.toString()<span style="color: #808030;">;</span>
        appendLog("This is the serialized object:")<span style="color: #808030;">;</span>
        appendLog(jsonString)<span style="color: #808030;">;</span>
        
        <span style="color: #696969;">// Deserialize the concrete object from the JSON string</span>
        JSONInputStream jis = new JSONInputStream(jsonString)<span style="color: #808030;">;</span>
        SimpleClass sc2 = null<span style="color: #808030;">;</span>
        try 
        <span style="color: #800080;">{</span>
            sc2 <span style="color: #808030;">=</span> jis<span style="color: #808030;">.</span>readObject<span style="color: #808030;">(</span>SimpleClass<span style="color: #808030;">.</span>class<span style="color: #808030;">)</span><span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span> 
        catch (JSONStreamException e) 
        <span style="color: #800080;">{</span>
            Log<span style="color: #808030;">.</span>e<span style="color: #808030;">(</span><span style="color: #0000e6;">"JSONObjectSerialization"</span><span style="color: #808030;">,</span> <span style="color: #0000e6;">"Failed to deserialize the object"</span><span style="color: #808030;">)</span><span style="color: #800080;">;</span>
            <span style="color: #800000; font-weight: bold;">return</span><span style="color: #800080;">;</span>
        <span style="color: #800080;">}</span>
        
        <span style="color: #696969;">// Print the deserialized object</span>
        appendLog("This is the de-serialized new object:")<span style="color: #808030;">;</span>
        appendLog(sc2.toString())<span style="color: #808030;">;</span>
        
        <span style="color: #696969;">// Bye</span>
        appendLog("Enjoy :)")<span style="color: #808030;">;</span>
</pre>
<!-- CODEEND -->
<p><span style="font-size: small;" size="3"><span style="font-family: Calibri;" face="Calibri">.</span></span></p>
<p><span style="font-family: Calibri; font-size: small;" size="3" face="Calibri">&nbsp;</span></p>
<p></p>
<pre style="color: #000000; background: #ffffff;">&nbsp;</pre>]]></content:encoded>
    </item>
  </channel>
</rss>