JSON.java [src/java/m/multiobj] Revision:   Date:
package m.multiobj;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.joda.time.DateTime;

/**
 * A class with some extra functions for JSON to make it easier to parse
 * 
 * @author Andre Dozier <andre.dozier@rams.colostate.edu>
 */
public class JSON {
    
    public final JSONObject object; 
    public final JSONArray array; 
    
    public JSON(JSONObject o) { 
        object = o; 
        array = null;
    }

    public JSON(JSONArray a) { 
        object = null; 
        array = a;
    }
    
    /*
    LOCAL METHODS
    */
    
    public String getString(String key) throws JSONException { 
        return getString(object, key); 
    }
    
    public String getString(String key, String def) throws JSONException { 
        return getString(object, key, def); 
    }

    public String getString(int index) throws JSONException { 
        return getString(array, index); 
    }
    
    public String getString(int index, String def) throws JSONException { 
        return getString(array, index, def); 
    }

    public int getInt(String key) throws JSONException { 
        return getInt(object, key); 
    }
    
    public int getInt(String key, int def) throws JSONException { 
        return getInt(object, key, def); 
    }

    public int getInt(int index) throws JSONException { 
        return getInt(array, index); 
    }
    
    public int getInt(int index, int def) throws JSONException { 
        return getInt(array, index, def); 
    }
    
    public double getDouble(String key) throws JSONException { 
        return getDouble(object, key); 
    }
    
    public double getDouble(String key, double def) throws JSONException { 
        return getDouble(object, key, def); 
    }

    public double getDouble(int index) throws JSONException { 
        return getDouble(array, index); 
    }
    
    public double getDouble(int index, double def) throws JSONException { 
        return getDouble(array, index, def); 
    }
    
    public DateTime getDateTime(String key) throws JSONException, ParseException { 
        return getDateTime(object, key); 
    }
    
    public DateTime getDateTime(String key, DateTime def) throws JSONException, ParseException { 
        return getDateTime(object, key, def); 
    }

    public DateTime getDateTime(int index) throws JSONException, ParseException { 
        return getDateTime(array, index); 
    }
    
    public DateTime getDateTime(int index, DateTime def) throws JSONException, ParseException { 
        return getDateTime(array, index, def); 
    }
    
    public String[] getStringArray(String key) throws JSONException { 
        return getStringArray(object, key); 
    }
    
    public String[] getStringArray(String key, String[] def) throws JSONException { 
        return getStringArray(object, key, def); 
    }

    public String[] getStringArray(int index) throws JSONException { 
        return getStringArray(array, index); 
    }
    
    public String[] getStringArray(int index, String[] def) throws JSONException { 
        return getStringArray(array, index, def); 
    }
    
    public int[] getIntArray(String key) throws JSONException { 
        return getIntArray(object, key); 
    }
    
    public int[] getIntArray(String key, int[] def) throws JSONException { 
        return getIntArray(object, key, def); 
    }

    public int[] getIntArray(int index) throws JSONException { 
        return getIntArray(array, index); 
    }
    
    public int[] getIntArray(int index, int[] def) throws JSONException { 
        return getIntArray(array, index, def); 
    }
    
    public double[] getDoubleArray(String key) throws JSONException { 
        return getDoubleArray(object, key); 
    }
    
    public double[] getDoubleArray(String key, double[] def) throws JSONException { 
        return getDoubleArray(object, key, def); 
    }

    public double[] getDoubleArray(int index) throws JSONException { 
        return getDoubleArray(array, index); 
    }
    
    public double[] getDoubleArray(int index, double[] def) throws JSONException { 
        return getDoubleArray(array, index, def); 
    }
    
    public DateTime[] getDateTimeArray(String key) throws JSONException, ParseException { 
        return getDateTimeArray(object, key); 
    }
    
    public DateTime[] getDateTimeArray(String key, DateTime[] def) throws JSONException, ParseException { 
        return getDateTimeArray(object, key, def); 
    }

    public DateTime[] getDateTimeArray(int index) throws JSONException, ParseException { 
        return getDateTimeArray(array, index); 
    }
    
    public DateTime[] getDateTimeArray(int index, DateTime[] def) throws JSONException, ParseException { 
        return getDateTimeArray(array, index, def); 
    }
    
    public JSONArray getJSONArray(String key) throws JSONException { 
        return getJSONArray(object, key); 
    }
    
    public JSONArray getJSONArray(String key, JSONArray def) throws JSONException { 
        return getJSONArray(object, key, def); 
    }
    
    public JSONArray getJSONArray(int index) throws JSONException { 
        return getJSONArray(array, index); 
    }
    
    public JSONArray getJSONArray(int index, JSONArray def) throws JSONException { 
        return getJSONArray(array, index, def); 
    }
    
    

    /*
    STATIC METHODS
    */
    
    public static String getString(JSONObject o, String key) throws JSONException { 
        return o.getString(key);
    }
    
    public static String getString(JSONObject o, String key, String def) throws JSONException { 
        if (o.has(key))
            return o.getString(key);
        else 
            return def; 
    }

    public static String getString(JSONArray o, int index) throws JSONException { 
        return o.getString(index);
    }
    
    public static String getString(JSONArray o, int index, String def) throws JSONException { 
        if (index < o.length())
            return o.getString(index);
        else 
            return def; 
    }
    
    public static int getInt(JSONObject o, String key) throws JSONException { 
        return o.getInt(key);
    }
    
    public static int getInt(JSONObject o, String key, int def) throws JSONException { 
        if (o.has(key))
            return o.getInt(key);
        else 
            return def; 
    }

    public static int getInt(JSONArray o, int index) throws JSONException { 
        return o.getInt(index);
    }
    
    public static int getInt(JSONArray o, int index, int def) throws JSONException { 
        if (index < o.length())
            return o.getInt(index);
        else 
            return def; 
    }

    public static double getDouble(JSONObject o, String key) throws JSONException { 
        return o.getDouble(key);
    }
    
    public static double getDouble(JSONObject o, String key, double def) throws JSONException { 
        if (o.has(key))
            return o.getDouble(key);
        else 
            return def; 
    }

    public static double getDouble(JSONArray o, int index) throws JSONException { 
        return o.getDouble(index);
    }
    
    public static double getDouble(JSONArray o, int index, double def) throws JSONException { 
        if (index < o.length())
            return o.getDouble(index);
        else 
            return def; 
    }
    
    public static DateTime getDateTime(JSONObject o, String key) throws JSONException, ParseException { 
        return DateUtils.toDate(o.getString(key), DateUtils.DEFAULT_DATE_FORMAT);
    }
    
    public static DateTime getDateTime(JSONObject o, String key, DateTime def) throws JSONException, ParseException { 
        if (o.has(key))
            return getDateTime(o, key);
        else 
            return def; 
    }

    public static DateTime getDateTime(JSONArray o, int index) throws JSONException, ParseException { 
        return DateUtils.toDate(o.getString(index), DateUtils.DEFAULT_DATE_FORMAT);
    }
    
    public static DateTime getDateTime(JSONArray o, int index, DateTime def) throws JSONException, ParseException { 
        if (index < o.length())
            return getDateTime(o, index);
        else 
            return def; 
    }
    
    public static JSONArray getJSONArray(JSONObject o, String key) throws JSONException {
        return o.getJSONArray(key);
    }
    
    public static JSONArray getJSONArray(JSONObject o, String key, JSONArray def) throws JSONException {
        if (o.has(key)) {
            return o.getJSONArray(key);
        } else {
            return def;
        }
    }
    
    public static JSONArray getJSONArray(JSONArray o, int index) throws JSONException { 
        return o.getJSONArray(index); 
    }
    
    public static JSONArray getJSONArray(JSONArray o, int index, JSONArray def) throws JSONException { 
        if (index < o.length())
            return getJSONArray(o, index); 
        else 
            return def; 
    }
    
    public static JSONObject getParam(JSONArray o, String paramName) throws JSONException { 
        int len = o.length();
        for (int i = 0; i < len; i++) { 
            JSONObject elem = o.getJSONObject(i);
            if (elem.has("name") && elem.getString("name").toLowerCase().equals(paramName.toLowerCase())) { 
                return elem.getJSONObject("value"); 
            }
        }
        return null;
    }
    
    public static void putParam(JSONArray o, String paramName, Object paramValue) throws JSONException { 
        
        int len = o.length();
        boolean added = false; 
        
        // search for the parameter already in the array 
        for (int i = 0; i < len; i++) { 
            JSONObject elem = o.getJSONObject(i);
            if (elem.has("name") && elem.getString("name").toLowerCase().equals(paramName.toLowerCase())) { 
                elem.put("value", paramValue);
                added = true; 
            }
        }
        
        // add it to the array if not found already 
        if (!added) { 
            JSONObject elem = new JSONObject(); 
            elem.put("name", paramName);
            elem.put("value", paramValue); 
            o.put(elem);
        }
    }
    
    public static String[] getStringArray(JSONObject o, String key) throws JSONException { 
        return toStringArray(o.getJSONArray(key)); 
    }
    
    public static String[] getStringArray(JSONObject o, String key, String[] def) throws JSONException { 
        if (o.has(key))
            return getStringArray(o, key); 
        else 
            return def; 
    }
    
    public static String[] getStringArray(JSONArray o, int index) throws JSONException { 
        return toStringArray(o.getJSONArray(index)); 
    }
    
    public static String[] getStringArray(JSONArray o, int index, String[] def) throws JSONException { 
        if (index < o.length())
            return getStringArray(o, index); 
        else 
            return def; 
    }
        
    public static int[] getIntArray(JSONObject o, String key) throws JSONException { 
        return toIntArray(o.getJSONArray(key)); 
    }
    
    public static int[] getIntArray(JSONObject o, String key, int[] def) throws JSONException { 
        if (o.has(key))
            return getIntArray(o, key); 
        else 
            return def; 
    }
    
    public static int[] getIntArray(JSONArray o, int index) throws JSONException { 
        return toIntArray(o.getJSONArray(index)); 
    }
    
    public static int[] getIntArray(JSONArray o, int index, int[] def) throws JSONException { 
        if (index < o.length())
            return getIntArray(o, index); 
        else 
            return def; 
    }
    
    public static double[] getDoubleArray(JSONObject o, String key) throws JSONException { 
        return toDoubleArray(o.getJSONArray(key)); 
    }
    
    public static double[] getDoubleArray(JSONObject o, String key, double[] def) throws JSONException { 
        if (o.has(key))
            return getDoubleArray(o, key); 
        else 
            return def; 
    }

    public static double[] getDoubleArray(JSONArray o, int index) throws JSONException { 
        return toDoubleArray(o.getJSONArray(index));
    }
    
    public static double[] getDoubleArray(JSONArray o, int index, double[] def) throws JSONException { 
        if (index < o.length())
            return getDoubleArray(o, index); 
        else 
            return def; 
    }
    
    public static DateTime[] getDateTimeArray(JSONObject o, String key) throws JSONException, ParseException { 
        return toDateTimeArray(o.getJSONArray(key)); 
    }
    
    public static DateTime[] getDateTimeArray(JSONObject o, String key, DateTime[] def) throws JSONException, ParseException { 
        if (o.has(key))
            return getDateTimeArray(o, key); 
        else 
            return def; 
    }
    
    public static DateTime[] getDateTimeArray(JSONArray o, int index) throws JSONException, ParseException { 
        return toDateTimeArray(o.getJSONArray(index)); 
    }
    
    public static DateTime[] getDateTimeArray(JSONArray o, int index, DateTime[] def) throws JSONException, ParseException { 
        if (index < o.length())
            return getDateTimeArray(o, index); 
        else 
            return def; 
    }
    
    public static String[] toStringArray(JSONArray o) throws JSONException { 
        String[] d = new String[o.length()];
        for (int i = 0; i < d.length; i++) { 
            d[i] = o.getString(i);
        }
        return d;
    }

    public static int[] toIntArray(JSONArray o) throws JSONException { 
        int[] d = new int[o.length()];
        for (int i = 0; i < d.length; i++) { 
            d[i] = o.getInt(i);
        }
        return d;
    }

    public static double[] toDoubleArray(JSONArray o) throws JSONException { 
        double[] d = new double[o.length()];
        for (int i = 0; i < d.length; i++) { 
            d[i] = o.getDouble(i);
        }
        return d;
    }

    public static DateTime[] toDateTimeArray(JSONArray o) throws JSONException, ParseException { 
        DateTime[] d = new DateTime[o.length()];
        for (int i = 0; i < d.length; i++) { 
            d[i] = DateUtils.toDate(o.getString(i), DateUtils.DEFAULT_DATE_FORMAT);
        }
        return d;
    }
    
    public static List<Double> toDoubleList(JSONArray o) throws JSONException { 
        int len = o.length(); 
        List<Double> d = new ArrayList<>(len);
        for (int i = 0; i < len; i++) { 
            d.add(o.getDouble(i));
        }
        return d;
    }

    public static List<DateTime> toDateTimeList(JSONArray o) throws JSONException, ParseException { 
        int len = o.length(); 
        List<DateTime> d = new ArrayList<>(len); 
        for (int i = 0; i < len; i++) { 
            d.add(DateUtils.toDate(o.getString(i), DateUtils.DEFAULT_DATE_FORMAT));
        }
        return d;
    }
    
    public static JSONArray toJSONArray(double[] val) {
        JSONArray arr = new JSONArray(val.length);
        try {
            for (double d : val) {
                arr.put(d);
            }
        } catch (JSONException ex) {
            return arr;
        }
        return arr;
    }

    public static JSONArray toJSONArray(Double[] val) {
        JSONArray arr = new JSONArray(val.length);
        try {
            for (double d : val) {
                arr.put(d);
            }
        } catch (JSONException ex) {
            return arr;
        }
        return arr;
    }

    public static JSONArray toJSONArray(boolean[] val) {
        JSONArray arr = new JSONArray(val.length);
        for (boolean d : val) {
            arr.put(d);
        }
        return arr;
    }
    
    public static JSONArray toJSONArray(Boolean[] val) {
        JSONArray arr = new JSONArray(val.length);
        for (boolean d : val) {
            arr.put(d);
        }
        return arr;
    }

    public static JSONArray toJSONArray(long[] val) {
        JSONArray arr = new JSONArray(val.length);
        for (long d : val) {
            arr.put(d);
        }
        return arr;
    }
    
    public static JSONArray toJSONArray(Long[] val) {
        JSONArray arr = new JSONArray(val.length);
        for (long d : val) {
            arr.put(d);
        }
        return arr;
    }

    public static JSONArray toJSONArray(int[] val) {
        JSONArray arr = new JSONArray(val.length);
        for (int d : val) {
            arr.put(d);
        }
        return arr;
    }

    public static JSONArray toJSONArray(Integer[] val) {
        JSONArray arr = new JSONArray(val.length);
        for (int d : val) {
            arr.put(d);
        }
        return arr;
    }
    
    public static JSONArray toJSONArray(String[] val) {
        JSONArray arr = new JSONArray(val.length);
        for (String d : val) {
            arr.put(d);
        }
        return arr;
    }
    
    public static JSONArray toJSONArray(DateTime[] val) {
        JSONArray arr = new JSONArray(val.length);
        for (DateTime d : val) {
            arr.put(d.toString(DateUtils.DEFAULT_DATE_FORMAT));
        }
        return arr;
    }
    
    public static JSONObject copy(JSONObject o) throws JSONException { 
        
        // return null 
        if (o == null) { 
            return null; 
        }
        
        // build metadata from variable information
        return copyInto(new JSONObject(), o); 
    }
    
    public static JSONObject copyInto(JSONObject target, JSONObject other) throws JSONException { 
        
        // return null 
        if (other == null) { 
            return target; 
        }
        
        // update metadata with additional tags
        Iterator iter = other.keys();
        while (iter.hasNext()) {
            String key = iter.next().toString();
            target.put(key, other.get(key));
        }
        
        return target;
    }
}