List of usage examples for org.json JSONArray put
public JSONArray put(Object value)
From source file:pt.webdetails.cfr.CfrApi.java
@GET @Path("/deletePermissions") @Produces(MimeTypes.JSON)//from w ww. jav a2 s .c o m public String deletePermissions(@QueryParam(MethodParams.PATH) String path, @QueryParam(MethodParams.ID) @DefaultValue("") List<String> ids, @QueryParam(MethodParams.RECURSIVE) @DefaultValue("false") Boolean recursive) throws JSONException, IOException { path = checkRelativePathSanity(path); String[] userOrGroupId = ids.toArray(new String[ids.size()]); JSONObject result = new JSONObject(); boolean admin = isUserAdmin(); boolean errorDeleting = false; if (path != null || (userOrGroupId != null && userOrGroupId.length > 0)) { List<String> files = new ArrayList<String>(); if (recursive) { files = getFileNameTree(path); } else { files.add(path); } JSONArray permissionDeleteResultArray = new JSONArray(); if (userOrGroupId == null || userOrGroupId.length == 0) { for (String f : files) { if (deletePermissions(f, null)) { permissionDeleteResultArray .put(new JSONObject().put("status", "Permissions for " + f + " deleted")); } else { if (admin) { permissionDeleteResultArray .put(new JSONObject().put("status", "Error deleting permissions for " + f)); } else { errorDeleting = true; } } } result.put("status", "Multiple permission deletion. Check Status array"); if (errorDeleting) { permissionDeleteResultArray .put(new JSONObject().put("status", "Some permissions could not be removed")); } result.put("statusArray", permissionDeleteResultArray); } else { for (String id : userOrGroupId) { for (String f : files) { JSONObject individualResult = new JSONObject(); boolean deleteResult = deletePermissions(f, id); if (deleteResult) { individualResult.put("status", String.format("Permission for %s and path %s deleted.", id, f)); } else { individualResult.put("status", String.format("Failed to delete permission for %s and path %s.", id, f)); } permissionDeleteResultArray.put(individualResult); } } result.put("status", "Multiple permission deletion. Check Status array"); result.put("statusArray", permissionDeleteResultArray); } } else { result.put("status", "Required arguments user/role and path not found"); } return result.toString(2); }
From source file:pt.webdetails.cfr.CfrApi.java
private JSONArray getFileListJson(String baseDir) throws JSONException { IFile[] files = getRepository().listFiles(baseDir); JSONArray arr = new JSONArray(); if (files != null) { for (IFile file : files) { if (mr.isCurrentUserAllowed(FilePermissionEnum.READ, relativeFilePath(baseDir, file.getName()))) { JSONObject obj = new JSONObject(); obj.put("fileName", file.getName()); obj.put("isDirectory", file.isDirectory()); obj.put("path", baseDir); arr.put(obj); }/*from w w w .jav a 2 s. c o m*/ } } return arr; }
From source file:net.dv8tion.jda.core.handle.ReadyHandler.java
private void sendGuildSyncRequests() { if (guildsRequiringSyncing.isEmpty()) return;//from w w w . ja v a 2 s . c o m JSONArray guildIds = new JSONArray(); for (TLongIterator it = guildsRequiringSyncing.iterator(); it.hasNext();) { guildIds.put(it.next()); //We can only request 50 guilds in a single request, so after we've reached 50, send them // and reset the if (guildIds.length() == 50) { api.getClient().chunkOrSyncRequest( new JSONObject().put("op", WebSocketCode.GUILD_SYNC).put("d", guildIds)); guildIds = new JSONArray(); } } //Send the remaining guilds that need to be sent if (guildIds.length() > 0) { api.getClient() .chunkOrSyncRequest(new JSONObject().put("op", WebSocketCode.GUILD_SYNC).put("d", guildIds)); } guildsRequiringSyncing.clear(); }
From source file:net.dv8tion.jda.core.handle.ReadyHandler.java
private void sendMemberChunkRequests() { if (guildsRequiringChunking.isEmpty()) return;// w w w .java 2 s. c o m JSONArray guildIds = new JSONArray(); for (TLongIterator it = guildsRequiringChunking.iterator(); it.hasNext();) { guildIds.put(it.next()); //We can only request 50 guilds in a single request, so after we've reached 50, send them // and reset the if (guildIds.length() == 50) { api.getClient().chunkOrSyncRequest(new JSONObject().put("op", 8).put("d", new JSONObject().put("guild_id", guildIds).put("query", "").put("limit", 0))); guildIds = new JSONArray(); } } //Send the remaining guilds that need to be sent if (guildIds.length() > 0) { api.getClient().chunkOrSyncRequest(new JSONObject().put("op", 8).put("d", new JSONObject().put("guild_id", guildIds).put("query", "").put("limit", 0))); } guildsRequiringChunking.clear(); }
From source file:org.archive.porky.JSON.java
/** * Convert the given Pig object into a JSON object, recursively * convert child objects as well.//w w w . j a va 2s . co m */ public static Object toJSON(Object o) throws JSONException, IOException { switch (DataType.findType(o)) { case DataType.NULL: return JSONObject.NULL; case DataType.BOOLEAN: case DataType.INTEGER: case DataType.LONG: case DataType.DOUBLE: return o; case DataType.FLOAT: return Double.valueOf(((Float) o).floatValue()); case DataType.CHARARRAY: return o.toString(); case DataType.MAP: { Map<String, Object> m = (Map<String, Object>) o; JSONObject json = new JSONObject(); for (Map.Entry<String, Object> e : m.entrySet()) { String key = e.getKey(); Object value = toJSON(e.getValue()); json.put(key, value); } return json; } case DataType.TUPLE: { JSONObject json = new JSONObject(); Tuple t = (Tuple) o; for (int i = 0; i < t.size(); ++i) { Object value = toJSON(t.get(i)); json.put("$" + i, value); } return json; } case DataType.BAG: { JSONArray values = new JSONArray(); for (Tuple t : ((DataBag) o)) { switch (t.size()) { case 0: continue; case 1: { Object innerObject = toJSON(t.get(0)); values.put(innerObject); } break; default: JSONArray innerList = new JSONArray(); for (int i = 0; i < t.size(); ++i) { Object innerObject = toJSON(t.get(i)); innerList.put(innerObject); } values.put(innerList); break; } } return values; } case DataType.BYTEARRAY: // FIXME? What else can we do? base-64 encoded string? System.err.println("Pig BYTEARRAY not supported for JSONStorage"); return null; default: System.out.println("unknown type: " + DataType.findType(o) + " value: " + o.toString()); return null; } }
From source file:com.eincs.athens.DataUtils.java
public static String toResponseString(List<? extends JConvertable> convertableList) throws JSONException { JSONObject result = new JSONObject(); JSONArray list = new JSONArray(); for (JConvertable convertable : convertableList) { list.put(convertable.toJSON()); }//from w w w .java 2s . c o m result.put("result", list); return result.toString(); }
From source file:cc.redpen.formatter.JSONFormatter.java
@Override public void format(PrintWriter pw, Map<Document, List<ValidationError>> docErrorsMap) throws RedPenException, IOException { BufferedWriter writer = new BufferedWriter(new PrintWriter(pw)); JSONArray errors = new JSONArray(); docErrorsMap.forEach((doc, errorList) -> errors.put(asJSON(doc, errorList))); writer.write(errors.toString());// www . ja v a 2s . co m writer.flush(); }
From source file:cc.redpen.formatter.JSONFormatter.java
/** * Render as a JSON object a list of errors for a given document * * @param document the document that has the errors * @param errors a list of errors/* w ww . j a va2 s. c om*/ * @return a JSON object representing the errors */ protected JSONObject asJSON(Document document, List<ValidationError> errors) { JSONObject jsonErrors = new JSONObject(); try { if (document.getFileName().isPresent()) { jsonErrors.put("document", document.getFileName().get()); } JSONArray documentErrors = new JSONArray(); for (ValidationError error : errors) { documentErrors.put(asJSON(error)); } jsonErrors.put("errors", documentErrors); } catch (JSONException e) { throw new RuntimeException(e); } return jsonErrors; }
From source file:com.facebook.share.internal.ShareInternalUtility.java
public static JSONArray removeNamespacesFromOGJsonArray(JSONArray jsonArray, boolean requireNamespace) throws JSONException { JSONArray newArray = new JSONArray(); for (int i = 0; i < jsonArray.length(); ++i) { Object value = jsonArray.get(i); if (value instanceof JSONArray) { value = removeNamespacesFromOGJsonArray((JSONArray) value, requireNamespace); } else if (value instanceof JSONObject) { value = removeNamespacesFromOGJsonObject((JSONObject) value, requireNamespace); }/*from w w w. j a v a 2s. c o m*/ newArray.put(value); } return newArray; }
From source file:org.fiware.cybercaptor.server.informationsystem.InformationSystem.java
/** * Generates the Json object relative to the hosts list * @return the Json Object containing the hosts list *//*from ww w .j a v a2s .c o m*/ public JSONObject getHostsListJson() { //Build the json list of hosts JSONObject json = new JSONObject(); JSONArray hosts_array = new JSONArray(); for (Host host : this.getTopology().getHosts()) { InformationSystemHost informationSystemHost = (InformationSystemHost) host; JSONObject host_object = new JSONObject(); host_object.put("name", informationSystemHost.getName()); JSONArray security_requirements_array = new JSONArray(); for (SecurityRequirement securityRequirement : informationSystemHost.getSecurityRequirements()) { JSONObject security_requirement = new JSONObject(); security_requirement.put("name", securityRequirement.getName()); security_requirement.put("metric", securityRequirement.getMetricPlainText()); security_requirements_array.put(security_requirement); } host_object.put("security_requirements", security_requirements_array); hosts_array.put(host_object); } json.put("hosts", hosts_array); return json; }