Example usage for com.google.gwt.json.client JSONNumber JSONNumber

List of usage examples for com.google.gwt.json.client JSONNumber JSONNumber

Introduction

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

Prototype

public JSONNumber(double value) 

Source Link

Document

Creates a new JSONNumber from the double value.

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   www  .j a  va2s. co 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");
        }
    }
}

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

License:Open Source License

protected static void writeData(String key, Object value) {
    JSONObject data = getData();/*from   w w  w  . j a  v  a 2  s.c  o  m*/
    JSONValue jsonValue = null;
    if (value instanceof String) {
        jsonValue = new JSONString((String) value);
    } else if (value instanceof Boolean) {
        jsonValue = JSONBoolean.getInstance((Boolean) value);
    } else if (value instanceof Number) {
        jsonValue = new JSONNumber(((Number) value).doubleValue());
    }

    if (jsonValue == null) {
        logger.severe("Can not store given '" + key + "' value's type to data");
        return;
    }
    data.put(key, jsonValue);
    storeData();
}

From source file:org.waveprotocol.wave.examples.client.webclient.client.WaveWebSocketClient.java

License:Apache License

/**
 *
 * @param sequenceNo//from w ww .j a v  a2s .com
 * @param message
 * @param callback callback to invoke for response, or null for none.
 */
public void sendMessage(int sequenceNo, JavaScriptObject message, SubmitResponseCallback callback) {

    JSONObject wrapper = new JSONObject();
    wrapper.put("version", new JSONNumber(VERSION));
    wrapper.put("sequenceNumber", new JSONNumber(sequenceNo));
    final String protocolBufferName = ProtocolOpenRequest.getProtocolBufferName(message);
    wrapper.put("messageType", new JSONString(protocolBufferName));
    deleteMessageName(message);

    if (protocolBufferName.equals("ProtocolOpenRequest")) {
        wrapper.put("messageJson",
                new JSONString(ProtocolOpenRequest.stringify((ProtocolOpenRequest) message)));
    } else if (protocolBufferName.equals("ProtocolSubmitRequest")) {
        wrapper.put("messageJson",
                new JSONString(ProtocolSubmitRequest.stringify((ProtocolSubmitRequest) message)));
        submitRequestCallbacks.put(sequenceNo, callback);
    }
    String json = wrapper.toString();
    LOG.info("Sending JSON data " + json);
    client.send(json);
}

From source file:org.wisepersist.gwt.uploader.client.Configurable.java

License:Apache License

@SuppressWarnings("rawtypes")
private JSONValue convertToJSONValue(Object value) {
    if (value == null) {
        return JSONNull.getInstance();
    } else if (value instanceof JSONValue) {
        return (JSONValue) value;
    }/*w ww.j  a v a 2 s  . c  o  m*/
    if (value instanceof Boolean) {
        return JSONBoolean.getInstance((Boolean) value);
    } else if (value instanceof Number) {
        return new JSONNumber(((Number) value).doubleValue());
    } else if (value instanceof String) {
        return new JSONString((String) value);
    } else if (value instanceof JavaScriptObject) {
        return new JSONObject((JavaScriptObject) value);
    } else if (value instanceof Configurable) {
        return ((Configurable) value).getOptions();
    } else if (value.getClass().isArray()) {
        JSONArray jsonArray = new JSONArray();
        Object[] valueArray = (Object[]) value;
        for (int i = 0, valueArrayLength = valueArray.length; i < valueArrayLength; i++) {
            Object arrayValue = valueArray[i];
            jsonArray.set(i, convertToJSONValue(arrayValue));
        }
        return jsonArray;
    }
    return null;
}

From source file:org.zoxweb.client.data.JSONClientUtil.java

License:Apache License

/**
 * Converts NVEntity to JSONObject.//from  w w  w . j a v  a  2 s.  com
 * @param nve
 * @return
 */
@SuppressWarnings("unchecked")
public static JSONObject toJSON(NVEntity nve, boolean printClass) {
    SharedUtil.checkIfNulls("Null NVEntity", nve);
    JSONObject jsonObject = new JSONObject();

    NVConfigEntity nvce = (NVConfigEntity) nve.getNVConfig();
    if (printClass)
        jsonObject.put(MetaToken.CLASS_TYPE.getName(), new JSONString(nve.getClass().getName()));

    for (NVConfig nvc : nvce.getAttributes()) {
        //         if (nvc instanceof NVConfigEntity)
        //         {
        //            if (!nvc.isArray() && nve.lookupValue(nvc) != null)
        //            {
        //               jsonObject.put(nvc.getName(), toJSON((NVEntity) nve.lookupValue(nvc)));
        //            }
        //         }
        //         else if (nvc instanceof NVConfig)
        {
            if (!nvc.isArray()) {
                JSONValue jsonValue = null;
                Object value = nve.lookupValue(nvc);

                if (value != null) {
                    if (value instanceof NVEntity) {
                        jsonObject.put(nvc.getName(), toJSON((NVEntity) value, printClass));
                    } else if (nvc.getMetaTypeBase().equals(String.class)) {
                        jsonValue = new JSONString((String) value);
                    } else if (nvc.getMetaTypeBase().equals(Long.class)
                            || nvc.getMetaTypeBase().equals(Integer.class)
                            || nvc.getMetaTypeBase().equals(Float.class)
                            || nvc.getMetaTypeBase().equals(Double.class)
                            || nvc.getMetaTypeBase().equals(BigDecimal.class)) {
                        Number num = (Number) value;
                        if (num.doubleValue() != 0 || num.longValue() != 0 || num.intValue() != 0
                                || num.floatValue() != 0)
                            jsonValue = new JSONNumber(((Number) value).doubleValue());
                    } else if (nvc.getMetaTypeBase().equals(Date.class)) {
                        jsonValue = new JSONNumber(((Number) value).doubleValue());
                    } else if (nvc.getMetaTypeBase().equals(Boolean.class)) {
                        if ((Boolean) value) {
                            jsonValue = JSONBoolean.getInstance((Boolean) value);
                        }
                    } else if (value instanceof Enum) {
                        jsonValue = new JSONString(((Enum<?>) value).name());
                    } else if (nvc.getMetaTypeBase().equals(NVGenericMap.class)) {
                        jsonValue = genericMapToJSON((NVGenericMap) nve.lookup(nvc), printClass,
                                Base64Type.URL);
                    }

                    if (jsonValue != null) {
                        jsonObject.put(nvc.getName(), jsonValue);
                    }
                }
            } else {
                JSONArray jsonArray = new JSONArray();
                int counter = 0;
                NVBase<?> nvb = nve.lookup(nvc);
                Class<?> metaBase = nvc.getMetaTypeBase();

                if (metaBase == String.class) {
                    ArrayValues<NVPair> values = (ArrayValues<NVPair>) nvb;

                    for (NVPair nvp : values.values()) {
                        JSONObject nvpJSON = toJSON(nvp);

                        if (nvpJSON != null) {
                            jsonArray.set(counter++, nvpJSON);
                        }
                    }
                } else if (nvb instanceof NVEntityReferenceList || nvb instanceof NVEntityReferenceIDMap
                        || nvb instanceof NVEntityGetNameMap) {
                    ArrayValues<NVEntity> values = (ArrayValues<NVEntity>) nvb;

                    for (NVEntity nveTemp : values.values()) {
                        if (nveTemp != null) {
                            jsonArray.set(counter++, toJSON(nveTemp, printClass));
                        }
                    }
                } else if (metaBase == Long.class) {
                    NVLongList values = (NVLongList) nvb;

                    for (Long val : values.getValue()) {
                        if (val != null) {
                            jsonArray.set(counter++, new JSONNumber((double) val));
                        }
                    }
                } else if (metaBase == Integer.class) {
                    NVIntList values = (NVIntList) nvb;

                    for (Integer val : values.getValue()) {
                        if (val != null) {
                            jsonArray.set(counter++, new JSONNumber((double) val));
                        }
                    }
                } else if (metaBase == Float.class) {
                    NVFloatList values = (NVFloatList) nvb;

                    for (Float val : values.getValue()) {
                        if (val != null) {
                            jsonArray.set(counter++, new JSONNumber((double) val));
                        }
                    }
                } else if (metaBase == Double.class) {
                    NVDoubleList values = (NVDoubleList) nvb;

                    for (Double val : values.getValue()) {
                        if (val != null) {
                            jsonArray.set(counter++, new JSONNumber((double) val));
                        }
                    }
                } else if (metaBase == BigDecimal.class) {
                    NVBigDecimalList values = (NVBigDecimalList) nvb;

                    for (BigDecimal val : values.getValue()) {
                        if (val != null) {
                            jsonArray.set(counter++, new JSONNumber(val.doubleValue()));
                        }
                    }
                } else if (metaBase.isEnum()) {
                    NVEnumList values = (NVEnumList) nvb;

                    for (Enum<?> e : values.getValue()) {
                        if (e != null) {
                            jsonArray.set(counter++, new JSONString(e.name()));
                        }
                    }
                } else if (nvc.getMetaType() == byte[].class) {
                    if (nvb.getValue() != null) {
                        byte[] base64 = SharedBase64.encode(((NVBlob) nvb).getValue());
                        jsonObject.put(nvc.getName(), new JSONString(SharedStringUtil.toString(base64)));

                    }
                    // so we don't add the array
                    continue;
                }

                jsonObject.put(nvc.getName(), jsonArray);
                // we have a primitive array
            }
        }
    }

    return jsonObject;
}

From source file:org.zoxweb.client.data.JSONClientUtil.java

License:Apache License

public static JSONObject genericMapToJSON(NVGenericMap nvgm, boolean printClass, Base64Type b64Type) {
    JSONObject ret = null;/*from   ww w .  j  a  va  2s .  co  m*/
    if (nvgm != null) {
        ret = new JSONObject();
        for (GetNameValue<?> gnv : nvgm.values()) {
            JSONValue jsonValue = null;
            if (gnv.getValue() != null) {
                if (gnv instanceof NVBoolean) {
                    if (((NVBoolean) gnv).getValue()) {
                        jsonValue = JSONBoolean.getInstance((Boolean) gnv.getValue());
                    }
                } else if (gnv instanceof NVLong || gnv instanceof NVInt || gnv instanceof NVFloat
                        || gnv instanceof NVDouble || gnv instanceof NVBigDecimal) {
                    Number num = (Number) gnv.getValue();
                    if (num.doubleValue() != 0 || num.longValue() != 0 || num.intValue() != 0
                            || num.floatValue() != 0) {
                        jsonValue = new JSONNumber(((Number) num).doubleValue());
                    }
                } else if (gnv.getValue() instanceof String) {
                    jsonValue = new JSONString((String) gnv.getValue());
                } else if (gnv instanceof NVEnum) {
                    jsonValue = new JSONString(((Enum<?>) gnv.getValue()).name());
                } else if (gnv instanceof NVBlob) {
                    jsonValue = new JSONString(SharedBase64.encodeAsString(b64Type, (byte[]) gnv.getValue()));
                }

                if (jsonValue != null) {
                    ret.put(gnv.getName(), jsonValue);
                }
            }
        }
    }

    return ret;
}

From source file:org.zoxweb.client.data.JSONClientUtil.java

License:Apache License

/**
 *
 * @param qr//from   w w  w  .  ja  va2  s . com
 * @return
 */
public static JSONObject toJSONQuery(QueryRequest qr) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(MetaToken.CANONICAL_ID.getName(), new JSONString(qr.getCanonicalID()));

    jsonObject.put(BATCH_SIZE, new JSONNumber(qr.getBatchSize()));

    if (qr.getFieldNames() != null) {
        JSONArray jsonArray = new JSONArray();
        int i = 0;

        for (String fn : qr.getFieldNames()) {
            if (!SharedStringUtil.isEmpty(fn)) {
                jsonArray.set(i++, new JSONString(fn));
            }
        }

        jsonObject.put(FIELD_NAMES, jsonArray);
    }

    if (qr.getQuery() != null) {
        JSONArray jsonArray = new JSONArray();
        int i = 0;

        for (QueryMarker qm : qr.getQuery()) {
            if (qm != null) {
                JSONObject qmJSON = new JSONObject();

                if (qm instanceof GetNameValue) {
                    if (qm instanceof QueryMatch) {
                        QueryMatch<?> qMatch = (QueryMatch<?>) qm;
                        Object value = qMatch.getValue();

                        if (value instanceof Number) {
                            qmJSON.put(qMatch.getName(), new JSONNumber(((Number) value).doubleValue()));
                        } else if (value instanceof String) {
                            qmJSON.put(qMatch.getName(), new JSONString((String) value));
                        } else if (value instanceof Enum) {
                            qmJSON.put(qMatch.getName(), new JSONString(((Enum<?>) value).name()));
                        }

                        if (qMatch.getOperator() != null) {
                            qmJSON.put(MetaToken.RELATIONAL_OPERATOR.getName(),
                                    new JSONString(qMatch.getOperator().name()));
                        }
                    }
                } else if (qm instanceof LogicalOperator) {
                    qmJSON.put(MetaToken.LOGICAL_OPERATOR.getName(),
                            new JSONString(((LogicalOperator) qm).name()));
                }

                jsonArray.set(i++, qmJSON);
            }
        }

        jsonObject.put(QUERY, jsonArray);
    }

    return jsonObject;
}

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

License:Apache License

@Override
public JSONValue writeJson(final Object instance) {
    final Byte wrapper = (Byte) instance;
    return wrapper == null ? (JSONValue) JSONNull.getInstance() : new JSONNumber(wrapper.byteValue());
}

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

License:Apache License

public JSONValue writeJson(final byte byteValue) {
    return new JSONNumber(byteValue);
}

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

License:Apache License

@Override
public JSONValue writeJson(final Object instance) {
    final Date date = (Date) instance;
    return null == date ? (JSONValue) JSONNull.getInstance() : new JSONNumber(date.getTime());
}