Example usage for com.google.gwt.json.client JSONValue isBoolean

List of usage examples for com.google.gwt.json.client JSONValue isBoolean

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONValue isBoolean.

Prototype

public JSONBoolean isBoolean() 

Source Link

Document

Returns a non-null reference if this JSONValue is really a JSONBoolean.

Usage

From source file:rocket.json.client.BooleanJsonSerializer.java

License:Apache License

public boolean read(final JSONValue jsonValue) {
    boolean value = false;

    while (true) {
        if (null == jsonValue) {
            break;
        }//  www.  ja  va  2s  . co  m
        final JSONBoolean jsonBoolean = jsonValue.isBoolean();
        if (null == jsonBoolean) {
            break;
        }

        value = jsonBoolean.booleanValue();
        break;
    }

    return value;
}

From source file:rpc.client.data.JSONSerializer.java

License:Open Source License

private Object fromJSONValue(JSONValue jsonValue, Type expected) throws NoSuitableSerializableFactory {

    if (jsonValue == null) {
        return null;
    }/*from   w w w  .  j  a  v a  2s . co m*/

    // Null
    JSONNull asNull = jsonValue.isNull();
    if (asNull != null) {
        return null;
    }

    // Boolean
    JSONBoolean asBoolean = jsonValue.isBoolean();
    if (asBoolean != null) {
        return asBoolean.booleanValue();
    }

    // Integer
    // Long
    // Float
    // Double
    JSONNumber asNumber = jsonValue.isNumber();
    if (asNumber != null) {
        double value = asNumber.doubleValue();

        if (expected.isInteger()) {
            return (int) value;
        }

        if (expected.isLong()) {
            return (long) value;
        }

        if (expected.isFloat()) {
            return (float) value;
        }

        if (expected.isDouble()) {
            return value;
        }
    }

    // String
    // Enum
    JSONString asString = jsonValue.isString();
    if (asString != null) {
        if (expected.isEnum()) {
            String value = asString.stringValue();
            return Enum.valueOf((Class) expected.getTypeClass(), value);
        } else {
            return asString.stringValue();
        }
    }

    // Map
    // Serializable
    JSONObject asObject = jsonValue.isObject();
    if (asObject != null) {
        if (expected.isMap()) {
            Map<Object, Object> map = new HashMap<Object, Object>();

            Type keyType = expected.getParameterized(0);
            Type valueType = expected.getParameterized(1);

            if (!(keyType.isString() || keyType.isEnum())) {
                return null;
            }

            for (String key : asObject.keySet()) {
                JSONValue value = asObject.get(key);

                if (keyType.isString()) {
                    map.put(key, fromJSONValue(value, valueType));
                }

                if (keyType.isEnum()) {
                    map.put(Enum.valueOf((Class) keyType.getTypeClass(), key), fromJSONValue(value, valueType));
                }
            }

            return map;
        } else {
            if (provider == null) {
                throw new NoSuitableSerializableFactory();
            }

            Serializable object = provider.make(expected);

            for (Map.Entry<String, Type> entry : object.fields().entrySet()) {

                String field = entry.getKey();
                Type fieldType = entry.getValue();

                JSONValue value = asObject.get(field);
                object.set(field, fromJSONValue(value, fieldType));
            }

            return object;
        }
    }

    // List
    JSONArray asArray = jsonValue.isArray();
    if (asArray != null) {
        int size = asArray.size();

        List<Object> list = new ArrayList<Object>();
        Type itemType = expected.getParameterized(0);

        for (int i = 0; i < size; i++) {
            JSONValue value = asArray.get(i);
            list.add(fromJSONValue(value, itemType));
        }

        return list;
    }

    return null;
}