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

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

Introduction

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

Prototype

public JSONString isString() 

Source Link

Document

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

Usage

From source file:org.dashbuilder.displayer.client.json.DisplayerSettingsJSONMarshaller.java

License:Apache License

private String getNodeValue(JSONObject node, String path) {
    if (node == null || StringUtils.isBlank(path))
        return null;

    int separatorIndex = path.lastIndexOf(".");
    String subNodesPath = separatorIndex > 0 ? path.substring(0, separatorIndex) : "";
    String leaf = separatorIndex > 0 ? path.substring(separatorIndex + 1) : path;

    JSONObject childNode = findNode(node, subNodesPath, false);
    String value = null;//from w ww  .  j  a  v  a2 s .c o  m
    if (childNode != null) {
        JSONValue jsonValue = childNode.get(leaf);
        if (jsonValue != null && jsonValue.isString() != null)
            value = jsonValue.isString().stringValue();
    }
    return value;
}

From source file:org.dashbuilder.displayer.client.json.DisplayerSettingsJSONMarshaller.java

License:Apache License

private void fillRecursive(String parentPath, JSONObject json, Map<String, String> settings) {
    String sb = new String(StringUtils.isBlank(parentPath) ? "" : parentPath + ".");
    for (String key : json.keySet()) {
        String path = sb + key;//w  w w.j  a va2s.co  m
        JSONValue value = json.get(key);
        if (value.isObject() != null)
            fillRecursive(path, value.isObject(), settings);
        else if (value.isString() != null)
            settings.put(path, value.isString().stringValue());
    }
}

From source file:org.drools.workbench.screens.scenariosimulation.client.collectioneditor.CollectionPresenter.java

License:Apache License

/**
 *
 * @param jsonObject/* w ww .  j a v a2 s .  c  o  m*/
 * @return a <code>Map</code> with <b>propertyName/propertyValue</b>
 */
protected Map<String, String> getSimplePropertiesMap(JSONObject jsonObject) {
    Map<String, String> toReturn = new HashMap<>();
    jsonObject.keySet().forEach(propertyName -> {
        final JSONValue jsonValue = jsonObject.get(propertyName);
        if (jsonValue.isString() != null) {
            toReturn.put(propertyName, jsonValue.isString().stringValue());
        }
    });
    return toReturn;
}

From source file:org.eclipse.che.api.languageserver.util.EitherUtil.java

License:Open Source License

private static boolean matches(JSONValue element, JsonDecision decision) {
    if (decision == JsonDecision.LIST) {
        return element.isArray() != null;
    }//w w w . j  av  a 2s.c  o m
    if (decision == JsonDecision.BOOLEAN) {
        return element.isBoolean() != null;
    }
    if (decision == JsonDecision.NUMBER) {
        return element.isNumber() != null;
    }
    if (decision == JsonDecision.STRING) {
        return element.isString() != null;
    }
    return element.isObject() != null;
}

From source file:org.eclipse.che.ide.jseditor.client.editortype.EditorTypeMappingImpl.java

License:Open Source License

@Override
public void loadFromPreferences() {
    final String pref = this.preferencesManager.getValue(PREFERENCE_PROPERTY_NAME);
    if (pref != null && !pref.isEmpty()) {
        final JSONObject keyMapping = JSONParser.parseStrict(pref).isObject();
        this.contentTypeMapping.clear();
        for (final String key : keyMapping.keySet()) {
            // the mime-type is stored in preferences
            final String contentType = key;
            final JSONValue value = keyMapping.get(key);
            if (value == null) {
                Log.warn(EditorTypeMappingImpl.class,
                        "Error in preferences: filetype " + contentType + " has null editor type set.");
                continue;
            }//from   w  ww.  ja  v  a 2s . co  m
            final String stringValue = value.isString().stringValue();
            final EditorType editorType = EditorType.getInstance(stringValue);
            if (editorType != null) {
                // special case for text/plain <-> defaultPlainTextFileType
                if (CONTENT_TYPE_TEXT_PLAIN.equals(contentType)) {
                    this.contentTypeMapping.put(plainTextFileType, editorType);
                } else {
                    final FileType fileType = fileTypeRegistry.getFileTypeByMimeType(contentType);
                    // any unknown mime type returns the default filetype ; ignore them
                    if (!unknownFileType.equals(fileType)) {
                        this.contentTypeMapping.put(fileType, editorType);
                    }
                }
            } else {
                Log.warn(EditorTypeMappingImpl.class,
                        "Unknown editor type key found for filetype " + contentType + ": " + stringValue);
            }
        }
    } else {
        Log.debug(EditorTypeMappingImpl.class, "No editor type mappings found in preferences.");
    }
}

From source file:org.eclipse.che.ide.jseditor.client.keymap.KeymapPrefReader.java

License:Open Source License

/**
 * Reads the keymap preference for the given editor.
 *
 * @param preferencesManager//w w  w. j ava  2 s . c  om
 *         the preferences manager
 * @param editorKey
 *         the editor key
 * @return the keymap in preference or null if none is set
 */
public static String readPref(final PreferencesManager preferencesManager, final String editorKey) {
    final String keymapPrefAsJson = preferencesManager.getValue(KEYMAP_PREF_KEY);
    if (keymapPrefAsJson == null || keymapPrefAsJson.isEmpty()) {
        return null;
    }
    JSONValue propertyObject;
    try {
        final JSONValue parseResult = JSONParser.parseStrict(keymapPrefAsJson);
        propertyObject = parseResult.isObject().get(editorKey);
    } catch (final RuntimeException e) {
        Log.error(KeymapPrefReader.class, "Error during preference parsing.", e);
        return null;
    }
    if (propertyObject == null) {
        return null;
    }
    String propertyValue;
    try {
        propertyValue = propertyObject.isString().stringValue();
    } catch (final RuntimeException e) {
        Log.error(KeymapPrefReader.class, "Invalid value for keymap preference.", e);
        return null;
    }
    return propertyValue;
}

From source file:org.eclipse.che.ide.jseditor.client.keymap.KeymapPrefReader.java

License:Open Source License

/**
 * Reads the keymap preferences and fills the {@link KeymapValuesHolder} instance.
 *
 * @param preferencesManager/*w w  w  . j  av a  2 s  .c om*/
 *         the preferences manager
 * @param valuesHolder
 *         the object that keeps the values
 */
public static void readPref(final PreferencesManager preferencesManager,
        final KeymapValuesHolder valuesHolder) {
    final String keymapPrefAsJson = preferencesManager.getValue(KEYMAP_PREF_KEY);
    if (keymapPrefAsJson == null || keymapPrefAsJson.isEmpty()) {
        return;
    }
    JSONObject propertyObject;
    try {
        final JSONValue parseResult = JSONParser.parseStrict(keymapPrefAsJson);
        propertyObject = parseResult.isObject();
    } catch (final RuntimeException e) {
        Log.error(KeymapPrefReader.class, "Error during preference parsing.", e);
        return;
    }
    for (final String key : propertyObject.keySet()) {
        final JSONValue value = propertyObject.get(key);
        if (value == null) {
            continue;
        }
        String valueString = null;
        try {
            valueString = value.isString().stringValue();
        } catch (final ClassCastException e) {
            Log.warn(KeymapPrefReader.class,
                    "Incorrect value type for keymap preference for editor " + key + ": " + value);
            continue;
        }
        if (valueString != null) {
            EditorType editorType = null;
            Keymap keymap = null;
            try {
                editorType = EditorType.fromKey(key);
                keymap = Keymap.fromKey(valueString);
            } catch (final RuntimeException e) {
                Log.error(KeymapPrefReader.class, "Invalid value for keymap preference.", e);
                continue;
            }
            if (editorType != null && keymap != null) {
                valuesHolder.setKeymap(editorType, keymap);
            }
        }
    }
}

From source file:org.eclipse.che.ide.json.JsonHelper.java

License:Open Source License

public static Map<String, String> toMap(String jsonStr) {
    Map<String, String> map = new HashMap<String, String>();

    JSONValue parsed = JSONParser.parseStrict(jsonStr);
    JSONObject jsonObj = parsed.isObject();
    if (jsonObj != null) {
        for (String key : jsonObj.keySet()) {
            JSONValue jsonValue = jsonObj.get(key);
            JSONString jsonString = jsonValue.isString();
            // if the json value is a string, set the unescaped value, else set the json representation of the value
            String stringValue = (jsonString == null) ? jsonValue.toString() : jsonString.stringValue();
            map.put(key, stringValue);//from w  w w. ja  v  a  2s.  c  o  m
        }
    }

    return map;
}

From source file:org.eclipse.che.ide.json.JsonHelper.java

License:Open Source License

/** Returns message or result of it parse if the message is json. */
public static String parseJsonMessage(String parsedMessage) {
    try {//  w w w  .  j a va  2s. c  o  m
        //parsed message
        JSONValue message = JSONParser.parseStrict(parsedMessage).isObject().get("message");
        return message.isString().stringValue();
    } catch (Exception e) {
        //not found json in message
        return parsedMessage;
    }
}

From source file:org.eclipse.che.ide.jsonrpc.impl.ResponseDispatcher.java

License:Open Source License

private String getId(JSONObject incomingJson) {
    final JSONValue idValue = incomingJson.get("id");
    final JSONString idString = idValue.isString();
    if (idString == null) {
        return Long.toString((long) idValue.isNumber().doubleValue());
    } else {//from w  ww . ja  v a  2s  . c  om
        return idString.stringValue();
    }
}