JSONNullWrapper.java :  » Client » 4trdroid » net » maxmilian » ftrdroid » Android Open Source

Android Open Source » Client » 4trdroid 
4trdroid » net » maxmilian » ftrdroid » JSONNullWrapper.java
package net.maxmilian.ftrdroid;

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

/**
 * @author xhovor01
 * 
 * Wraps JSONObject methods getString, getInt,...
 * and handles null values.
 * 
 * If null occures, exception is catched and null
 * or some other value is returned
 * 
 * fok ju tu, JSON!
 *
 */
public class JSONNullWrapper {

  public static Integer getInteger (JSONObject object, String name) {

    int value;


    value = object.optInt(name, Integer.MAX_VALUE);


    if (value == Integer.MAX_VALUE) return null;

    return value;
  }

  public static Double getDouble (JSONObject object, String name) {

    double value;
    value = object.optDouble(name, Double.MAX_VALUE);

    if (value == Double.MAX_VALUE) return null;
    return value;
  }

  public static String getString (JSONObject object, String name) {

    return object.optString(name, null);
  }
  public static Boolean getBoolean (JSONObject object, String name) {

    Boolean value;

    try {

      value = object.getBoolean(name);

 
    }
    catch (Exception e)  {
      value = null;
    }

    return value;
  }
  public static JSONArray getJSONArray (JSONObject object, String name) {

    JSONArray value;

    value = object.optJSONArray(name);

    if (value == JSONObject.NULL) value =  null;

    return value;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.