All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redmine.ta.internal.json.JsonInput Maven / Gradle / Ivy

Go to download

Free open-source Java API for Redmine and Chiliproject bug/task management systems.

There is a newer version: 1.15.0
Show newest version
package org.redmine.ta.internal.json;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

public class JsonInput {
	/**
	 * Parses required item list.
	 * 
	 * @param obj
	 *            object to extract a list from.
	 * @param field
	 *            field to parse.
	 * @param parser
	 *            single item parser.
	 * @return parsed objects.
	 * @throws JSONException
	 *             if format is invalid.
	 */
	public static  List getListNotNull(JSONObject obj, String field,
			JsonObjectParser parser) throws JSONException {
		final JSONArray items = getArrayNotNull(obj, field);
		final int length = items.length();
		final List result = new ArrayList(length);
		for (int i = 0; i < length; i++)
			result.add(parser.parse(items.getJSONObject(i)));
		return result;
	}

	/**
	 * Parses optional item list.
	 * 
	 * @param obj
	 *            object to extract a list from.
	 * @param field
	 *            field to parse.
	 * @param parser
	 *            single item parser.
	 * @return parsed objects.
	 * @throws JSONException
	 *             if format is invalid.
	 */
	public static  List getListOrNull(JSONObject obj, String field,
			JsonObjectParser parser) throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		final JSONArray items = obj.getJSONArray(field);
		final int length = items.length();
		final List result = new ArrayList(length);
		for (int i = 0; i < length; i++)
			result.add(parser.parse(items.getJSONObject(i)));
		return result;
	}

	/**
	 * Parses optional item list.
	 * 
	 * @param obj
	 *            object to extract a list from.
	 * @param field
	 *            field to parse.
	 * @param parser
	 *            single item parser.
	 * @return parsed objects.
	 * @throws JSONException
	 *             if format is invalid.
	 */
	public static  List getListOrEmpty(JSONObject obj, String field,
			JsonObjectParser parser) throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return new ArrayList();
		final JSONArray items = obj.getJSONArray(field);
		if (items == null)
			return new ArrayList();
		final int length = items.length();
		final List result = new ArrayList(length);
		for (int i = 0; i < length; i++)
			result.add(parser.parse(items.getJSONObject(i)));
		return result;
	}

	/**
	 * Fetch a date or null.
	 * 
	 * @param obj
	 *            object to get.
	 * @param field
	 *            field to use.
	 * @param dateFormat
	 *            field date format.
	 * @return data format.
	 * @throws JSONException
	 *             if error occurs.
	 */
	public static Date getDateOrNull(JSONObject obj, String field,
			final SimpleDateFormat dateFormat) throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		final String guess = obj.getString(field);
		try {
			return dateFormat.parse(guess);
		} catch (ParseException e) {
            try {
                return getISO8610DateOrNull(guess);
            } catch (ParseException e1) {
                try {
                    return getISO8610ShortDateOrNull(guess);
                } catch (ParseException e2) {
                    throw new JSONException("Bad date value " + guess);
                }
            }
		}
	}

    private static Date getISO8610DateOrNull(String input) throws ParseException {
                //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks
        //things a bit.  Before we go on we have to repair this.
        SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );

        //this is zero time so we need to add that TZ indicator for
        if ( input.endsWith( "Z" ) ) {
            input = input.substring( 0, input.length() - 1) + "GMT-00:00";
        } else {
            int inset = 6;

            String s0 = input.substring( 0, input.length() - inset );
            String s1 = input.substring( input.length() - inset, input.length() );

            input = s0 + "GMT" + s1;
        }
        return df.parse( input );
    }

    private static Date getISO8610ShortDateOrNull(String input) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
        return df.parse( input );
    }

	/**
	 * Fetches an optional string from an object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @throws JSONException
	 *             if value is not valid
	 */
	public static String getStringOrNull(JSONObject obj, String field)
			throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		return obj.getString(field);
	}

	/**
	 * Fetches an optional string from an object. Absent value is returned as an
	 * empty string instead of null.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @throws JSONException
	 *             if value is not valid
	 */
	public static String getStringOrEmpty(JSONObject obj, String field)
			throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return "";
		return obj.getString(field);
	}

	/**
	 * Fetches a string from an object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @throws JSONException
	 *             if value is not valid, not exists, etc...
	 */
	public static String getStringNotNull(JSONObject obj, String field)
			throws JSONException {
		return obj.getString(field);
	}

	/**
	 * Fetches an int from an object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @throws JSONException
	 *             if value is not valid, not exists, etc...
	 */
	public static int getInt(JSONObject obj, String field) throws JSONException {
		return obj.getInt(field);
	}

	/**
	 * Fetches an int from an object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @param deflt
	 *            default value.
	 * @throws JSONException
	 *             if value is not valid, not exists, etc...
	 */
	public static int getInt(JSONObject obj, String field, int deflt)
			throws JSONException {
		return obj.optInt(field, deflt);
	}

	/**
	 * Fetches an optional int from an object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @throws JSONException
	 *             if value is not valid, not exists, etc...
	 */
	public static Integer getIntOrNull(JSONObject obj, String field)
			throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		return obj.getInt(field);
	}

	/**
	 * Fetches a long from an object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @throws JSONException
	 *             if value is not valid, not exists, etc...
	 */
	public static long getLong(JSONObject obj, String field)
			throws JSONException {
		return obj.getLong(field);
	}

	/**
	 * Fetches an optional long from an object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @throws JSONException
	 *             if value is not valid, not exists, etc...
	 */
	public static Long getLongOrNull(JSONObject obj, String field)
			throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		return obj.getLong(field);
	}

	/**
	 * Fetches an optional float from an object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            field to get a value from.
	 * @throws JSONException
	 *             if value is not valid, not exists, etc...
	 */
	public static Float getFloatOrNull(JSONObject obj, String field)
			throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		return (float) obj.getDouble(field);
	}

	/**
	 * Retreive optional object.
	 * 
	 * @param obj
	 *            object to parse.
	 * @param field
	 *            field part.
	 * @param parser
	 *            parset ojbect.
	 * @return parsed object.
	 * @throws JSONException
	 *             if value is not valid.
	 */
	public static  T getObjectOrNull(JSONObject obj, String field,
			JsonObjectParser parser) throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		return parser.parse(obj.getJSONObject(field));
	}

	/**
	 * Returns a json array as "not-null" value.
	 * 
	 * @param obj
	 *            object to get a value from.
	 * @param field
	 *            field to get a value from.
	 * @return json array.
	 */
	public static JSONArray getArrayOrNull(JSONObject obj, String field)
			throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		return obj.getJSONArray(field);
	}

	/**
	 * Returns a json array as "not-null" value.
	 * 
	 * @param obj
	 *            object to get a value from.
	 * @param field
	 *            field to get a value from.
	 * @return json array.
	 */
	public static JSONArray getArrayNotNull(JSONObject obj, String field)
			throws JSONException {
		return obj.getJSONArray(field);
	}

	/**
	 * Returns a json object field for a specified object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            returned field.
	 * @return object field.
	 * @throws JSONException
	 *             if target field is not an object.
	 */
	public static JSONObject getObjectNotNull(JSONObject obj, String field)
			throws JSONException {
		return obj.getJSONObject(field);
	}

	/**
	 * Returns a json object field for a specified object.
	 * 
	 * @param obj
	 *            object to get a field from.
	 * @param field
	 *            returned field.
	 * @return object field.
	 * @throws JSONException
	 *             if target field is not an object.
	 */
	public static JSONObject getObjectOrNull(JSONObject obj, String field)
			throws JSONException {
		if (!obj.has(field) || obj.isNull(field))
			return null;
		return obj.getJSONObject(field);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy