title: java bean \ object to android json object
body: <p>I have an android application that relies heavily on Json Web Services.
  so I have many requests and responses based on <code>json</code> format.
  The basic idea is i have a request that get's a bean object, for example <code>HotelSearchData</code> and the request knows how to convert that bean to json map\ object then it writes that <code>JSONObject</code> to the network (using <code>http\https</code>).</p>
  
  <p>naturally I need to convert from and to <code>json</code>.
  For the time being I'm doing it in a bad but fast way:</p>
  
  <ol>
  <li>get the <code>object</code></li>
  <li>have a list of const in the form of <code>KEY_XXXX</code> where <code>xxxx</code> is the name of the key and the value is the string that will be the actual key in the <code>json</code> object.</li>
  <li>use the key like in the following example: <code>base.put(KEY_FROM_DATE, hotelSearchData.getFromDate())</code></li>
  <li>for ArrayList\ arrays I use <code>JSONArray</code> naturally, each <code>{}</code> in <code>json</code> is a bean object as far as I'm concerned and  I need to create a bean for it.</li>
  </ol>
  
  <p>The problem is that this way, for future development is not so good (for the very least...)
  Every new request requires me new beans that some of them may have the same information inside them causing information and code duplicity.</p>
  
  <p>Every new request requires me to create new <em>KEY finals</em> copying sometimes from other request with similar keys - idiotic :-/</p>
  
  <p>for the first item, except for optimizing what I'm about to do and dividing shared code beans to smaller items i can extend later on i can't see any elegant solution.</p>
  
  <p>But for the requests\response I thought of something but I'm not so sure how elegant it is...
  I know <em>Google</em> have a different json library that uses reflection to <code>serialize\deserialize</code> <code>JSONObject</code>s. I thought of using <em>annotations</em> and reflection to do the <code>serialization</code> and <code>deserialization</code>, the <code>annotation</code>s should help in the cases where the member name is <strong>NOT</strong> the same as the json key for the member's value. also for <code>ArrayList&lt;?&gt;</code> that it's type is not saved in byte code and so does not exist after compilation, so you can either specify basic type or full package name.</p>
  
  <p>In this way once I create the bean with the help of some <code>annotations</code> (maybe <code>python</code> script \ant script to automate part of it ?) I can make beans that if tomorrow I move to <code>xml</code> or something else (<code>binary</code>) I can just create different <code>serializer</code>, <code>deserializer</code> and be done with it.</p>
  
  <p>I hoped to hear your opinion on it, or some other solutions, get some inspiration, I'm really disappointed with my current code... :-(</p>