Android Open Source - RZAndroidBaseUtils J S O N Helper






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.raizlabs.json;
/*ww  w.ja  v  a 2  s .  co m*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.Log;

/**
 * Class with common utilities for JSON parsing
 * 
 * @author Dylan James
 */
public class JSONHelper {
  
  /**
   * Delegate which is executed on individual keys of a {@link JSONObject}. 
   */
  public interface JSONKeyDelegate {
    /**
     * Called to execute this delegate on the given key.
     * @param json The JSONObject the key belongs to.
     * @param key The key to handle.
     */
    public void execute(JSONObject json, String key);
  }
  
  /**
   * Runs the given delegate on all of the keys in the given JSON object.
   * @param json The JSON whose keys to run over.
   * @param delegate The delegate to run on each key.
   */
  public static void runOverKeys(JSONObject json, JSONKeyDelegate delegate) {
    if (json != null) {
      // JSON uses a set of Strings and returns an Iterator<String>
      // but doesn't clarify in the method definition.
      // Scary, but always works under the current implementation.
      @SuppressWarnings("unchecked")
      Iterator<String> iterator = json.keys();
      while (iterator.hasNext()) {
        delegate.execute(json, iterator.next());
      }
    }
  }
  
  /**
   * Iterates over the {@link JSONArray} defined at the given key in the
   * given JSON object, if one exists, and parses each element with the given
   * {@link JSONArrayParserDelegate}. If the delegate returns an item, it
   * will be added to the result list. If it does not, that index will be
   * skipped - no null will be added to the list.
   * @param json The {@link JSONObject} to get the array from
   * @param key The key to look for the array under
   * @param delegate The {@link JSONArrayParserDelegate} to call to parse
   * each object
   * @return A {@link List} containing all parsed objects or null if the key
   * didn't map to a {@link JSONArray}
   */
  public static <T> List<T> parseJSONArray(JSONObject json, String key,
      JSONArrayParserDelegate<T> delegate) {
    JSONArray array = json.optJSONArray(key);
    if (array == null) return null;
    
    return parseJSONArray(array, delegate);
  }
  
  /**
   * Iterates over the given {@link JSONArray} and parses each element with
   * the given {@link JSONArrayParserDelegate}. If the delegate returns an
   * item, it will be added to the result list. If it does not, that index
   * will be skipped - no null will be added to the list.
   * @param array The {@link JSONArray} to parse
   * @param delegate The {@link JSONArrayParserDelegate} to call to parse
   * each object
   * @return A {@link List} containing all parsed objects
   */
  public static <T> List<T> parseJSONArray(JSONArray array, 
      JSONArrayParserDelegate<T> delegate) {
    
    final int numItems = (array == null) ? 0 : array.length();
  
    if (numItems == 0) {
      return new ArrayList<T>(0);
    } else {
      List<T> items = new ArrayList<T>(numItems);
      
      for (int i = 0; i < numItems; i++) {
        try {
          JSONObject objectJSON = array.getJSONObject(i);
          
          T obj = delegate.parseObject(objectJSON);
          if (obj != null) items.add(obj);
        } catch (JSONException e) {
          // This shouldn't really ever happen since we are checking
          // the length
          Log.d(JSONHelper.class.getName(), "Error parsing object", e);
        }
      }
      
      return items;
    }
  }
  
  /**
   * Parses all strings out of the data at the given key inside the given
   * {@link JSONObject} if it exists
   * @param json The {@link JSONObject} to look for the key in
   * @param key The key to look for the strings under
   * @return The list of strings, or null if they couldn't be parsed or the
   * key wasn't defined
   */
  public static List<String> parseStrings(JSONObject json, String key) {
    // Attempt to grab the messages as an array of messages
    List<String> strings = parseStrings(json.optJSONArray(key));
    if (strings == null) {
      // If there is only one string, the field will be a simple string
      // mapping instead of an array of size 1 :(
      String string = json.optString(key);
      if (!TextUtils.isEmpty(string)) {
        strings = new ArrayList<String>(1);
        strings.add(string);
      }
    }

    return strings;
  }
  
  /**
   * Parses all strings out of the given {@link JSONArray} into a list.
   * @param json The array to parse.
   * @return A list containing all the strings or null if the input array
   * was null.
   */
  public static List<String> parseStrings(JSONArray json) {
    if (json != null) {

      final int numStrings = json.length();

      List<String> strings = new ArrayList<String>(numStrings);

      for (int i = 0; i < numStrings; i++) {
        String string = json.optString(i);
        if (!TextUtils.isEmpty(string)) strings.add(string);
      }

      return strings;
    }
    return null;
  }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java