Example usage for com.google.gson JsonElement getAsJsonArray

List of usage examples for com.google.gson JsonElement getAsJsonArray

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonArray.

Prototype

public JsonArray getAsJsonArray() 

Source Link

Document

convenience method to get this element as a JsonArray .

Usage

From source file:com.skysql.manager.api.ChartProperties.java

License:Open Source License

public ChartProperties deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    ChartProperties chartProperties = new ChartProperties();

    JsonElement jsonElement = json.getAsJsonObject().get("chartProperties");
    if (jsonElement != null && !jsonElement.isJsonNull()) {

        LinkedHashMap<String, ArrayList<ChartMappings>> chartsMap = new LinkedHashMap<String, ArrayList<ChartMappings>>();
        chartProperties.setChartsMap(chartsMap);

        JsonArray array = jsonElement.getAsJsonArray();
        for (int i = 0; i < array.size(); i++) {
            JsonObject jsonObject = array.get(i).getAsJsonObject();

            String systemType = (jsonElement = jsonObject.get("systemtype")).isJsonNull() ? null
                    : jsonElement.getAsString();
            JsonArray mappingJson = jsonObject.get("mappings").getAsJsonArray();
            int length = mappingJson.size();

            ArrayList<ChartMappings> chartsList = new ArrayList<ChartMappings>(length);

            for (int j = 0; j < length; j++) {
                JsonObject mappingObject = mappingJson.get(j).getAsJsonObject();

                JsonElement element;/* ww w . j  ava2  s.  co m*/
                String name = (element = mappingObject.get("name")).isJsonNull() ? null : element.getAsString();
                String description = (element = mappingObject.get("description")).isJsonNull() ? null
                        : element.getAsString();
                String unit = (element = mappingObject.get("unit")).isJsonNull() ? null : element.getAsString();
                String type = (element = mappingObject.get("type")).isJsonNull() ? null : element.getAsString();
                int points = (element = mappingObject.get("points")).isJsonNull() ? null : element.getAsInt();
                element = mappingObject.get("monitorIDs");
                ArrayList<String> monitorIDs = new ArrayList<String>();
                if (element != null && !element.isJsonNull()) {
                    JsonArray IDs = element.getAsJsonArray();
                    for (int k = 0; k < IDs.size(); k++) {
                        String id = String.valueOf(IDs.get(k).getAsString());
                        monitorIDs.add(id);
                    }
                }
                ChartMappings chartMapping = new ChartMappings(name, description, unit, type, points,
                        monitorIDs);
                chartsList.add(chartMapping);
            }

            chartsMap.put(systemType, chartsList);
        }

    }

    jsonElement = json.getAsJsonObject().get("chartSettings");
    if (jsonElement != null && !jsonElement.isJsonNull()) {
        JsonObject settingsJson = jsonElement.getAsJsonObject();
        JsonElement element;
        chartProperties.timeSpan = (element = settingsJson.get("timeSpan")).isJsonNull() ? 0
                : element.getAsInt();
        chartProperties.theme = (element = settingsJson.get("theme")).isJsonNull() ? null
                : element.getAsString();
    }

    return chartProperties;

}

From source file:com.skysql.manager.api.CommandsDeserializer.java

License:Open Source License

public Commands deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    Commands commands = new Commands();

    JsonElement jsonElement = json.getAsJsonObject().get("commands");
    if (jsonElement.isJsonNull()) {
        commands.setDescriptions(null);//from  ww w .  j  av  a2  s  .  co m
        commands.setNames(null);
        commands.setSteps(null);
    } else {
        JsonArray array = jsonElement.getAsJsonArray();
        int length = array.size();

        LinkedHashMap<String, String> descriptions = new LinkedHashMap<String, String>(length);
        LinkedHashMap<String, String> names = new LinkedHashMap<String, String>(length);
        LinkedHashMap<String, String> steps = new LinkedHashMap<String, String>(length);
        for (int i = 0; i < length; i++) {
            JsonObject pair = array.get(i).getAsJsonObject();
            String command = pair.get("command").getAsString();
            descriptions.put(command, pair.get("description").getAsString());
            names.put(command, command);
            steps.put(command, pair.get("steps").getAsString());
        }
        commands.setDescriptions(descriptions);
        commands.setNames(names);
        commands.setSteps(steps);
    }

    return commands;
}

From source file:com.skysql.manager.api.CommandStates.java

License:Open Source License

public CommandStates deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    CommandStates commandStates = new CommandStates();

    JsonElement jsonElement = json.getAsJsonObject().get("commandStates");
    if (jsonElement.isJsonNull()) {
        commandStates.setDescriptions(null);
    } else {/* w  w  w .j ava2 s .c o  m*/
        JsonArray array = jsonElement.getAsJsonArray();
        int length = array.size();

        LinkedHashMap<String, String> descriptions = new LinkedHashMap<String, String>(length);
        List<CommandStates.States> validStates = Arrays.asList(CommandStates.States.values());
        for (int i = 0; i < length; i++) {
            JsonObject pair = array.get(i).getAsJsonObject();
            String state = pair.get("state").getAsString();
            try {
                if (validStates.contains(CommandStates.States.valueOf(state))) {
                    descriptions.put(state, pair.get("description").getAsString());
                }
            } catch (IllegalArgumentException e) {
                new ErrorDialog(e, "Unknown Command State (" + state + ") found in API response");
                throw new RuntimeException("Unknown Command State (" + state + ") found in API response");
            }
        }
        commandStates.setDescriptions(descriptions);
    }

    return commandStates;
}

From source file:com.skysql.manager.api.MonitorData.java

License:Open Source License

public MonitorData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    MonitorData monitorData = new MonitorData();

    JsonElement jsonElement = json.getAsJsonObject().get("monitor_data");
    if (jsonElement.isJsonNull() || jsonElement.isJsonArray()) {
        return monitorData;
    }//from w ww .  j a va2 s.  co m

    JsonObject jsonObject = jsonElement.getAsJsonObject();

    if (jsonObject.has("timestamp")) {
        JsonElement jsonTime = jsonElement.getAsJsonObject().get("timestamp");
        if (jsonTime != null && !jsonTime.isJsonNull()) {
            JsonArray array = jsonTime.getAsJsonArray();
            int length = array.size();
            ArrayList<Long> timeStamps = new ArrayList<Long>(length);
            for (int i = 0; i < length; i++) {
                Long timeStamp = array.get(i).getAsLong();
                timeStamps.add(timeStamp);
            }
            monitorData.setTimeStamps(timeStamps);
        }
    }

    if (jsonObject.has("value")) {
        JsonElement jsonValue = jsonElement.getAsJsonObject().get("value");
        if (jsonValue != null && !jsonValue.isJsonNull()) {
            JsonArray array = jsonValue.getAsJsonArray();
            int length = array.size();
            ArrayList<Number> avgPoints = new ArrayList<Number>(length);
            for (int i = 0; i < length; i++) {
                Double dataPoint = array.get(i).getAsDouble();
                avgPoints.add(sanitize(dataPoint));
            }
            monitorData.setAvgPoints(avgPoints);
        }
    }

    if (jsonObject.has("min")) {
        JsonElement jsonValue = jsonElement.getAsJsonObject().get("min");
        if (jsonValue != null && !jsonValue.isJsonNull()) {
            JsonArray array = jsonValue.getAsJsonArray();
            int length = array.size();
            ArrayList<Number> minPoints = new ArrayList<Number>(length);
            for (int i = 0; i < length; i++) {
                Double dataPoint = array.get(i).getAsDouble();
                minPoints.add(sanitize(dataPoint));
            }
            monitorData.setMinPoints(minPoints);
        }
    }

    if (jsonObject.has("max")) {
        JsonElement jsonValue = jsonElement.getAsJsonObject().get("max");
        if (jsonValue != null && !jsonValue.isJsonNull()) {
            JsonArray array = jsonValue.getAsJsonArray();
            int length = array.size();
            ArrayList<Number> maxPoints = new ArrayList<Number>(length);
            for (int i = 0; i < length; i++) {
                Double dataPoint = array.get(i).getAsDouble();
                maxPoints.add(sanitize(dataPoint));
            }
            monitorData.setMaxPoints(maxPoints);
        }
    }

    return monitorData;

}

From source file:com.skysql.manager.api.MonitorDataRaw.java

License:Open Source License

public MonitorDataRaw deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    MonitorDataRaw monitorData = new MonitorDataRaw();

    JsonElement jsonElement = json.getAsJsonObject().get("monitor_rawdata");
    if (jsonElement.isJsonNull() || jsonElement.isJsonArray()) {
        return monitorData;
    }//from   w ww  .j  a  v a  2s .  co  m

    JsonObject jsonObject = jsonElement.getAsJsonObject();

    if (jsonObject.has("timestamp")) {
        JsonElement jsonTime = jsonElement.getAsJsonObject().get("timestamp");
        if (jsonTime != null && !jsonTime.isJsonNull()) {
            JsonArray array = jsonTime.getAsJsonArray();
            int length = array.size();
            ArrayList<Long> timeStamps = new ArrayList<Long>(length);
            for (int i = 0; i < length; i++) {
                Long timeStamp = array.get(i).getAsLong();
                timeStamps.add(timeStamp);
            }
            monitorData.setTimeStamps(timeStamps);
        }
    }

    if (jsonObject.has("value")) {
        JsonElement jsonValue = jsonElement.getAsJsonObject().get("value");
        if (jsonValue != null && !jsonValue.isJsonNull()) {
            JsonArray array = jsonValue.getAsJsonArray();
            int length = array.size();
            ArrayList<Double> dataPoints = new ArrayList<Double>(length);
            for (int i = 0; i < length; i++) {
                Double dataPoint = array.get(i).getAsDouble();
                dataPoints.add(dataPoint);
            }
            monitorData.setDataPoints(dataPoints);
        }
    }

    return monitorData;
}

From source file:com.skysql.manager.api.Monitors.java

License:Open Source License

public Monitors deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    Monitors monitors = new Monitors();

    JsonArray array = null;//from   w ww  .j  a  va2 s.  co m
    JsonObject object = null;

    if (json.getAsJsonObject().has("monitorclasses")) {
        JsonElement monitorsElement = json.getAsJsonObject().get("monitorclasses");
        if (monitorsElement.isJsonArray()) {
            array = monitorsElement.getAsJsonArray();
        } else {
            object = monitorsElement.getAsJsonObject();
        }
    } else if (json.getAsJsonObject().has("monitorclass")) {
        array = json.getAsJsonObject().get("monitorclass").getAsJsonArray();
    } else {
        monitors.setMonitorsMap(null);
        return monitors;
    }

    LinkedHashMap<String, LinkedHashMap<String, MonitorRecord>> monitorsMap = new LinkedHashMap<String, LinkedHashMap<String, MonitorRecord>>();
    monitors.setMonitorsMap(monitorsMap);
    for (String type : SystemTypes.getList().keySet()) {
        monitorsMap.put(type, new LinkedHashMap<String, MonitorRecord>());
    }

    if (array != null) {
        parseMonitors(array, monitorsMap);
    } else {
        for (String type : SystemTypes.getList().keySet()) {
            array = object.get(type).getAsJsonArray();
            parseMonitors(array, monitorsMap);
        }
    }

    return monitors;
}

From source file:com.skysql.manager.api.NodeStates.java

License:Open Source License

public NodeStates deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    NodeStates nodeStates = new NodeStates();

    JsonElement jsonElement = json.getAsJsonObject().get("nodestates");
    if (jsonElement != null && !jsonElement.isJsonNull()) {

        LinkedHashMap<String, NodeStates> stateRecords = new LinkedHashMap<String, NodeStates>();
        Set<Entry<String, JsonElement>> set = jsonElement.getAsJsonObject().entrySet();
        Iterator<Entry<String, JsonElement>> iter = set.iterator();
        while (iter.hasNext()) {
            Entry<String, JsonElement> entry = iter.next();
            JsonElement element = entry.getValue();
            String systemType = entry.getKey();
            JsonArray statesArray = element.isJsonNull() ? null : element.getAsJsonArray();
            NodeStates states = parseStates(systemType, statesArray);
            stateRecords.put(systemType, states);
        }/*from w  w w  .  ja  va 2 s. c  o  m*/
        nodeStates.setNodeStates(stateRecords);

    }

    return nodeStates;
}

From source file:com.skysql.manager.api.Steps.java

License:Open Source License

public Steps deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {

    Steps steps = new Steps();

    JsonElement jsonElement = json.getAsJsonObject().get("command_steps");
    if (jsonElement.isJsonNull()) {
        steps.setStepsList(null);//from   ww w.  j a  v a2s .  co  m
    } else {
        JsonArray array = jsonElement.getAsJsonArray();
        int length = array.size();

        LinkedHashMap<String, String> stepsList = new LinkedHashMap<String, String>(length);
        for (int i = 0; i < length; i++) {
            JsonObject backupJson = array.get(i).getAsJsonObject();

            JsonElement element;
            String id = (element = backupJson.get("step")).isJsonNull() ? null : element.getAsString();
            String description = (element = backupJson.get("description")).isJsonNull() ? null
                    : element.getAsString();
            stepsList.put(id, description);
        }
        steps.setStepsList(stepsList);
    }
    return steps;

}

From source file:com.skysql.manager.api.SystemInfo.java

License:Open Source License

public SystemInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    SystemInfo systemInfo = new SystemInfo();

    JsonArray array = null;/*from  w  w  w  .  j a v a2  s  .  c  o  m*/

    int length = 0;
    if (json.getAsJsonObject().has("systems")) {
        array = json.getAsJsonObject().get("systems").getAsJsonArray();
        length = array.size();
    } else if (json.getAsJsonObject().has("system")) {
        length = 1;
    }

    LinkedHashMap<String, SystemRecord> systemsMap = new LinkedHashMap<String, SystemRecord>(length);
    systemInfo.setSystemsMap(systemsMap);
    if (length == 0) {
        return systemInfo;
    }

    for (int i = 0; i < length; i++) {

        SystemRecord systemRecord = new SystemRecord(SystemInfo.SYSTEM_ROOT);

        JsonObject systemObject = (array != null) ? array.get(i).getAsJsonObject()
                : json.getAsJsonObject().get("system").getAsJsonObject();
        JsonElement element;
        systemRecord
                .setID((element = systemObject.get("systemid")).isJsonNull() ? null : element.getAsString());
        systemRecord.setSystemType(
                (element = systemObject.get("systemtype")).isJsonNull() ? null : element.getAsString());
        systemRecord.setName((element = systemObject.get("name")).isJsonNull() ? null : element.getAsString());
        systemRecord
                .setState((element = systemObject.get("state")).isJsonNull() ? null : element.getAsString());
        systemRecord.setStartDate(
                (element = systemObject.get("started")).isJsonNull() ? null : element.getAsString());
        systemRecord.setLastAccess(
                (element = systemObject.get("lastaccess")).isJsonNull() ? null : element.getAsString());
        systemRecord.setLastBackup(
                (element = systemObject.get("lastbackup")).isJsonNull() ? null : element.getAsString());
        systemRecord.setDBUsername(
                (element = systemObject.get("dbusername")).isJsonNull() ? null : element.getAsString());
        systemRecord.setDBPassword(
                (element = systemObject.get("dbpassword")).isJsonNull() ? null : element.getAsString());
        systemRecord.setRepUsername(
                (element = systemObject.get("repusername")).isJsonNull() ? null : element.getAsString());
        systemRecord.setRepPassword(
                (element = systemObject.get("reppassword")).isJsonNull() ? null : element.getAsString());
        systemRecord.setLastMonitored(
                ((element = systemObject.get("lastmonitored")).isJsonNull()) ? null : element.getAsString());

        MonitorLatest monitorLatest = null;
        if ((element = systemObject.get("monitorlatest")) != null && !element.isJsonNull()) {
            monitorLatest = APIrestful.getGson().fromJson(element.toString(), MonitorLatest.class);
        }
        systemRecord.setMonitorLatest(monitorLatest);

        String[] nodes = null;
        if ((element = systemObject.get("nodes")) != null && !element.isJsonNull()) {
            JsonArray nodesJson = element.getAsJsonArray();
            int nodesCount = nodesJson.size();

            nodes = new String[nodesCount];
            for (int nodesIndex = 0; nodesIndex < nodesCount; nodesIndex++) {
                nodes[nodesIndex] = nodesJson.get(nodesIndex).getAsString();
            }
        }
        systemRecord.setNodes(nodes);

        LinkedHashMap<String, String> properties = new LinkedHashMap<String, String>();
        if ((element = systemObject.get("properties")) != null && !element.isJsonNull()) {

            JsonObject propertiesJson = element.getAsJsonObject();

            if ((element = propertiesJson.get(SystemInfo.PROPERTY_EIP)) != null) {
                properties.put(SystemInfo.PROPERTY_EIP, element.isJsonNull() ? null : element.getAsString());
            }

            if ((element = propertiesJson.get(SystemInfo.PROPERTY_MONYOG)) != null) {
                properties.put(SystemInfo.PROPERTY_MONYOG, element.isJsonNull() ? null : element.getAsString());
            }

            if ((element = propertiesJson.get(SystemInfo.PROPERTY_PHPMYADMIN)) != null) {
                properties.put(SystemInfo.PROPERTY_PHPMYADMIN,
                        element.isJsonNull() ? null : element.getAsString());
            }

            if ((element = propertiesJson.get(SystemInfo.PROPERTY_DEFAULTMONITORINTERVAL)) != null) {
                properties.put(SystemInfo.PROPERTY_DEFAULTMONITORINTERVAL,
                        element.isJsonNull() ? null : element.getAsString());
            }

            if ((element = propertiesJson.get(SystemInfo.PROPERTY_DEFAULTMAXBACKUPCOUNT)) != null) {
                properties.put(SystemInfo.PROPERTY_DEFAULTMAXBACKUPCOUNT,
                        element.isJsonNull() ? null : element.getAsString());
            }

            if ((element = propertiesJson.get(SystemInfo.PROPERTY_DEFAULTMAXBACKUPSIZE)) != null) {
                properties.put(SystemInfo.PROPERTY_DEFAULTMAXBACKUPSIZE,
                        element.isJsonNull() ? null : element.getAsString());
            }

            if ((element = propertiesJson.get(SystemInfo.PROPERTY_VERSION)) != null) {
                properties.put(SystemInfo.PROPERTY_VERSION,
                        element.isJsonNull() ? null : element.getAsString());
            }

            if ((element = propertiesJson.get(SystemInfo.PROPERTY_SKIPLOGIN)) != null) {
                properties.put(SystemInfo.PROPERTY_SKIPLOGIN,
                        element.isJsonNull() ? null : element.getAsString());
            }

        }
        systemRecord.setProperties(properties);

        systemsMap.put(systemRecord.getID(), systemRecord);
    }

    if (array != null) {
        // create a "ROOT" system record to contain the series of flat systems; in a hierarchical organization of systems, this might be provided by the API
        SystemRecord rootRecord = new SystemRecord(null);
        rootRecord.setID(SystemInfo.SYSTEM_ROOT);
        rootRecord.setName("Root");
        String[] systems = new String[systemsMap.keySet().size()];
        int i = 0;
        for (String systemID : systemsMap.keySet()) {
            systems[i++] = systemID;
        }
        rootRecord.setNodes(systems);
        systemsMap.put(SystemInfo.SYSTEM_ROOT, rootRecord);
    }

    return systemInfo;
}

From source file:com.skysql.manager.api.UserInfo.java

License:Open Source License

public UserInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException, NullPointerException {
    UserInfo userInfo = new UserInfo();

    JsonElement jsonElement = json.getAsJsonObject().get("users");
    if (jsonElement.isJsonNull()) {
        userInfo.setUsersList(new LinkedHashMap<String, UserObject>());
    } else {/*w w w. j  a  va 2  s  .c  om*/
        JsonArray array = jsonElement.getAsJsonArray();
        int length = array.size();

        LinkedHashMap<String, UserObject> usersList = new LinkedHashMap<String, UserObject>(length);
        for (int i = 0; i < length; i++) {
            JsonObject backupJson = array.get(i).getAsJsonObject();
            JsonElement element;
            String username = (element = backupJson.get("username")).isJsonNull() ? null
                    : element.getAsString();
            String name = (element = backupJson.get("name")).isJsonNull() ? null : element.getAsString();
            UserObject userObject = new UserObject(username, name);
            usersList.put(username, userObject);
        }
        userInfo.setUsersList(usersList);
    }

    return userInfo;
}