title: Best technique to map JSON data into ContentValues
body: <p>I'm developing an Android app which will initiate a number of API calls that return JSON data structures and then store the results in a content provider. Different API calls return different JSON data structures and map to a corresponding table schema in the content provider. I'm looking for a simple and Java-esque method to map from the properties in a JSONObject to a flat ContentValues object. I started to use a simple HashMap and iterating over it's entrySet mapping key strings in the JSONObject to value strings for the ContentValues object, but I'd like to account for the fact that some JSON properties are integers or booleans. Also, in some cases I'd like a more complex mapping such as a JSONArray into a comma separated string.  In C, I'd probably just do this with a struct array name, value, type, and an optional callback to handle more complex mappings.</p>
  
  <p><strong>UPDATE</strong>:  Due to the hierarchal nature of the JSON Data Structure and due to the fact that it can actually have sub-tables at certain depths I've taken the following approach.</p>
  
  <pre><code>private static interface MapJSON {
      public void mapData(JSONObject object, ContentValues values)
          throws JSONException;
  }
  
  private static abstract class AbstractMapJSON implements MapJSON {
      protected final String mJSONName;
      protected final String mContentName;
  
      public AbstractMapJSON(String jsonName, String contentName) {
          mJSONName = jsonName;
          mContentName = contentName;
      }
  
      public abstract void mapData(JSONObject object, ContentValues values)
          throws JSONException;
  }
  
  /* This is the basic template for each of the basic types */
  private static class BooleanMapJSON extends AbstractMapJSON {
      public BooleanMapJSON(String jsonName, String contentName) {
          super(jsonName, contentName);
      }
  
      public void mapData(JSONObject object, ContentValues values)
          throws JSONException {
          values.put(mContentName, object.getBoolean(mJSONName));
      }
  }
  
  /* This class takes a nested JSON Object and flattens it into the same table */
  private static class ObjectMapJSON implements MapJSON { 
      protected final String mJSONName;
      protected final MapJSON[] mMap;
  
      public ObjectMapJSON(String jsonName, MapJSON[] map) {
          mJSONName = jsonName;
          mMap = map;
      }
  
      public void mapData(JSONObject object, ContentValues values)
          throws JSONException {
          JSONObject subObject = object.getJSONObject(mJSONName);
          for(MapJSON mapItem: mMap) {
              mapItem.mapData(subObject, values);
          }
      }
  }
  </code></pre>
  
  <p>With that defined, I've can create mappings like this:</p>
  
  <pre><code>private static final MapJSON[] mainSiteMap = new MapJSON[] {
      new StringMapJSON("name", StackPad.Sites.NAME),
      new LongMapJSON("creation_date", StackPad.Sites.CREATION_DATE),
      new StringMapJSON("description", StackPad.Sites.DESCRIPTION),
  };
  
  private static final MapJSON sitesMap = new ObjectMapJSON("main_site", mainSiteMap);
  </code></pre>
  
  <p>But it still seems like it needs a little work to mesh well.</p>