Example usage for com.google.gson JsonElement isJsonArray

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

Introduction

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

Prototype

public boolean isJsonArray() 

Source Link

Document

provides check for verifying if this element is an array or not.

Usage

From source file:com.urswolfer.gerrit.client.rest.http.changes.CommitInfoParser.java

License:Apache License

public List<CommitInfo> parseCommitInfos(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(gson.fromJson(result, CommitInfo.class));
    }//from  ww w . j a v a 2 s  .  c om
    return gson.fromJson(result, TYPE);
}

From source file:com.urswolfer.gerrit.client.rest.http.changes.EditInfoParser.java

License:Apache License

public List<EditInfo> parseEditInfos(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(gson.fromJson(result, EditInfo.class));
    }//from w  ww  .j a v a2  s  . com
    return gson.fromJson(result, TYPE);
}

From source file:com.urswolfer.gerrit.client.rest.http.changes.ReviewerInfoParser.java

License:Apache License

public List<ReviewerInfo> parseReviewerInfos(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(gson.fromJson(result, ReviewerInfo.class));
    }// www . ja  v  a  2 s  . c  om
    return gson.fromJson(result, TYPE);
}

From source file:com.urswolfer.gerrit.client.rest.http.changes.SuggestedReviewerInfoParser.java

License:Apache License

public List<SuggestedReviewerInfo> parseSuggestReviewerInfos(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(gson.fromJson(result, SuggestedReviewerInfo.class));
    }//from  w  w w  .  j ava  2  s .  c o  m
    return gson.fromJson(result, TYPE);
}

From source file:com.urswolfer.gerrit.client.rest.http.groups.GroupsParser.java

License:Apache License

public List<GroupInfo> parseGroupInfos(JsonElement result) {
    if (result.isJsonArray()) {
        return gson.fromJson(result, GROUP_LIST_TYPE);
    } else {//from   w ww .j  a  va 2  s .  com
        SortedMap<String, GroupInfo> map = gson.fromJson(result, GROUP_MAP_TYPE);
        return new ArrayList<GroupInfo>(map.values());
    }
}

From source file:com.urswolfer.gerrit.client.rest.http.groups.GroupsParser.java

License:Apache License

public List<AccountInfo> parseGroupMembers(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(parseGroupMember(result));
    } else {/*  w  w w .j  a  v  a  2 s .co m*/
        return gson.fromJson(result, ACCOUNT_LIST_TYPE);
    }
}

From source file:com.urswolfer.gerrit.client.rest.http.projects.BranchInfoParser.java

License:Apache License

public List<BranchInfo> parseBranchInfos(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(gson.fromJson(result, BranchInfo.class));
    }/*from w w  w .ja  va 2  s  .c o  m*/
    return gson.fromJson(result, TYPE);
}

From source file:com.urswolfer.gerrit.client.rest.http.projects.TagInfoParser.java

License:Apache License

public List<TagInfo> parseTagInfos(JsonElement result) {
    if (!result.isJsonArray()) {
        return Collections.singletonList(gson.fromJson(result, TagInfo.class));
    }// www . j  a v a 2  s .  c  om
    return gson.fromJson(result, TYPE);
}

From source file:com.urswolfer.intellij.plugin.gerrit.rest.GerritUtil.java

License:Apache License

@NotNull
private static List<ChangeInfo> parseChangeInfos(@NotNull JsonElement result) {
    if (!result.isJsonArray()) {
        LOG.assertTrue(result.isJsonObject(), String.format("Unexpected JSON result format: %s", result));
        return Collections.singletonList(parseSingleChangeInfos(result.getAsJsonObject()));
    }/*from   www.  j  av a2s.co  m*/

    List<ChangeInfo> changeInfoList = new ArrayList<ChangeInfo>();
    for (JsonElement element : result.getAsJsonArray()) {
        LOG.assertTrue(element.isJsonObject(), String
                .format("This element should be a JsonObject: %s%nTotal JSON response: %n%s", element, result));
        changeInfoList.add(parseSingleChangeInfos(element.getAsJsonObject()));
    }
    return changeInfoList;
}

From source file:com.vmware.xenon.common.serialization.ObjectCollectionTypeConverter.java

License:Open Source License

@Override
public Collection<Object> deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    if (!json.isJsonArray()) {
        throw new JsonParseException("Expecting a json array object but found: " + json);
    }//from   w  ww  .j a va  2  s.c om

    Collection<Object> result;
    if (TYPE_SET.equals(type)) {
        result = new HashSet<>();
    } else if (TYPE_LIST.equals(type) || TYPE_COLLECTION.equals(type)) {
        result = new LinkedList<>();
    } else {
        throw new JsonParseException("Unexpected target type: " + type);
    }

    JsonArray jsonArray = json.getAsJsonArray();
    for (JsonElement entry : jsonArray) {
        if (entry.isJsonNull()) {
            result.add(null);
        } else if (entry.isJsonPrimitive()) {
            JsonPrimitive elem = entry.getAsJsonPrimitive();
            Object value = null;
            if (elem.isBoolean()) {
                value = elem.getAsBoolean();
            } else if (elem.isString()) {
                value = elem.getAsString();
            } else if (elem.isNumber()) {
                // We don't know if this is an integer, long, float or double...
                BigDecimal num = elem.getAsBigDecimal();
                try {
                    value = num.longValueExact();
                } catch (ArithmeticException e) {
                    value = num.doubleValue();
                }
            } else {
                throw new RuntimeException("Unexpected value type for json element:" + elem);
            }
            result.add(value);
        } else {
            // keep JsonElement to prevent stringified json issues
            result.add(entry);
        }
    }
    return result;
}