Example usage for org.json.simple JSONArray indexOf

List of usage examples for org.json.simple JSONArray indexOf

Introduction

In this page you can find the example usage for org.json.simple JSONArray indexOf.

Prototype

public int indexOf(Object o) 

Source Link

Document

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Usage

From source file:com.valygard.aohruthless.utils.config.JsonConfiguration.java

/**
 * Writes a JSONArray to memory at a specified String key.
 * /*from  w  w w  .  j av a2  s.  c om*/
 * @param key
 *            the String pathway
 * @param array
 *            the JSONArray
 * @return the JSONConfiguration instance
 */
@SuppressWarnings("unchecked")
public JsonConfiguration writeArray(String key, JSONArray array) {
    for (Object value : array) {
        if (value == null)
            continue;
        array.set(array.indexOf(value), JSONValue.toJSONString(value));
    }
    return writeObject(key, array);
}

From source file:edu.vt.vbi.patric.portlets.IDMapping.java

@SuppressWarnings("unchecked")
private void responseWriteFilters(ResourceResponse response) throws IOException {

    final String idGroupPATRIC = "PATRIC";
    final String idGroupOther = "Other";

    JSONObject grpPATRIC = new JSONObject();
    JSONObject grpPATRIC1 = new JSONObject();
    JSONObject grpPATRIC2 = new JSONObject();
    JSONObject grpPATRIC3 = new JSONObject();
    JSONObject grpPATRIC4 = new JSONObject();

    JSONObject grpRefSeq = new JSONObject();
    JSONObject grpRefSeq1 = new JSONObject();
    JSONObject grpRefSeq2 = new JSONObject();
    JSONObject grpRefSeq3 = new JSONObject();
    JSONObject grpRefSeq4 = new JSONObject();

    JSONObject grpOther = new JSONObject();

    // PATRIC Identifiers
    grpPATRIC.put("id", "<h5>PATRIC Identifier</h5>");
    grpPATRIC.put("value", "");

    grpPATRIC1.put("id", "PATRIC ID");
    grpPATRIC1.put("value", "patric_id");
    grpPATRIC1.put("group", idGroupPATRIC);

    grpPATRIC2.put("id", "Feature ID");
    grpPATRIC2.put("value", "feature_id");
    grpPATRIC2.put("group", idGroupPATRIC);

    grpPATRIC3.put("id", "Alt Locus Tag");
    grpPATRIC3.put("value", "alt_locus_tag");
    grpPATRIC3.put("group", idGroupPATRIC);

    grpPATRIC4.put("id", "P2 Feature ID");
    grpPATRIC4.put("value", "p2_feature_id");
    grpPATRIC4.put("group", idGroupPATRIC);

    // RefSeq Identifiers
    grpRefSeq.put("id", "<h5>RefSeq Identifiers</h5>");
    grpRefSeq.put("value", "");

    grpRefSeq1.put("id", "RefSeq");
    grpRefSeq1.put("value", "protein_id");
    grpRefSeq1.put("group", idGroupPATRIC);

    grpRefSeq2.put("id", "RefSeq Locus Tag");
    grpRefSeq2.put("value", "refseq_locus_tag");
    grpRefSeq2.put("group", idGroupPATRIC);

    grpRefSeq3.put("id", "Gene ID");
    grpRefSeq3.put("value", "gene_id");
    grpRefSeq3.put("group", idGroupPATRIC);

    grpRefSeq4.put("id", "GI");
    grpRefSeq4.put("value", "gi");
    grpRefSeq4.put("group", idGroupPATRIC);

    // Other Identifiers
    grpOther.put("id", "<h5>Other Identifiers</h5>");
    grpOther.put("value", "");

    JSONArray jsonIdTypes = new JSONArray();
    jsonIdTypes.add(grpPATRIC);/*  www. j a v  a2s.  c  om*/
    jsonIdTypes.add(grpPATRIC1);
    jsonIdTypes.add(grpPATRIC2);
    jsonIdTypes.add(grpPATRIC3);
    jsonIdTypes.add(grpPATRIC4);

    jsonIdTypes.add(grpRefSeq);
    jsonIdTypes.add(grpRefSeq1);
    jsonIdTypes.add(grpRefSeq2);
    jsonIdTypes.add(grpRefSeq3);
    jsonIdTypes.add(grpRefSeq4);

    jsonIdTypes.add(grpOther);
    List<String> otherTypes = getIdTypes();
    for (String type : otherTypes) {
        JSONObject item = new JSONObject();
        item.put("id", type);
        item.put("value", type);
        item.put("group", idGroupOther);

        jsonIdTypes.add(item);
    }
    // add UniProtKB-Accession, for easier processing, treat UniProtKB-Accession as a PATRIC attribute
    JSONObject item = new JSONObject();
    item.put("id", "UniProtKB-ID");
    item.put("value", "UniProtKB-ID");
    item.put("group", idGroupOther);
    int idx = jsonIdTypes.indexOf(item);
    item.put("id", "UniProtKB-Accession");
    item.put("value", "uniprotkb_accession");
    item.put("group", idGroupPATRIC);
    jsonIdTypes.add(idx + 1, item);

    JSONObject json = new JSONObject();
    json.put("id_types", jsonIdTypes);

    response.setContentType("application/json");
    json.writeJSONString(response.getWriter());
}

From source file:org.mozilla.gecko.sync.repositories.android.AndroidBrowserBookmarksRepositorySession.java

/**
 * If the provided record doesn't have correct parent information,
 * update appropriate bookkeeping to improve the situation.
 *
 * @param bmk//from   w  w w.jav a2 s  .c  o  m
 */
private void handleParenting(BookmarkRecord bmk) {
    if (guidToID.containsKey(bmk.parentID)) {
        bmk.androidParentID = guidToID.get(bmk.parentID);

        // Might as well set a basic position from the downloaded children array.
        JSONArray children = parentToChildArray.get(bmk.parentID);
        if (children != null) {
            int index = children.indexOf(bmk.guid);
            if (index >= 0) {
                bmk.androidPosition = index;
            }
        }
    } else {
        bmk.androidParentID = guidToID.get("unfiled");
        ArrayList<String> children;
        if (missingParentToChildren.containsKey(bmk.parentID)) {
            children = missingParentToChildren.get(bmk.parentID);
        } else {
            children = new ArrayList<String>();
        }
        children.add(bmk.guid);
        needsReparenting++;
        missingParentToChildren.put(bmk.parentID, children);
    }
}

From source file:org.mozilla.gecko.sync.repositories.android.AndroidBrowserBookmarksRepositorySession.java

@Override
protected void updateBookkeeping(Record record)
        throws NoGuidForIdException, NullCursorException, ParentNotFoundException {
    super.updateBookkeeping(record);
    BookmarkRecord bmk = (BookmarkRecord) record;

    // If record is folder, update maps and re-parent children if necessary.
    if (!bmk.isFolder()) {
        Logger.debug(LOG_TAG, "Not a folder. No bookkeeping.");
        return;/*from   w  ww. jav a  2  s .  c  o  m*/
    }

    Logger.debug(LOG_TAG, "Updating bookkeeping for folder " + record.guid);

    // Mappings between ID and GUID.
    // TODO: update our persisted children arrays!
    // TODO: if our Android ID just changed, replace parents for all of our children.
    guidToID.put(bmk.guid, bmk.androidID);
    idToGuid.put(bmk.androidID, bmk.guid);

    JSONArray childArray = bmk.children;

    if (Logger.logVerbose(LOG_TAG)) {
        Logger.trace(LOG_TAG, bmk.guid + " has children " + childArray.toJSONString());
    }
    parentToChildArray.put(bmk.guid, childArray);

    // Re-parent.
    if (missingParentToChildren.containsKey(bmk.guid)) {
        for (String child : missingParentToChildren.get(bmk.guid)) {
            // This might return -1; that's OK, the bookmark will
            // be properly repositioned later.
            long position = childArray.indexOf(child);
            dataAccessor.updateParentAndPosition(child, bmk.androidID, position);
            needsReparenting--;
        }
        missingParentToChildren.remove(bmk.guid);
    }
}