Package org.gwtoolbox.commons.json.client

Source Code of org.gwtoolbox.commons.json.client.JSONUtils$ObjectIterator

package org.gwtoolbox.commons.json.client;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import org.gwtoolbox.commons.collections.client.ReadOnlyIterator;

import java.util.Iterator;

/**
* @author Uri Boness
*/
public class JSONUtils {

    private JSONUtils() {
    }

    public static Boolean getBoolean(JSONObject object, String key) {
        JSONValue value = object.get(key);
        return value == null || value.isNull() != null ? null : value.isBoolean().booleanValue();
    }

    public static boolean getBoolean(JSONObject object, String key, boolean defaultValue) {
        Boolean value = getBoolean(object, key);
        return value == null ? defaultValue : value;
    }

    public static Integer getInt(JSONObject object, String key) {
        JSONValue value = object.get(key);
        return value == null || value.isNull() != null ? null : (int) value.isNumber().doubleValue();
    }

    public static int getInt(JSONObject object, String key, int defaultValue) {
        Integer value = getInt(object, key);
        return value == null ? defaultValue : value;
    }

    public static Long getLong(JSONObject object, String key) {
        JSONValue value = object.get(key);
        return value == null || value.isNull() != null ? null : (long) value.isNumber().doubleValue();
    }

    public static long getLong(JSONObject object, String key, long defaultValue) {
        Long value = getLong(object, key);
        return value == null ? defaultValue : value;
    }

    public static Float getFloat(JSONObject object, String key) {
        JSONValue value = object.get(key);
        return value == null || value.isNull() != null ? null : (float) value.isNumber().doubleValue();
    }

    public static float getFloat(JSONObject object, String key, float defaultValue) {
        Float value = getFloat(object, key);
        return value == null ? defaultValue : value;
    }

    public static Double getDouble(JSONObject object, String key) {
        JSONValue value = object.get(key);
        return value == null || value.isNull() != null ? null : value.isNumber().doubleValue();
    }

    public static double getFloat(JSONObject object, String key, double defaultValue) {
        Double value = getDouble(object, key);
        return value == null ? defaultValue : value;
    }

    public static String getString(JSONObject object, String key) {
        JSONValue value = object.get(key);
        if (value == null || value.isNull() != null) {
            return  null;
        }
        if (value.isString() == null) {
            throw new RuntimeException("Expected string value for key [" + key + "] but found '" + String.valueOf(value) + "' instead");
        }
        return  value.isString().stringValue();
    }

    public static String getString(JSONObject object, String key, String defaultValue) {
        String value = getString(object, key);
        return value == null ? defaultValue : value;
    }

    public static JSONObject getObject(JSONObject object, String key) {
        JSONValue value = object.get(key);
        return value == null || value.isNull() != null ? null : value.isObject();
    }

    public static JSONArray getArray(JSONObject object, String key) {
        JSONValue value = object.get(key);
        return value == null || value.isNull() != null ? null : value.isArray();
    }

    public static Iterable<JSONObject> getObjects(JSONObject object, String key) {
        final JSONArray array = getArray(object, key);
        return new Iterable<JSONObject>() {
            public Iterator<JSONObject> iterator() {
                return new ObjectIterator(array);
            }
        };
    }


    //================================================= Inner Classes ==================================================

    private static class ObjectIterator extends ReadOnlyIterator<JSONObject> {

        private final JSONArray array;

        private int i = 0;

        private ObjectIterator(JSONArray array) {
            this.array = array;
        }

        public boolean hasNext() {
            return i < array.size();
        }

        public JSONObject next() {
            return array.get(i++).isObject();
        }
    }
}
TOP

Related Classes of org.gwtoolbox.commons.json.client.JSONUtils$ObjectIterator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.