Example usage for org.json JSONObject getJSONObject

List of usage examples for org.json JSONObject getJSONObject

Introduction

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

Prototype

public JSONObject getJSONObject(String key) throws JSONException 

Source Link

Document

Get the JSONObject value associated with a key.

Usage

From source file:com.imaginary.home.cloud.device.Light.java

static void mapLight(@Nonnull ControllerRelay relay, @Nonnull JSONObject json,
        @Nonnull Map<String, Object> state) throws JSONException {
    mapPoweredDevice(relay, json, state);
    state.put("deviceType", "light");
    if (json.has("color")) {
        JSONObject color = json.getJSONObject("color");
        ColorMode colorMode = null;/*from w  w w.j av a  2  s.c o  m*/
        float[] components = null;

        if (color.has("colorMode") && !color.isNull("colorMode")) {
            try {
                colorMode = ColorMode.valueOf(color.getString("colorMode"));
            } catch (IllegalArgumentException e) {
                throw new JSONException("Invalid color mode: " + color.getString("colorMode"));
            }
        }
        if (color.has("components") && !color.isNull("components")) {
            JSONArray arr = color.getJSONArray("components");
            components = new float[arr.length()];

            for (int i = 0; i < arr.length(); i++) {
                components[i] = (float) arr.getDouble(i);
            }
        }
        if (colorMode != null || components != null) {
            state.put("colorMode", colorMode);
            state.put("colorValues", components);
        }
    }
    if (json.has("brightness") && !json.isNull("brightness")) {
        state.put("brightness", (float) json.getDouble("brightness"));
    }
    if (json.has("supportsColorChanges")) {
        state.put("colorChangeSupported",
                !json.isNull("supportsColorChanges") && json.getBoolean("supportsColorChanges"));
    }
    if (json.has("supportsBrightnessChanges")) {
        state.put("dimmable",
                !json.isNull("supportsBrightnessChanges") && json.getBoolean("supportsBrightnessChanges"));
    }
    if (json.has("colorModes")) {
        JSONArray arr = json.getJSONArray("colorModes");
        ColorMode[] modes = new ColorMode[arr.length()];

        for (int i = 0; i < arr.length(); i++) {
            try {
                modes[i] = ColorMode.valueOf(arr.getString(i));
            } catch (IllegalArgumentException e) {
                throw new JSONException("Invalid color mode: " + arr.getString(i));
            }
        }
        state.put("colorModesSupported", modes);
    }
}

From source file:com.google.wave.api.impl.EventDataSerializer.java

@Override
public Object unmarshall(SerializerState state, Class clazz, Object json) throws UnmarshallException {
    if (!EventData.class.isAssignableFrom(clazz)) {
        throw new UnmarshallException(clazz.getName() + " is not assignable from EventType");
    }/* w  w  w .j a v a 2s  .c o  m*/

    JSONObject jsonObject = (JSONObject) json;
    EventData event = null;
    try {
        String modifiedBy = (String) ser.unmarshall(state, String.class, jsonObject.get("modifiedBy"));
        Long timestamp = (Long) ser.unmarshall(state, Long.class, jsonObject.get("timestamp"));
        EventType eventType = EventType.valueOf(jsonObject.getString("type"));
        Map<String, Object> properties = (Map<String, Object>) ser.unmarshall(state, Map.class,
                jsonObject.getJSONObject("properties"));
        event = new EventData(eventType, modifiedBy, timestamp);
        event.setProperties(properties);
    } catch (JSONException jsonx) {
        throw new UnmarshallException("Event could not be unmarshalled.");
    }
    return event;
}

From source file:cn.code.notes.gtask.data.TaskList.java

public void setContentByLocalJSON(JSONObject js) {
    if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) {
        Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
    }/*from  w  w  w  . jav a  2s .  c  o  m*/

    try {
        JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);

        if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
            String name = folder.getString(NoteColumns.SNIPPET);
            setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name);
        } else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
            if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER)
                setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT);
            else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER)
                setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE);
            else
                Log.e(TAG, "invalid system folder");
        } else {
            Log.e(TAG, "error type");
        }
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    }
}

From source file:com.norman0406.slimgress.API.Plext.PlextBase.java

public static PlextBase createByJSON(JSONArray json) throws JSONException {
    if (json.length() != 3) {
        Log.e("PlextBase", "invalid array size");
        return null;
    }/*from   w  w w  . ja  v a 2 s. c  om*/

    JSONObject item = json.getJSONObject(2);
    JSONObject plext = item.getJSONObject("plext");

    PlextBase newPlext = null;

    String plextType = plext.getString("plextType");
    if (plextType.equals("PLAYER_GENERATED"))
        newPlext = new PlextPlayer(json);
    else if (plextType.equals("SYSTEM_BROADCAST"))
        newPlext = new PlextSystem(json);
    else
        Log.w("PlextBase", "unknown plext type: " + plextType);

    return newPlext;
}

From source file:com.norman0406.slimgress.API.Plext.PlextBase.java

protected PlextBase(PlextType type, JSONArray json) throws JSONException {
    super(json);/*from w ww . j a v  a  2  s .co  m*/
    mPlextType = type;

    JSONObject item = json.getJSONObject(2);

    JSONObject plext = item.getJSONObject("plext");
    JSONArray markup = plext.getJSONArray("markup");

    mText = plext.getString("text");

    mMarkups = new LinkedList<Markup>();
    for (int i = 0; i < markup.length(); i++) {
        JSONArray markupItem = markup.getJSONArray(i);

        Markup newMarkup = Markup.createByJSON(markupItem);
        if (newMarkup != null)
            mMarkups.add(newMarkup);
    }
}

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

@Test
public void testMerge() throws Exception {
    // clone a repo
    URI workspaceLocation = createWorkspace(getMethodName());
    String workspaceId = workspaceIdFromLocation(workspaceLocation);
    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
    IPath clonePath = getClonePath(workspaceId, project);
    JSONObject clone = clone(clonePath);
    String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
    String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION);
    String branchesLocation = clone.getString(GitConstants.KEY_BRANCH);

    // get project metadata
    WebRequest request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project = new JSONObject(response.getText());

    // create branch 'a'
    branch(branchesLocation, "a");

    // checkout 'a'
    Repository db1 = getRepositoryForContentLocation(cloneContentLocation);
    Git git = new Git(db1);
    assertBranchExist(git, "a");
    checkoutBranch(cloneLocation, "a");

    // modify while on 'a'
    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "change in a");

    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
    String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
    String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

    // "git add ."
    request = GitAddTest.getPutGitIndexRequest(gitIndexUri);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // commit all
    request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit on a", false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // assert clean
    assertStatus(StatusResult.CLEAN, gitStatusUri);

    // checkout 'master'
    checkoutBranch(cloneLocation, Constants.MASTER);

    // modify a different file on master
    JSONObject folder1 = getChild(project, "folder");
    JSONObject folderTxt = getChild(folder1, "folder.txt");
    modifyFile(folderTxt, "change in master");

    gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
    gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
    gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

    // "git add ."
    request = GitAddTest.getPutGitIndexRequest(gitIndexUri);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // commit all
    request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit on master", false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // assert clean
    assertStatus(StatusResult.CLEAN, gitStatusUri);

    // merge: "git merge a"
    JSONObject merge = merge(gitHeadUri, "a");
    MergeStatus mergeResult = MergeStatus.valueOf(merge.getString(GitConstants.KEY_RESULT));
    assertEquals(MergeStatus.MERGED, mergeResult);

    // assert clean
    assertStatus(StatusResult.CLEAN, gitStatusUri);

    request = getGetRequest(testTxt.getString(ProtocolConstants.KEY_LOCATION));
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    assertEquals("change in a", response.getText());

    request = getGetRequest(folderTxt.getString(ProtocolConstants.KEY_LOCATION));
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    assertEquals("change in master", response.getText());

    // TODO: check commits, bug 340051
}

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

@Test
public void testMergeAlreadyUpToDate() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    String projectName = getMethodName();
    JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString());

    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "change in master");

    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
    String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
    String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

    // "git add ."
    WebRequest request = GitAddTest.getPutGitIndexRequest(gitIndexUri);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    assertStatus(new StatusResult().setChanged(1), gitStatusUri);

    // "git merge master"
    JSONObject merge = merge(gitHeadUri, Constants.MASTER);
    MergeStatus mergeResult = MergeResult.MergeStatus.valueOf(merge.getString(GitConstants.KEY_RESULT));
    assertEquals(MergeResult.MergeStatus.ALREADY_UP_TO_DATE, mergeResult);

    // status hasn't changed
    assertStatus(new StatusResult().setChanged(1), gitStatusUri);
}

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

@Test
public void testMergeConflict() throws Exception {
    // clone a repo
    URI workspaceLocation = createWorkspace(getMethodName());
    String workspaceId = workspaceIdFromLocation(workspaceLocation);
    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
    IPath clonePath = getClonePath(workspaceId, project);
    JSONObject clone = clone(clonePath);
    String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
    String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION);
    String branchesLocation = clone.getString(GitConstants.KEY_BRANCH);

    // get project metadata
    WebRequest request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project = new JSONObject(response.getText());
    JSONObject gitSection = project.optJSONObject(GitConstants.KEY_GIT);
    assertNotNull(gitSection);//from   w w w.  ja v  a 2s.co m
    String gitRemoteUri = gitSection.optString(GitConstants.KEY_REMOTE, null);
    assertNotNull(gitRemoteUri);

    // create branch 'a'
    branch(branchesLocation, "a");

    // checkout 'a'
    Repository db1 = getRepositoryForContentLocation(cloneContentLocation);
    Git git = new Git(db1);
    assertBranchExist(git, "a");
    checkoutBranch(cloneLocation, "a");

    // modify while on 'a'
    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "change in a");

    gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
    String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
    String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

    // "git add ."
    request = GitAddTest.getPutGitIndexRequest(gitIndexUri);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // commit all
    request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit on a", false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // assert clean
    assertStatus(StatusResult.CLEAN, gitStatusUri);

    // checkout 'master'
    checkoutBranch(cloneLocation, Constants.MASTER);

    // modify the same file on master
    modifyFile(testTxt, "change in master");

    gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
    gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
    gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

    // "git add ."
    request = GitAddTest.getPutGitIndexRequest(gitIndexUri);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // commit all
    request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit on master", false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // assert clean
    assertStatus(StatusResult.CLEAN, gitStatusUri);

    // merge: "git merge a"
    JSONObject merge = merge(gitHeadUri, "a");
    MergeStatus mergeResult = MergeStatus.valueOf(merge.getString(GitConstants.KEY_RESULT));
    assertEquals(MergeStatus.CONFLICTING, mergeResult);

    // check status
    assertStatus(new StatusResult().setConflictingNames("test.txt"), gitStatusUri);

    request = getGetRequest(testTxt.getString(ProtocolConstants.KEY_LOCATION));
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    String[] responseLines = response.getText().split("\n");
    assertEquals(5, responseLines.length);
    assertEquals("<<<<<<< HEAD", responseLines[0]);
    assertEquals("change in master", responseLines[1]);
    assertEquals("=======", responseLines[2]);
    assertEquals("change in a", responseLines[3]);
    // ignore the last line since it's different each time
    // assertEquals(">>>>>>> c5ddb0e22e7e829683bb3b336ca6cb24a1b5bb2e", responseLines[4]);

    // TODO: check commits, bug 340051
}

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

@Test
public void testMergeIntoLocalFailedDirtyWorkTree() throws Exception {
    // clone a repo
    URI workspaceLocation = createWorkspace(getMethodName());
    String workspaceId = workspaceIdFromLocation(workspaceLocation);
    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
    IPath clonePath = getClonePath(workspaceId, project);
    clone(clonePath);/*from  w w w  .  j a va 2  s. c o m*/

    // get project metadata
    WebRequest request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project = new JSONObject(response.getText());
    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
    String gitRemoteUri = gitSection.getString(GitConstants.KEY_REMOTE);

    // add a parallel commit in secondary clone and push it to the remote
    JSONObject project2 = createProjectOrLink(workspaceLocation, getMethodName() + "2", null);
    IPath clonePath2 = getClonePath(workspaceId, project2);
    clone(clonePath2);

    // get project2 metadata
    request = getGetRequest(project2.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project2 = new JSONObject(response.getText());
    JSONObject gitSection2 = project2.getJSONObject(GitConstants.KEY_GIT);
    String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);

    JSONObject testTxt = getChild(project2, "test.txt");
    modifyFile(testTxt, "change in secondary");
    addFile(testTxt);
    commitFile(testTxt, "commit on branch", false);

    ServerStatus pushStatus = push(gitRemoteUri2, 1, 0, Constants.MASTER, Constants.HEAD, false);
    assertEquals(true, pushStatus.isOK());

    // modify on master and try to merge
    testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "dirty");

    JSONObject masterDetails = getRemoteBranch(gitRemoteUri, 1, 0, Constants.MASTER);
    String masterLocation = masterDetails.getString(ProtocolConstants.KEY_LOCATION);
    fetch(masterLocation);
    JSONObject merge = merge(gitHeadUri, Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER);

    MergeStatus mergeResult = MergeStatus.valueOf(merge.getString(GitConstants.KEY_RESULT));
    assertEquals(MergeStatus.FAILED, mergeResult);
    JSONObject failingPaths = merge.getJSONObject(GitConstants.KEY_FAILING_PATHS);
    assertEquals(1, failingPaths.length());
    assertEquals(MergeFailureReason.DIRTY_WORKTREE,
            MergeFailureReason.valueOf(failingPaths.getString("test.txt")));
}

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

@Test
public void testMergeFailedDirtyWorkTree() throws Exception {
    // clone a repo
    URI workspaceLocation = createWorkspace(getMethodName());
    String workspaceId = workspaceIdFromLocation(workspaceLocation);
    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
    IPath clonePath = getClonePath(workspaceId, project);
    JSONObject clone = clone(clonePath);
    String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION);
    String branchesLocation = clone.getString(GitConstants.KEY_BRANCH);

    // get project metadata
    WebRequest request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project = new JSONObject(response.getText());
    JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);

    final String branch = "branch";

    branch(branchesLocation, branch);//from   w  w w  . ja v a2s  . co m
    checkoutBranch(cloneLocation, branch);

    // create commit on branch
    JSONObject testTxt = getChild(project, "test.txt");
    modifyFile(testTxt, "change in a");
    addFile(testTxt);
    commitFile(testTxt, "commit on branch", false);

    // assert clean
    gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
    assertStatus(StatusResult.CLEAN, gitStatusUri);

    // checkout 'master'
    checkoutBranch(cloneLocation, Constants.MASTER);

    // modify the same file on master
    modifyFile(testTxt, "change in master");

    gitSection = project.getJSONObject(GitConstants.KEY_GIT);
    gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS);
    String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);

    addFile(testTxt);
    commitFile(testTxt, "commit on master", false);

    // modify again
    modifyFile(testTxt, "change in the working dir");

    // assert clean
    assertStatus(new StatusResult().setModified(1), gitStatusUri);

    // merge: "git merge branch"
    JSONObject merge = merge(gitHeadUri, branch);
    MergeStatus mergeResult = MergeStatus.valueOf(merge.getString(GitConstants.KEY_RESULT));
    assertEquals(MergeStatus.FAILED, mergeResult);
    JSONObject failingPaths = merge.getJSONObject(GitConstants.KEY_FAILING_PATHS);
    assertEquals(1, failingPaths.length());
    assertEquals(MergeFailureReason.DIRTY_WORKTREE,
            MergeFailureReason.valueOf(failingPaths.getString("test.txt")));
}