Example usage for org.json JSONObject has

List of usage examples for org.json JSONObject has

Introduction

In this page you can find the example usage for org.json JSONObject has.

Prototype

public boolean has(String key) 

Source Link

Document

Determine if the JSONObject contains a specific key.

Usage

From source file:com.nextgis.maplib.map.RemoteTMSLayer.java

@Override
public void fromJSON(JSONObject jsonObject) throws JSONException {
    super.fromJSON(jsonObject);
    mURL = jsonObject.getString(JSON_URL_KEY);
    if (jsonObject.has(JSON_LOGIN_KEY))
        mLogin = jsonObject.getString(JSON_LOGIN_KEY);
    if (jsonObject.has(JSON_PASSWORD_KEY))
        mPassword = jsonObject.getString(JSON_PASSWORD_KEY);

    analizeURL(mURL);//from  ww w.j av  a2s.c o m
}

From source file:info.rmapproject.webapp.auth.GoogleOAuthProvider.java

public OAuthProviderAccount loadOAuthProviderAccount(Token accessToken, OAuthProviderName provider) {

    OAuthService service = this.getService();

    // getting user profile
    OAuthRequest oauthRequest = new OAuthRequest(Verb.GET, config.getProfileUrl(), service);

    service.signRequest(accessToken, oauthRequest);
    Response oauthResponse = oauthRequest.send();
    String jsonString = oauthResponse.getBody();
    JSONObject root = new JSONObject(jsonString);
    JSONArray emailArray = root.getJSONArray(GOOGLE_JSON_EMAILLIST_PROPERTY);
    JSONObject firstEmail = emailArray.getJSONObject(0);

    String accountId = root.getString(GOOGLE_JSON_ACCOUNTID_PROPERTY);
    String displayName = root.getString(GOOGLE_JSON_DISPLAYNAME_PROPERTY);
    String publicId = firstEmail.getString(GOOGLE_JSON_EMAIL_PROPERTY);
    String profilePath = "";
    if (root.has(GOOGLE_JSON_PROFILEPATH_PROPERTY)) {
        profilePath = root.getString(GOOGLE_JSON_PROFILEPATH_PROPERTY);
    }// ww  w.j  a  va2 s  .c om

    OAuthProviderAccount profile = new OAuthProviderAccount(accessToken, provider, displayName, accountId,
            publicId, profilePath);

    //logger.info("Google profile=" + jsonString);
    //logger.info("Google token=" + accessToken.getRawResponse());

    return profile;
}

From source file:com.tassadar.multirommgr.installfragment.InstallCard.java

private int getDefaultKernel() {
    int res = 0;//from   w  w w. j  a  v a 2 s . co m
    Iterator<Map.Entry<String, Manifest.InstallationFile>> itr = m_manifest.getKernels().entrySet().iterator();
    for (int i = 0; itr.hasNext(); ++i) {
        JSONObject extra = itr.next().getValue().extra;
        if (extra == null)
            continue;
        try {
            if (extra.has("display") && Build.DISPLAY.indexOf(extra.getString("display")) == -1)
                continue;

            if (extra.has("releases")) {
                JSONArray r = extra.getJSONArray("releases");
                boolean found = false;
                for (int x = 0; x < r.length(); ++x) {
                    if (r.getString(x).equals(Build.VERSION.RELEASE)) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    continue;
            }
            res = i;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return res;
}

From source file:com.github.cambierr.lorawanpacket.semtech.PullResp.java

public PullResp(byte[] _randoms, ByteBuffer _raw) throws MalformedPacketException {
    super(_randoms, PacketType.PULL_RESP);
    _raw.order(ByteOrder.LITTLE_ENDIAN);

    if (_raw.remaining() < 1) {
        throw new MalformedPacketException("too short");
    }// w w w  .j  a v a2 s.  co m

    byte[] json = new byte[_raw.remaining()];
    _raw.get(json);
    JSONObject jo;

    try {
        jo = new JSONObject(new String(json));
    } catch (JSONException ex) {
        throw new MalformedPacketException("malformed json");
    }

    txpks = new ArrayList<>();

    if (!jo.has("txpk")) {
        throw new MalformedPacketException("missing json (txpk)");
    }

    if (!jo.get("txpk").getClass().equals(JSONArray.class)) {
        throw new MalformedPacketException("malformed json (txpk)");
    }
    JSONArray rxpk = jo.getJSONArray("txpk");

    for (int i = 0; i < rxpk.length(); i++) {
        txpks.add(new Txpk(rxpk.getJSONObject(i)));
    }
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitStashTest.java

@Test
public void testEmptyStashList() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    String stashLocation = getStashLocation(project);
    ServerStatus status = getStashList(stashLocation);
    assertTrue(status.isOK());//from  w  ww .  ja  va 2 s.co  m

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(0, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitStashTest.java

@Test
public void testStashListPagination() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());
    String stashLocation = getStashLocation(project);

    int CHANGES = 12;
    int pageSize = 5;
    int k = CHANGES;

    while (k-- > 0) {
        JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
        modifyFile(testTxt, "change to stash " + String.valueOf(k)); //$NON-NLS-1$

        ServerStatus status = createStash(stashLocation);
        assertTrue(status.isOK());//from  w w  w.jav  a 2s.  co  m
    }

    ServerStatus status = getStashList(stashLocation, 1, pageSize);
    assertTrue(status.isOK());

    /* first page */
    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(pageSize, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
    assertTrue(stash.has(ProtocolConstants.KEY_NEXT_LOCATION));
    assertFalse(stash.has(ProtocolConstants.KEY_PREVIOUS_LOCATION));

    status = getStashList(stash.getString(ProtocolConstants.KEY_NEXT_LOCATION));
    assertTrue(status.isOK());

    /* second page */
    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(pageSize, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
    assertTrue(stash.has(ProtocolConstants.KEY_NEXT_LOCATION));
    assertTrue(stash.has(ProtocolConstants.KEY_PREVIOUS_LOCATION));

    status = getStashList(stash.getString(ProtocolConstants.KEY_NEXT_LOCATION));
    assertTrue(status.isOK());

    /* third page */
    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals((CHANGES - 2 * pageSize), stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
    assertFalse(stash.has(ProtocolConstants.KEY_NEXT_LOCATION));
    assertTrue(stash.has(ProtocolConstants.KEY_PREVIOUS_LOCATION));
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitStashTest.java

@Test
public void testStashCreateWithUntracked() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
    modifyFile(testTxt, "change to stash"); //$NON-NLS-1$

    String stashLocation = getStashLocation(project);
    ServerStatus status = getStashList(stashLocation);
    assertTrue(status.isOK());//from w  w  w  .  j  a  v  a 2s  . c o  m

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(0, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    status = createStash(stashLocation);
    assertTrue(status.isOK());

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitStashTest.java

@Test
public void testStashCreateWithUntrackedAndIndex() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
    modifyFile(testTxt, "change to stash"); //$NON-NLS-1$

    /* stage folder.txt */
    JSONObject folder1 = getChild(project, "folder"); //$NON-NLS-1$
    JSONObject folderTxt = getChild(folder1, "folder.txt"); //$NON-NLS-1$
    modifyFile(folderTxt, "change to stash"); //$NON-NLS-1$
    addFile(folderTxt);/*ww w.j  av a  2s  .c o m*/

    String stashLocation = getStashLocation(project);
    ServerStatus status = getStashList(stashLocation);
    assertTrue(status.isOK());

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(0, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    status = createStash(stashLocation);
    assertTrue(status.isOK());

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    JSONObject stashChange = stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).getJSONObject(0);
    assertTrue(stashChange.has(GitConstants.KEY_COMMIT_DIFFS));
    assertEquals(2, stashChange.getJSONArray(GitConstants.KEY_COMMIT_DIFFS).length());
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitStashTest.java

@Test
public void testStashApply() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
    modifyFile(testTxt, "change to stash"); //$NON-NLS-1$
    String beforeStash = getFileContent(testTxt);

    String stashLocation = getStashLocation(project);
    ServerStatus status = createStash(stashLocation);
    assertTrue(status.isOK());//from   www  .  j a v  a  2  s  . c  o m

    String afterStash = getFileContent(testTxt);
    assertFalse(beforeStash.equals(afterStash));

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    JSONObject change = stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).getJSONObject(0);
    assertTrue(change.has(GitConstants.KEY_STASH_APPLY_LOCATION));

    status = applyStash(change.getString(GitConstants.KEY_STASH_APPLY_LOCATION));
    assertTrue(status.isOK());

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    String afterApply = getFileContent(testTxt);
    assertTrue(beforeStash.equals(afterApply));
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitStashTest.java

@Test
public void testStashDrop() throws Exception {
    String projectName = getMethodName();
    createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt"); //$NON-NLS-1$
    modifyFile(testTxt, "change to stash"); //$NON-NLS-1$

    String stashLocation = getStashLocation(project);
    ServerStatus status = createStash(stashLocation);
    assertTrue(status.isOK());//from   w  w  w  .ja  v  a  2s.  c o m

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    JSONObject stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(1, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());

    JSONObject change = stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).getJSONObject(0);
    assertTrue(change.has(GitConstants.KEY_STASH_DROP_LOCATION));

    status = dropStash(change.getString(GitConstants.KEY_STASH_DROP_LOCATION));
    assertTrue(status.isOK());

    status = getStashList(stashLocation);
    assertTrue(status.isOK());

    stash = status.getJsonData();
    assertTrue(stash.has(ProtocolConstants.KEY_CHILDREN));
    assertEquals(0, stash.getJSONArray(ProtocolConstants.KEY_CHILDREN).length());
}