Example usage for com.google.gson JsonObject getAsJsonArray

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

Introduction

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

Prototype

public JsonArray getAsJsonArray(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonArray.

Usage

From source file:com.twitter.intellij.pants.service.project.model.TargetInfoDeserializer.java

License:Apache License

@Override
public TargetInfo deserialize(JsonElement element, Type type, final JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject object = element.getAsJsonObject();
    final List<String> targets = getStringListField(object, "targets");
    final List<String> libraries = getStringListField(object, "libraries");
    final List<String> excludes = getStringListField(object, "excludes");
    final List<SourceRoot> sourceRoots = getFieldAsList(object.getAsJsonArray("roots"),
            new Function<JsonElement, SourceRoot>() {
                @Override// w  w  w. j a  va 2 s.c o  m
                public SourceRoot fun(JsonElement element) {
                    return context.deserialize(element, SourceRoot.class);
                }
            });
    final TargetAddressInfo addressInfo = context.deserialize(element, TargetAddressInfo.class);
    return new TargetInfo(new HashSet<TargetAddressInfo>(Collections.singleton(addressInfo)),
            new HashSet<String>(targets), new HashSet<String>(libraries), new HashSet<String>(excludes),
            new HashSet<SourceRoot>(sourceRoots));
}

From source file:com.twitter.intellij.pants.service.project.model.TargetInfoDeserializer.java

License:Apache License

@NotNull
private List<String> getStringListField(JsonObject object, String memberName) {
    if (!object.has(memberName)) {
        return Collections.emptyList();
    }//from   w  ww.  j a  v  a2 s.  c om
    return getFieldAsList(object.getAsJsonArray(memberName), new Function<JsonElement, String>() {
        @Override
        public String fun(JsonElement element) {
            return element.getAsString();
        }
    });
}

From source file:com.vmware.eucenablement.horizontoolset.av.api.impl.VolumeImpl.java

/**
 * get all writable volumes from volume manager
 *
 * @return all writable volumes/*  w  ww.  jav  a 2 s .  c o m*/
 */
public List<Writable> listWritables() {
    List<Writable> writableList = null;
    try {
        String result = volumeHelper.requestProcess(null, "cv_api/writables", HTTPMethod.GET);

        if (result.contains("datastores") && result.contains("writable_volumes")) {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(result).getAsJsonObject();
            JsonObject datastores = jsonObject.getAsJsonObject("datastores");
            JsonArray jsonWritables = datastores.getAsJsonArray("writable_volumes");

            writableList = gson.fromJson(jsonWritables, new TypeToken<List<Writable>>() {
            }.getType());

        } else {
            LOG.warn(new Date().toString() + ": can not get app stacks, try latter");
        }
    } catch (Exception e) {
        LOG.error(new Date().toString() + ": can not get writables, try latter:" + e);
    }

    return writableList;
}

From source file:com.vmware.eucenablement.horizontoolset.av.api.impl.VolumeImpl.java

/**
 * get all available app stacks from volume manager
 *
 * @return all app stacks// w  w w . j  a va  2  s  . co m
 */
public List<AppStack> listAppStacks() {
    List<AppStack> appStackList = null;
    try {
        String result = volumeHelper.requestProcess(null, "cv_api/appstacks", HTTPMethod.GET);
        if (result.contains("currentappstacks") && result.contains("appstacks")) {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(result).getAsJsonObject();
            JsonObject currentappstacks = jsonObject.getAsJsonObject("currentappstacks");
            JsonArray appstacks = currentappstacks.getAsJsonArray("appstacks");

            appStackList = gson.fromJson(appstacks, new TypeToken<List<AppStack>>() {
            }.getType());

        } else {
            LOG.warn(new Date().toString() + ": can not get app stacks, try latter");
        }

    } catch (Exception e) {
        LOG.error(new Date().toString() + ": can not get app stacks, try latter:" + e);
    }
    return appStackList;
}

From source file:com.vmware.eucenablement.horizontoolset.av.api.impl.VolumeImpl.java

/**
 * get the available app stacks for computer
 *
 * @param id/* w  w  w.  j a  v a2s. co m*/
 *            of computer
 * @return app stacks which can be attached to the computer
 */
public List<AppStack> listAvailableAppStacks4Computer(long id) {
    List<AppStack> tmp = null;
    List<AppStack> appStackList = new ArrayList<>();
    try {
        String result = volumeHelper.requestProcess(null, "cv_api/computers/" + id + "/assignable_appstacks",
                HTTPMethod.GET);
        if (result.contains("appstacks")) {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(result).getAsJsonObject();
            JsonArray appstacks = jsonObject.getAsJsonArray("appstacks");
            tmp = gson.fromJson(appstacks, new TypeToken<List<AppStack>>() {
            }.getType());
            for (int i = 0; i < tmp.size(); i++) {
                if (tmp.get(i).status.equals("assigned") && tmp.get(i).assigned) {
                    continue;
                } else {
                    appStackList.add(tmp.get(i));
                }
            }
        }
    } catch (Exception e) {
        LOG.error(new Date().toString() + ": can not get available appstacks for computer " + id + ": " + e);
    }
    return appStackList;
}

From source file:com.vmware.eucenablement.horizontoolset.av.api.impl.VolumeImpl.java

/**
 * get app stacks which have been attached to the specified computer
 *
 * @param id// w  w  w.j a v a  2  s .  c o m
 *            of computer
 * @return list, app stacks which have been attached to the computer
 */
public List<AppStack> listAssignments4Computer(long id) {
    List<AppStack> tmp = null;
    List<AppStack> appStackList = new ArrayList<>();
    try {
        String result = volumeHelper.requestProcess(null, "cv_api/computers/" + id + "/assignable_appstacks",
                HTTPMethod.GET);
        if (result.contains("appstacks")) {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(result).getAsJsonObject();
            JsonArray appstacks = jsonObject.getAsJsonArray("appstacks");
            tmp = gson.fromJson(appstacks, new TypeToken<List<AppStack>>() {
            }.getType());
            for (int i = 0; i < tmp.size(); i++)
                if (tmp.get(i).status.equals("enabled") && !tmp.get(i).assigned)
                    continue;
                else
                    appStackList.add(tmp.get(i));

        }
    } catch (Exception e) {
        LOG.error(new Date().toString() + ": can not get assignments for computer " + id + ": " + e);
    }
    return appStackList;
}

From source file:com.vmware.eucenablement.horizontoolset.av.api.impl.VolumeImpl.java

/**
 * get the available app stacks for user
 *
 * @param id/*from  w  w w  .  j a  v  a 2s. c o  m*/
 *            of user
 * @return app stacks which can be attached to the user
 */
public List<AppStack> listAvailableAppStacks4User(long id) {
    List<AppStack> tmp = null;
    List<AppStack> appStackList = new ArrayList<>();
    try {
        String result = volumeHelper.requestProcess(null, "cv_api/users/" + id + "/assignable_appstacks",
                HTTPMethod.GET);
        if (result.contains("appstacks")) {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(result).getAsJsonObject();
            JsonArray appstacks = jsonObject.getAsJsonArray("appstacks");
            tmp = gson.fromJson(appstacks, new TypeToken<List<AppStack>>() {
            }.getType());
            for (int i = 0; i < tmp.size(); i++)
                if (tmp.get(i).status.equals("assigned") && tmp.get(i).assigned)
                    continue;
                else
                    appStackList.add(tmp.get(i));

        }
    } catch (Exception e) {
        LOG.error(new Date().toString() + ": can not get available appstacks for user " + id + ": " + e);
    }
    return appStackList;
}

From source file:com.vmware.eucenablement.horizontoolset.av.api.impl.VolumeImpl.java

/**
 * get app stacks which have been attached to the specified user
 *
 * @param id/*from  w  w  w  . java 2 s  .  c  o  m*/
 *            of user
 * @return list, app stacks which have been attached to the user
 */
public List<AppStack> listAssignments4User(long id) {
    List<AppStack> tmp = null;
    List<AppStack> appStackList = new ArrayList<>();
    try {
        String result = volumeHelper.requestProcess(null, "cv_api/users/" + id + "/assignable_appstacks",
                HTTPMethod.GET);
        if (result.contains("appstacks")) {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(result).getAsJsonObject();
            JsonArray appstacks = jsonObject.getAsJsonArray("appstacks");
            tmp = gson.fromJson(appstacks, new TypeToken<List<AppStack>>() {
            }.getType());
            for (int i = 0; i < tmp.size(); i++)
                if (tmp.get(i).status.equals("enabled") && !tmp.get(i).assigned)
                    continue;
                else
                    appStackList.add(tmp.get(i));

        }
    } catch (Exception e) {
        LOG.error(new Date().toString() + ": can not get assignments for user " + id + ": " + e);
    }
    return appStackList;
}

From source file:com.vmware.eucenablement.horizontoolset.av.api.impl.VolumeImpl.java

/**
 * get the applications an app stack include
 *
 * @param id//from w  w  w . j  ava  2 s.  co m
 *            of app stack
 * @return list, applications information
 */
public List<Application> listApplication4Stack(long id) {
    List<Application> applicationList = null;
    try {
        String result = volumeHelper.requestProcess(null, "cv_api/appstacks/" + id + "/applications?id=" + id,
                HTTPMethod.GET);
        if (result.contains("applications")) {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(result).getAsJsonObject();
            JsonArray appstacks = jsonObject.getAsJsonArray("applications");
            applicationList = gson.fromJson(appstacks, new TypeToken<List<Application>>() {
            }.getType());
        }
    } catch (Exception e) {
        LOG.error(new Date().toString() + ": can not get applications for app stack " + id + ": " + e);
    }
    return applicationList;
}

From source file:com.vmware.eucenablement.horizontoolset.av.api.impl.VolumeImpl.java

/**
 * get the online entites currently/*  w ww  .ja  v a2  s  . co  m*/
 *
 * @return list, online entities
 */
public List<OnlineEntity> listOnlineEntities() {
    List<OnlineEntity> onlineEntityList = null;
    try {
        String result = volumeHelper.requestProcess(null, "cv_api/online_entities", HTTPMethod.GET);
        if (result.contains("online") && result.contains("records")) {
            Gson gson = new Gson();
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(result).getAsJsonObject();
            JsonObject onlines = jsonObject.getAsJsonObject("online");
            JsonArray records = onlines.getAsJsonArray("records");
            onlineEntityList = gson.fromJson(records, new TypeToken<List<OnlineEntity>>() {
            }.getType());
        }

    } catch (Exception e) {
        LOG.error(new Date().toString() + ": can not get online entity: " + e);
    }
    return onlineEntityList;
}