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

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

Introduction

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

Prototype

public JSONObject isObject() 

Source Link

Document

Returns non-null if this JSONValue is really a JSONObject.

Usage

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

License:Open Source License

protected void readState(OfflineConnector connector) {
    JSONValue jsonState = OfflineStorage.getStateJson(connector.getConnectorId());
    SharedState state = decodeState(jsonState, connector.getState(), connector.getConnection());
    if (state != null) {
        connector.onOfflineState(state, jsonState.isObject());
        List<String> children = OfflineStorage.getChildren(connector.getConnectorId());

        if (children != null) {
            List<OfflineConnector> added = new ArrayList<OfflineConnector>();
            for (String pid : children) {
                OfflineConnector childConnector = getOfflineConnector(pid);
                added.add(childConnector);
            }// w w  w. jav a 2  s  .c o  m
            if (!added.isEmpty()) {
                OfflineUtil.handleOfflineConnectorChange((OfflineContainerConnector) connector, added);
            }
            for (OfflineConnector child : added) {
                readState(child);
            }
        }
    } else {
        logger.severe("Failed to read state of component " + connector.getConnectorId()
                + ", leaving it uninitialized.");
    }
}

From source file:org.waveprotocol.wave.client.gadget.renderer.GadgetMetadata.java

License:Apache License

/**
 * Helper function to extract a JSONObject from a JSONValue if it exists.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the JSONObject if it exists./* w  ww  .j a  v a 2 s . co  m*/
 */
private static JSONObject getJsonObjectValue(JSONObject json, String key) {
    JSONValue value = json.get(key);
    return (value != null) ? value.isObject() : null;
}

From source file:org.waveprotocol.wave.client.wavepanel.impl.toolbar.gadget.GwtGadgetInfoParser.java

License:Apache License

private GadgetInfo parseGadgetInfo(JSONValue item) {
    JSONObject object = item.isObject();
    if (object != null) {
        String name = object.get("name").isString().stringValue();
        String desc = object.get("desc").isString().stringValue();
        GadgetCategoryType primaryCategory = GadgetCategoryType
                .of(object.get("primaryCategory").isString().stringValue());
        GadgetCategoryType secondaryCategory = GadgetCategoryType
                .of(object.get("secondaryCategory").isString().stringValue());
        String gadgetUrl = object.get("gadgetUrl").isString().stringValue();
        String author = object.get("author").isString().stringValue();
        String submittedBy = object.get("submittedBy").isString().stringValue();
        String imageUrl = object.get("imageUrl").isString().stringValue();

        return new GadgetInfo(name, desc, primaryCategory, secondaryCategory, gadgetUrl, author, submittedBy,
                imageUrl);//from  w w  w.j ava2  s  .c o  m
    }
    return null;
}

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

License:Apache License

public WaveWebSocketClient(final WaveWebSocketCallback callback) {
    submitRequestCallbacks = new HashMap<Integer, SubmitResponseCallback>();
    WebSocketCallback wsCallback = new WebSocketCallback() {

        public void onConnect() {
            connected = ConnectState.CONNECTED;
            callback.connected();/*from w ww  .j  a va 2  s.co m*/
        }

        public void onDisconnect() {
            connected = ConnectState.DISCONNECTED;
            callback.disconnected();
        }

        public void onMessage(final String message) {
            LOG.info("received JSON message " + message);
            JSONValue json = JSONParser.parse(message);
            // TODO(arb): pull apart the wrapper, extract the message.
            JSONObject wrapper = json.isObject();
            String messageType = wrapper.get("messageType").isString().stringValue();
            String payload = wrapper.get("messageJson").isString().stringValue();
            if (messageType.equals("ProtocolWaveletUpdate")) {
                ProtocolWaveletUpdate payloadMessage = ProtocolWaveletUpdate.parse(payload);
                callback.handleWaveletUpdate(payloadMessage);
            } else if (messageType.equals("ProtocolSubmitResponse")) {
                ProtocolSubmitResponse payloadMessage = ProtocolSubmitResponse.parse(payload);
                SubmitResponseCallback submitCallback = submitRequestCallbacks
                        .get((int) wrapper.get("sequenceNumber").isNumber().doubleValue());
                submitCallback.run(payloadMessage);
            }
        }
    };

    client = new WebSocket(wsCallback);
}

From source file:org.xwiki.gwt.wysiwyg.client.MenuItemDescriptorJSONParser.java

License:Open Source License

/**
 * Creates a menu item descriptor from the given JSON value.
 * // ww w . ja v a 2 s .co m
 * @param value a JSON value
 * @return a menu item descriptor
 */
private MenuItemDescriptor getMenuItemDescriptor(JSONValue value) {
    JSONString feature = value.isString();
    if (feature != null) {
        return new MenuItemDescriptor(feature.stringValue());
    } else {
        JSONObject jsDescriptor = value.isObject();
        if (jsDescriptor == null) {
            return null;
        }
        JSONValue oFeature = jsDescriptor.get("feature");
        if (oFeature == null || oFeature.isString() == null) {
            return null;
        }
        MenuItemDescriptor descriptor = new MenuItemDescriptor(oFeature.isString().stringValue());
        JSONValue subMenu = jsDescriptor.get("subMenu");
        if (subMenu != null) {
            descriptor.getSubMenu().addAll(getMenuItemDescriptors(subMenu));
        }
        return descriptor;
    }
}

From source file:pt.ist.processpedia.client.translator.JsonTranslator.java

License:Open Source License

public Object parse(String externalizedObject) throws CannotTranslateObjectException {
    JSONValue jsonValue = parseJson(externalizedObject);
    JSONObject jsonObject = jsonValue.isObject();
    if (jsonObject != null) {
        if (jsonObject.containsKey(ID_KEY)) {
            return parseObject(jsonObject);
        } else if (jsonObject.containsKey(ERROR_CODE_KEY)) {
            return parseException(jsonObject);
        } else {/*w  ww  .j  av  a  2s  .  c  o m*/
            throw new CannotTranslateObjectException(externalizedObject);
        }
    } else {
        throw new CannotTranslateObjectException(externalizedObject);
    }
}

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

License:Apache License

public Map<String, Object> readMap(final JSONValue jsonValue) {
    final Map<String, Object> map = new HashMap<String, Object>();
    if (null != jsonValue) {
        final JSONObject object = jsonValue.isObject();
        final Iterator<String> keys = object.keySet().iterator();
        while (keys.hasNext()) {
            final String key = (String) keys.next();
            final JSONValue value = (JSONValue) object.get(key);

            final Object mapValue = this.readObject(value);

            map.put(key, mapValue);/*from   w ww. j a va2 s .  c om*/
        }
    }
    return map;
}

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;
    }/* ww  w . j ava 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;
}

From source file:stroom.dashboard.client.vis.PostMessage.java

License:Apache License

private static void receiveMessage(final MessageEvent event) {
    try {//  w  w w . j  a va2 s.c o  m
        final JSONValue json = JSONParser.parseStrict(event.getData());
        final JSONObject message = json.isObject();
        final Integer frameId = JSONUtil.getInteger(message.get("frameId"));
        if (frameId == null) {
            Window.alert("PostMessage - receiveMessage() - Null frame id");
        } else {
            final FrameListener frameListener = frameListeners.get(frameId);
            if (frameListener != null) {
                frameListener.receiveMessage(event, message);
            }
        }
    } catch (final Exception e) {
        Window.alert("PostMessage - receiveMessage() - " + e.getMessage());
    }
}

From source file:stroom.util.client.JSONUtil.java

License:Apache License

public static JSONObject getObject(final JSONValue v) {
    if (v != null) {
        return v.isObject();
    }//www  .j a  v a  2  s .c o m
    return null;
}