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.ccc.crest.core.cache.crest.tournament.Tournaments.java

License:Open Source License

@Override
public Tournaments deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (TotalCountStringKey.equals(key))
            totalCountStr = value.getAsString();
        else if (ItemsKey.equals(key)) {
            JsonElement objectElement = objectEntry.getValue();
            if (!objectElement.isJsonArray())
                throw new JsonParseException(
                        "Expected " + ItemsKey + " array received json element " + objectElement.toString());
            int size = ((JsonArray) objectElement).size();
            for (int i = 0; i < size; i++) {
                JsonElement childElement = ((JsonArray) objectElement).get(i);
                Tournament child = new Tournament();
                tournaments.add(child);//from   w  w  w .  j  a  va2 s .com
                child.deserialize(childElement, typeOfT, context);
            }
        } else if (PageCountKey.equals(key))
            pageCount = value.getAsLong();
        else if (PageCountStringKey.equals(key))
            pageCountStr = value.getAsString();
        else if (TotalCountKey.equals(key))
            totalCount = value.getAsLong();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.cinchapi.concourse.importer.LineBasedImporter.java

License:Apache License

/**
 * Import the data contained in {@code file} into {@link Concourse}.
 * <p>//from w  ww.j a va 2s  . c o  m
 * <strong>Note</strong> that if {@code resolveKey} is specified, an attempt
 * will be made to add the data in from each group into the existing records
 * that are found using {@code resolveKey} and its corresponding value in
 * the group.
 * </p>
 * 
 * @param file
 * @param resolveKey
 * @return a collection of {@link ImportResult} objects that describes the
 *         records created/affected from the import and whether any errors
 *         occurred.
 */
public final Set<Long> importFile(String file, @Nullable String resolveKey) {
    // TODO add option to specify batchSize, which is how many objects to
    // send over the wire in one atomic batch
    List<String> lines = FileOps.readLines(file);
    String[] keys = header();
    JsonArray array = new JsonArray();
    boolean checkedFileFormat = false;
    for (String line : lines) {
        if (!checkedFileFormat) {
            validateFileFormat(line);
            checkedFileFormat = true;
        }
        if (keys == null) {
            keys = parseKeys(line);
            log.info("Parsed keys from header: " + line);
        } else {
            JsonObject object = parseLine(line, keys);
            if (resolveKey != null && object.has(resolveKey)) {
                JsonElement resolveValue = object.get(resolveKey);
                if (!resolveValue.isJsonArray()) {
                    JsonArray temp = new JsonArray();
                    temp.add(resolveValue);
                    resolveValue = temp;
                }
                for (int i = 0; i < resolveValue.getAsJsonArray().size(); ++i) {
                    String value = resolveValue.getAsJsonArray().get(i).toString();
                    Object stored = Convert.stringToJava(value);
                    Set<Long> resolved = concourse.find(resolveKey, Operator.EQUALS, stored);
                    for (long record : resolved) {
                        object = parseLine(line, keys); // this is
                                                        // inefficient, but
                                                        // there is no good
                                                        // way to clone the
                                                        // original object
                        object.addProperty(Constants.JSON_RESERVED_IDENTIFIER_NAME, record);
                        array.add(object);
                    }
                }
            } else {
                array.add(object);
            }
            log.info("Importing {}", line);
        }

    }
    Set<Long> records = importString(array.toString());
    return records;
}

From source file:com.claresco.tinman.json.JsonUtility.java

License:Open Source License

protected static ArrayList<XapiActivity> convertJsonArrayOfActivities(JsonObject theObject,
        String theElementName, ArrayList<XapiActivity> theArrayList, JsonDeserializationContext theContext) {
    JsonElement theActivities;

    if (hasElement(theObject, theElementName)) {
        theActivities = get(theObject, theElementName);
        if (theActivities.isJsonArray()) {
            JsonArray theJsonArray = getAnArray(theObject, theElementName);
            for (JsonElement e : theJsonArray) {
                theArrayList.add((XapiActivity) delegateDeserialization(theContext, e, XapiActivity.class));
            }//from w w w. ja  va2s . c o  m
        } else {
            theArrayList
                    .add((XapiActivity) delegateDeserialization(theContext, theActivities, XapiActivity.class));
        }

    }
    return theArrayList;
}

From source file:com.claresco.tinman.json.XapiCredentialsListJson.java

License:Open Source License

@Override
public XapiCredentialsList deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    if (!arg0.isJsonArray()) {
        return null;
    }/*w  w w  .j a  v  a2s .co m*/

    JsonArray theArray = arg0.getAsJsonArray();

    HashMap<XapiKeySecret, XapiCredentials> theCredentialMap = new HashMap<XapiKeySecret, XapiCredentials>();

    for (JsonElement e : theArray) {
        JsonObject theObject = JsonUtility.convertJsonElementToJsonObject(e);
        JsonObject theKeySecretJson = JsonUtility
                .convertJsonElementToJsonObject(JsonUtility.get(theObject, KEYSECRET));

        XapiKeySecret theKS = new XapiKeySecret(JsonUtility.getElementAsString(theKeySecretJson, KEY),
                JsonUtility.getElementAsString(theKeySecretJson, SECRET));

        XapiCredentials theCred = JsonUtility.delegateDeserialization(arg2,
                JsonUtility.get(theObject, CREDENTIALS), XapiCredentials.class);

        theCredentialMap.put(theKS, theCred);
    }

    return new XapiCredentialsList(theCredentialMap);
}

From source file:com.claresco.tinman.json.XapiPersonJson.java

License:Open Source License

private ArrayList<String> fillTheList(String key, JsonObject theObject) {
    ArrayList<String> theList = new ArrayList<String>();

    if (JsonUtility.hasElement(theObject, key)) {
        JsonElement theElement = JsonUtility.get(theObject, key);
        if (theElement.isJsonArray()) {
            JsonArray theArray = JsonUtility.getAnArray(theObject, key);
            for (JsonElement e : theArray) {
                theList.add(e.getAsString());
            }/*from  www.  ja v a 2  s.  co  m*/
        }
    }

    return theList;
}

From source file:com.claresco.tinman.json.XapiStatementBatchJson.java

License:Open Source License

@Override
public XapiStatementBatch deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {

    XapiStatementBatch theBatch = new XapiStatementBatch();

    // if it is not an array of statement
    if (!arg0.isJsonArray()) {
        XapiStatement theStatement = JsonUtility.delegateDeserialization(arg2, arg0, XapiStatement.class);

        theBatch.addStatementToBatch(theStatement);
    } else {//from  ww  w.ja  v a2 s  .  com
        JsonArray statementArrayJson = arg0.getAsJsonArray();

        for (JsonElement element : statementArrayJson) {
            XapiStatement theStatement = JsonUtility.delegateDeserialization(arg2, element,
                    XapiStatement.class);
            theBatch.addStatementToBatch(theStatement);
        }
    }

    return theBatch;
}

From source file:com.cloud.bridge.util.JsonAccessor.java

License:Apache License

public int getMatchCount(String propPath) {
    JsonElement jsonElement = tryEval(propPath);
    if (jsonElement == null)
        return -1;

    if (jsonElement.isJsonNull())
        return 0;

    if (jsonElement.isJsonArray())
        return ((JsonArray) jsonElement).size();

    return 1;// w w  w  .j  a  va  2s. c om
}

From source file:com.cloudbase.CBNaturalDeserializer.java

License:Open Source License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    if (json.isJsonNull())
        return null;
    else if (json.isJsonPrimitive())
        return handlePrimitive(json.getAsJsonPrimitive());
    else if (json.isJsonArray())
        return handleArray(json.getAsJsonArray(), context);
    else/*from w  ww . j  ava  2 s . c o  m*/
        return handleObject(json.getAsJsonObject(), context);
}

From source file:com.codereligion.bugsnag.logback.resource.TabVOSerializer.java

License:Apache License

private void filterJsonElement(final JsonElement jsonElement) {
    if (jsonElement.isJsonArray()) {
        filterJsonArray(jsonElement.getAsJsonArray());
    } else if (jsonElement.isJsonObject()) {
        filterJsonObject(jsonElement.getAsJsonObject());
    }//ww  w.  j  av a  2 s  .c  o m
}

From source file:com.collective.celos.ci.testing.fixtures.compare.json.JsonElementConstructor.java

License:Apache License

private JsonElement deepCopy(String path, JsonElement el, Set<String> ignorePaths) {
    if (el.isJsonArray()) {
        return deepCopyJsonArray(path, el.getAsJsonArray(), ignorePaths);
    }/*from  w w w . j av a2s .c  o m*/
    if (el.isJsonObject()) {
        return deepCopyJsonObject(path, el.getAsJsonObject(), ignorePaths);
    }
    if (el.isJsonPrimitive() || el.isJsonNull()) {
        return el;
    }
    throw new IllegalStateException(
            "JSONElement should be either Array, Object, Primitive or Null. So you cannot get here");
}