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.eclipse.che.ide.editor.preferences.EditorPreferencesManager.java

License:Open Source License

private static Map<String, JSONValue> readPreferencesFromJson(String jsonPreferences) {
    Map<String, JSONValue> result = new HashMap<>();
    JSONValue parsed = JSONParser.parseStrict(jsonPreferences);

    JSONObject jsonObj = parsed.isObject();
    if (jsonObj != null) {
        jsonObj.keySet().forEach(key -> result.put(key, jsonObj.get(key)));
    }//ww w.  j  a  va 2  s . c  o  m
    return result;
}

From source file:org.eclipse.che.ide.editor.preferences.editorproperties.EditorPropertiesManager.java

License:Open Source License

private static Map<String, JSONValue> readPropertiesFromJson(String jsonProperties) {
    Map<String, JSONValue> result = new HashMap<>();
    JSONValue parsed = JSONParser.parseStrict(jsonProperties);

    JSONObject jsonObj = parsed.isObject();
    if (jsonObj != null) {
        for (String key : jsonObj.keySet()) {
            JSONValue jsonValue = jsonObj.get(key);
            result.put(key, jsonValue);/*from www  . j a  va2s .  co m*/
        }
    }
    return result;
}

From source file:org.eclipse.che.ide.ext.java.jdt.core.util.JSONUtil.java

License:Open Source License

/**
 * @param json/*  w w w. j  a  v  a 2s . c o  m*/
 * @return
 */
private static JSONValue parse(JSONValue value) {
    if (value.isObject() != null) {
        value = value.isObject().get("rsc").isArray();
    }
    return value;
}

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/*from   w  ww  .  j av  a  2s  . c o m*/
 *         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/*from   w  w  w .  ja v 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.jseditor.client.keymap.KeymapPrefReader.java

License:Open Source License

/**
 * Updates the keymap in preferences./*from w  w  w. j  a v a 2  s.  com*/
 *
 * @param preferencesManager
 *         the preferences manager
 * @param valuesHolder
 *         the object that contains the values to store
 */
public static void storePrefs(final PreferencesManager preferencesManager,
        final KeymapValuesHolder valuesHolder) {
    final String keymapPrefAsJson = preferencesManager.getValue(KEYMAP_PREF_KEY);

    JSONObject prefObject;
    if (keymapPrefAsJson == null) {
        prefObject = new JSONObject();
    } else {
        final JSONValue parseResult = JSONParser.parseStrict(keymapPrefAsJson);
        prefObject = parseResult.isObject();
    }

    for (final Entry<EditorType, Keymap> entry : valuesHolder) {
        if (entry.getKey() != null && entry.getValue() != null) {
            prefObject.put(entry.getKey().getEditorTypeKey(), new JSONString(entry.getValue().getKey()));
        }
    }

    final String newJson = prefObject.toString();
    preferencesManager.setValue(KEYMAP_PREF_KEY, newJson);
}

From source file:org.eclipse.che.ide.jseditor.client.prefmodel.EditorPreferenceReader.java

License:Open Source License

/**
 * Retrieves the editor preference object as stored in the preference json string.
 * @return the preference object or null
 *///from  w ww.  ja  va2s.  c  o m
private EditorPreferences getPreferencesOrNull() {
    final String prefAsJson = this.preferencesManager.getValue(PREFERENCE_PROPERTY);
    if (prefAsJson == null || prefAsJson.isEmpty()) {
        return null;
    }
    JSONValue propertyObject;
    try {
        final JSONValue parseResult = JSONParser.parseStrict(prefAsJson);
        propertyObject = parseResult.isObject();
    } catch (final RuntimeException e) {
        Log.error(KeymapPrefReader.class, "Error during preference parsing.", e);
        return null;
    }
    if (propertyObject == null) {
        return null;
    }
    JavaScriptObject propertyValue;
    try {
        propertyValue = propertyObject.isObject().getJavaScriptObject();
    } catch (final RuntimeException e) {
        Log.error(KeymapPrefReader.class, "Invalid value for editor preference.", e);
        return null;
    }
    return propertyValue.cast();
}

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  ww.  j a  v  a2 s  .c  o m
        }
    }

    return map;
}

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

License:Open Source License

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

    JSONValue parsed = JSONParser.parseStrict(jsonStr);
    JSONObject jsonObj = parsed.isObject();
    if (jsonObj != null) {
        for (String key : jsonObj.keySet()) {
            JSONValue jsonValue = jsonObj.get(key);
            JSONArray jsonArray = jsonValue.isArray();

            List<String> values = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                values.add(jsonArray.get(i).isString().stringValue());
            }// w  w w.j a v  a 2  s . c om
            map.put(key, values);
        }
    }

    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 Map<String, String> parseErrorAttributes(String parsedMessage) {
    try {//from   ww w .  j  a  va  2 s .co  m
        //parsed message
        JSONValue message = JSONParser.parseStrict(parsedMessage).isObject().get("attributes");
        return toMap(message.isObject().toString());
    } catch (Exception e) {
        //not found json in message
        return Collections.emptyMap();
    }
}