List of usage examples for org.json JSONObject length
public int length()
From source file:com.nosoop.json.VDF.java
/** * Checks that a JSONObject converted from a VDF file is an array. If so, * the only keys in the JSONObject are a continues set of integers * represented by Strings starting from "0". Note that empty JSONObjects are * also treated as arrays./* w ww .j av a 2 s . c o m*/ * * @param object The JSONObject to check for a VDF-formatted array. * @return Whether or not the JSONObject is a VDF-formatted array. */ private static boolean containsVDFArray(JSONObject object) { int indices = object.length(); int[] index = new int[indices]; for (int i = 0; i < indices; i++) { index[i] = -1; } /** * Fail if we encounter a non-integer, if a value isn't a JSONObject, * or if the key is a number that is larger than the size of the array * (meaning we're missing a value). */ for (@SuppressWarnings("unchecked") Iterator<String> iter = object.keys(); iter.hasNext();) { String name = iter.next(); if (object.optJSONObject(name) == null) { return false; } try { int i = Integer.parseInt(name); if (i >= indices) { return false; } index[i] = i; } catch (NumberFormatException e) { return false; } } // Fail if we are missing any values (e.g., 0, 1, 2, 3, 4, 5, 7, 8, 9). for (int i = 0; i < indices; i++) { if (index[i] != i) { return false; } } return true; }
From source file:org.eclipse.orion.server.tests.servlets.git.GitBlameTest.java
@Test public void testBlameNoCommits() throws IOException, SAXException, JSONException, CoreException { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { //clone a repo JSONObject clone = clone(clonePath); String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); //get project/folder metadata WebRequest request = getGetRequest(cloneContentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject folder = new JSONObject(response.getText()); // get blameUri JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT); String gitBlameUri = gitSection.getString(GitConstants.KEY_BLAME); // blame request request = getGetGitBlameRequest(gitBlameUri); response = webConversation.getResource(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); // test//from w w w . j av a 2 s. com JSONObject blameObject = new JSONObject(response.getText()); assertEquals(blameObject.length(), 4); assertEquals(blameObject.get("Severity"), "Error"); assertEquals(blameObject.get("HttpCode"), 400); assertEquals(blameObject.get("Code"), 0); } }
From source file:org.eclipse.orion.server.tests.servlets.git.GitBlameTest.java
@Test public void testBlameMultiCommit() throws IOException, SAXException, JSONException, CoreException { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { //clone a repo JSONObject clone = clone(clonePath); String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); //get project/folder metadata WebRequest request = getGetRequest(cloneContentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject folder = new JSONObject(response.getText()); JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); //create file test.txtx JSONObject testTxt = getChild(folder, "test.txt"); modifyFile(testTxt, "line one \n line two \n line 3 \n line 4"); //commit the file addFile(testTxt);//from ww w . j a va 2 s .co m request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // get the blame uri for this file JSONObject testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT); String blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME); // blame the file request = getGetGitBlameRequest(blameUri); response = webConversation.getResource(request); // testing JSONObject blameObject = new JSONObject(response.getText()); // non blame info tests JSONArray blame = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN); assertNotNull(blameObject.get(ProtocolConstants.KEY_CHILDREN)); assertEquals(blame.length(), 1); blameObject = blame.getJSONObject(0); assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_EMAIL)); assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_NAME)); assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_IMAGE)); assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_EMAIL)); assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_NAME)); assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_MESSAGE)); assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_TIME)); assertNotNull(blameObject.get(GitConstants.KEY_COMMIT)); assertNotNull(blameObject.get(ProtocolConstants.KEY_NAME)); JSONArray children = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN); JSONObject child = children.getJSONObject(0); assertEquals(children.length(), 1); assertEquals(child.get(GitConstants.KEY_START_RANGE), 1); assertEquals(child.get(GitConstants.KEY_END_RANGE), 4); //save commit info to test String commitLocation1 = blameObject.getString(GitConstants.KEY_COMMIT); int commitTime1 = blameObject.getInt(GitConstants.KEY_COMMIT_TIME); String commitId1 = blameObject.getString(ProtocolConstants.KEY_NAME); // modify the file modifyFile(testTxt, "LINE ONE \n LINE TWO \n LINE THREE \n LINE FOUR \n LINE FIVE"); addFile(testTxt); //commit the new changes request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // get blame uri testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT); blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME); // blame file request = getGetGitBlameRequest(blameUri); response = webConversation.getResource(request); // test blameObject = new JSONObject(response.getText()); // non blame info tests assertEquals(blameObject.length(), 4); assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame"); assertEquals(blameObject.getString(ProtocolConstants.KEY_LOCATION), blameUri); blame = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN); assertNotNull(blameObject.get(ProtocolConstants.KEY_CHILDREN)); // test first commit assertEquals(blame.length(), 1); blameObject = blame.getJSONObject(0); //test second commit blameObject = blame.getJSONObject(0); assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_EMAIL)); assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_NAME)); assertNotNull(blameObject.get(GitConstants.KEY_AUTHOR_IMAGE)); assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_EMAIL)); assertNotNull(blameObject.get(GitConstants.KEY_COMMITTER_NAME)); assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_MESSAGE)); assertNotNull(blameObject.get(GitConstants.KEY_COMMIT_TIME)); assertNotNull(blameObject.get(GitConstants.KEY_COMMIT)); assertNotNull(blameObject.get(ProtocolConstants.KEY_NAME)); children = blameObject.getJSONArray(ProtocolConstants.KEY_CHILDREN); child = children.getJSONObject(0); assertEquals(children.length(), 1); assertEquals(child.get(GitConstants.KEY_START_RANGE), 1); assertEquals(child.get(GitConstants.KEY_END_RANGE), 5); String commitLocation2 = blameObject.getString(GitConstants.KEY_COMMIT); int commitTime2 = blameObject.getInt(GitConstants.KEY_COMMIT_TIME); String commitId2 = blameObject.getString(ProtocolConstants.KEY_NAME); // test that there are not duplicates of the same commit assertNotSame(commitId1, commitId2); assertNotSame(commitLocation1, commitLocation2); assertNotSame(commitTime1, commitTime2); } }
From source file:org.eclipse.orion.server.tests.servlets.git.GitBlameTest.java
@Test public void testFolderBlame() throws IOException, SAXException, JSONException, CoreException { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { //clone a repo JSONObject clone = clone(clonePath); String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); //get project/folder metadata WebRequest request = getGetRequest(cloneContentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject folder = new JSONObject(response.getText()); JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); JSONObject newfolder = getChild(folder, "folder"); //modifyFile(newfolder, "commit me"); // commit the new file addFile(newfolder);/*from www . j av a 2 s .c o m*/ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit on testFile2.txt", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject newfolderGitSection = newfolder.getJSONObject(GitConstants.KEY_GIT); String blameUri = newfolderGitSection.getString(GitConstants.KEY_BLAME); // blame request request = getGetGitBlameRequest(blameUri); response = webConversation.getResource(request); // get BlameInfo JSONObject blameObject = new JSONObject(response.getText()); //Test assertEquals(blameObject.length(), 4); assertEquals(blameObject.get("Severity"), "Error"); assertEquals(blameObject.get("HttpCode"), 400); assertEquals(blameObject.get("Code"), 0); } }
From source file:com.facebook.internal.JsonUtilTest.java
@Test public void testJsonObjectClear() throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put("hello", "world"); jsonObject.put("hocus", "pocus"); JsonUtil.jsonObjectClear(jsonObject); assertEquals(0, jsonObject.length()); }
From source file:com.facebook.internal.JsonUtilTest.java
@Test public void testJsonObjectPutAll() throws JSONException { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("hello", "world"); map.put("hocus", "pocus"); JSONObject jsonObject = new JSONObject(); JsonUtil.jsonObjectPutAll(jsonObject, map); assertEquals("pocus", jsonObject.get("hocus")); assertEquals(2, jsonObject.length()); }
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);/* w w w. ja v a 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 . j a v a2 s . c o 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"))); }
From source file:com.leanengine.JsonDecode.java
private static Map<String, Object> accountPropsFromJson(JSONObject jsonNode) throws LeanException, JSONException { Map<String, Object> props = new HashMap<String, Object>(jsonNode.length()); // must have some properties if (jsonNode.length() == 0) throw new LeanException(LeanError.Type.ServerError, "Malformed reply: JSON parameter 'providerProperties' must not be empty."); Iterator fieldNames = jsonNode.keys(); while (fieldNames.hasNext()) { String field = (String) fieldNames.next(); props.put(field, jsonNode.get(field)); }/* w w w . jav a 2s . c o m*/ return props; }
From source file:com.leanengine.JsonDecode.java
static Map<String, Object> entityPropertiesFromJson(JSONObject jsonNode) throws LeanException, JSONException { Map<String, Object> props = new HashMap<String, Object>(jsonNode.length()); // must have some properties if (jsonNode.length() == 0) throw new LeanException(LeanError.Type.ServerError, "Empty reply."); Iterator fieldNames = jsonNode.keys(); while (fieldNames.hasNext()) { String field = (String) fieldNames.next(); // skip LeanEngine system properties (starting with underscore '_') if (field.startsWith("_")) continue; Object subNode = jsonNode.get(field); props.put(field, propertyFromJson(subNode)); }//w w w . j a va 2s .c om return props; }