Search in sources :

Example 1 with AddContentOverlay

use of org.dartlang.analysis.server.protocol.AddContentOverlay in project intellij-plugins by JetBrains.

the class RequestUtilities method buildJsonElement.

@VisibleForTesting
public static JsonElement buildJsonElement(Object object) {
    if (object instanceof Boolean) {
        return new JsonPrimitive((Boolean) object);
    } else if (object instanceof Number) {
        return new JsonPrimitive((Number) object);
    } else if (object instanceof String) {
        return new JsonPrimitive((String) object);
    } else if (object instanceof List<?>) {
        List<?> list = (List<?>) object;
        JsonArray jsonArray = new JsonArray();
        for (Object item : list) {
            JsonElement jsonItem = buildJsonElement(item);
            jsonArray.add(jsonItem);
        }
        return jsonArray;
    } else if (object instanceof Map<?, ?>) {
        Map<?, ?> map = (Map<?, ?>) object;
        JsonObject jsonObject = new JsonObject();
        for (Entry<?, ?> entry : map.entrySet()) {
            Object key = entry.getKey();
            // prepare string key
            String keyString;
            if (key instanceof String) {
                keyString = (String) key;
            } else {
                throw new IllegalArgumentException("Unable to convert to string: " + getClassName(key));
            }
            // prepare JsonElement value
            Object value = entry.getValue();
            JsonElement valueJson = buildJsonElement(value);
            // put a property into the JSON object
            if (keyString != null && valueJson != null) {
                jsonObject.add(keyString, valueJson);
            }
        }
        return jsonObject;
    } else if (object instanceof AnalysisError) {
        return buildJsonObjectAnalysisError((AnalysisError) object);
    } else if (object instanceof AddContentOverlay) {
        return ((AddContentOverlay) object).toJson();
    } else if (object instanceof ChangeContentOverlay) {
        return ((ChangeContentOverlay) object).toJson();
    } else if (object instanceof RemoveContentOverlay) {
        return ((RemoveContentOverlay) object).toJson();
    } else if (object instanceof AnalysisOptions) {
        return ((AnalysisOptions) object).toJson();
    } else if (object instanceof Location) {
        return buildJsonObjectLocation((Location) object);
    }
    throw new IllegalArgumentException("Unable to convert to JSON: " + object);
}
Also used : AddContentOverlay(org.dartlang.analysis.server.protocol.AddContentOverlay) JsonPrimitive(com.google.gson.JsonPrimitive) AnalysisOptions(org.dartlang.analysis.server.protocol.AnalysisOptions) AnalysisError(org.dartlang.analysis.server.protocol.AnalysisError) ChangeContentOverlay(org.dartlang.analysis.server.protocol.ChangeContentOverlay) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) RemoveContentOverlay(org.dartlang.analysis.server.protocol.RemoveContentOverlay) JsonElement(com.google.gson.JsonElement) List(java.util.List) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) Location(org.dartlang.analysis.server.protocol.Location) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 List (java.util.List)1 Map (java.util.Map)1 AddContentOverlay (org.dartlang.analysis.server.protocol.AddContentOverlay)1 AnalysisError (org.dartlang.analysis.server.protocol.AnalysisError)1 AnalysisOptions (org.dartlang.analysis.server.protocol.AnalysisOptions)1 ChangeContentOverlay (org.dartlang.analysis.server.protocol.ChangeContentOverlay)1 Location (org.dartlang.analysis.server.protocol.Location)1 RemoveContentOverlay (org.dartlang.analysis.server.protocol.RemoveContentOverlay)1