Example usage for com.vaadin.client JsArrayObject size

List of usage examples for com.vaadin.client JsArrayObject size

Introduction

In this page you can find the example usage for com.vaadin.client JsArrayObject size.

Prototype

public native int size()
    ;

Source Link

Usage

From source file:org.vaadin.alump.offlinebuilder.client.offline.JsonEncoder.java

License:Open Source License

/**
 * Encode a value to a JSON representation for transport from the client to
 * the server.//from   w  ww  .  j  a va 2 s.c  o  m
 *
 * @param value
 *            value to convert
 * @param connection
 * @return JSON representation of the value
 */
public static JSONValue encode(Object value, Type type, ApplicationConnection connection) {
    if (null == value) {
        return JSONNull.getInstance();
    } else if (value instanceof JSONValue) {
        return (JSONValue) value;
    } else if (value instanceof String[]) {
        String[] array = (String[]) value;
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < array.length; ++i) {
            jsonArray.set(i, new JSONString(array[i]));
        }
        return jsonArray;
    } else if (value instanceof String) {
        return new JSONString((String) value);
    } else if (value instanceof Boolean) {
        return JSONBoolean.getInstance((Boolean) value);
        // --- added to this class to get offline mode working (start) ---
    } else if (value instanceof Short) {
        return new JSONNumber(((Short) value).shortValue());
    } else if (value instanceof Integer) {
        return new JSONNumber(((Integer) value).intValue());
    } else if (value instanceof Long) {
        return new JSONNumber(((Long) value).longValue());
    } else if (value instanceof Double) {
        return new JSONNumber(((Double) value).doubleValue());
    } else if (value instanceof Float) {
        return new JSONNumber(((Float) value).floatValue());
        // --- added to this class to get offline mode working (end) ---
    } else if (value instanceof Byte) {
        return new JSONNumber((Byte) value);
    } else if (value instanceof Character) {
        return new JSONString(String.valueOf(value));
    } else if (value instanceof Object[] && type == null) {
        // Non-legacy arrays handed by generated serializer
        return encodeLegacyObjectArray((Object[]) value, connection);
    } else if (value instanceof Enum) {
        return encodeEnum((Enum<?>) value, connection);
    } else if (value instanceof Map) {
        return encodeMap((Map) value, type, connection);
    } else if (value instanceof Connector) {
        Connector connector = (Connector) value;
        return new JSONString(connector.getConnectorId());
    } else if (value instanceof Collection) {
        return encodeCollection((Collection) value, type, connection);
    } else if (value instanceof UidlValue) {
        return encodeVariableChange((UidlValue) value, connection);
    } else {
        // First see if there's a custom serializer
        JSONSerializer<Object> serializer = null;
        if (type != null) {
            serializer = (JSONSerializer<Object>) type.findSerializer();
            if (serializer != null) {
                return serializer.serialize(value, connection);
            }
        }

        String transportType = getTransportType(value);
        if (transportType != null) {
            // Send the string value for remaining legacy types
            return new JSONString(String.valueOf(value));
        } else if (type != null) {
            // And finally try using bean serialization logic
            try {
                JsArrayObject<Property> properties = type.getPropertiesAsArray();

                JSONObject jsonObject = new JSONObject();

                int size = properties.size();
                for (int i = 0; i < size; i++) {
                    Property property = properties.get(i);
                    Object propertyValue = property.getValue(value);
                    Type propertyType = property.getType();
                    JSONValue encodedPropertyValue = encode(propertyValue, propertyType, connection);
                    jsonObject.put(property.getName(), encodedPropertyValue);
                }
                return jsonObject;

            } catch (NoDataException e) {
                throw new RuntimeException("Can not encode " + type.getSignature(), e);
            }

        } else {
            throw new RuntimeException("Can't encode " + value.getClass() + " without type information");
        }
    }
}