/** * Get the optional object value associated with an index. * @param index The index must be between 0 and length() - 1. * @return An object value, or null if there is no * object at that index. */ public Object opt(int index) { return (index < 0 || index >= length()) ? null : this.myArrayList.get(index); }
/** * Make a string from the contents of this JSONArray. The * <code>separator</code> string is inserted between each element. * Warning: This method assumes that the data structure is acyclical. * @param separator A string that will be inserted between the elements. * @return a string. * @throws JSONException If the array contains an invalid number. */ public String join(JSONArray ja, String separator) throws JSONException { final int len = ja.length(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < len; i += 1) { if (i > 0) { sb.append(separator); } sb.append(JSONObject.valueToString(ja.get(i))); } return sb.toString(); }
/** * Produce a JSONObject by combining a JSONArray of names with the values * of this JSONArray. * @param names A JSONArray containing a list of key strings. These will be * paired with the values. * @return A JSONObject, or null if there are no names or if this JSONArray * has no values. * @throws JSONException If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.length() == 0 || length() == 0) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
/** * Produce a comma delimited text from a JSONArray of JSONObjects using * a provided list of names. The list of names is not included in the * output. * @param names A JSONArray of strings. * @param ja A JSONArray of JSONObjects. * @return A comma delimited text. * @throws JSONException */ public static String toString(JSONArray names, JSONArray ja) throws JSONException { if (names == null || names.length() == 0) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < ja.length(); i += 1) { JSONObject jo = ja.optJSONObject(i); if (jo != null) { sb.append(rowToString(jo.toJSONArray(names))); } } return sb.toString(); } }
/** * Produce a JSONArray containing the names of the elements of this * JSONObject. * @return A JSONArray containing the key strings, or null if the JSONObject * is empty. */ public JSONArray names() { JSONArray ja = new JSONArray(); Iterator<String> keys = keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
/** * Produce a JSONArray of JSONObjects from a comma delimited text string * using a supplied JSONArray as the source of element names. * @param names A JSONArray of strings. * @param x A JSONTokener of the source text. * @return A JSONArray of JSONObjects. * @throws JSONException */ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (;;) { JSONObject jo = rowToJSONObject(names, x); if (jo == null) { break; } ja.put(jo); } if (ja.length() == 0) { return null; } return ja; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject. * @param names A JSONArray containing a list of key strings. This * determines the sequence of the values in the result. * @return A JSONArray of values. * @throws JSONException If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.length() == 0) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
public Parameters(SlingHttpServletRequest request) throws IOException, JSONException { final JSONObject json = new JSONObject(request.getParameter("params")); final List<String> customProperties = new ArrayList<String>(); final List<String> groups = new ArrayList<String>(); groupFilter = json.getString(GROUP_FILTER); JSONArray groupsJSON = json.getJSONArray(GROUPS); for (int i = 0; i < groupsJSON.length(); i++) { groups.add(groupsJSON.getString(i)); } this.groups = groups.toArray(new String[groups.size()]); JSONArray customPropertiesJSON = json.getJSONArray(CUSTOM_PROPERTIES); for (int i = 0; i < customPropertiesJSON.length(); i++) { JSONObject tmp = customPropertiesJSON.getJSONObject(i); String relativePropertyPath = tmp.optString(RELATIVE_PROPERTY_PATH); if (StringUtils.isNotBlank(relativePropertyPath)) { customProperties.add(relativePropertyPath); } } this.customProperties = customProperties.toArray(new String[customProperties.size()]); }
private boolean isSupported(JSONObject obj) { try { if (obj.has(PROTOCOLS)) { JSONArray protocols = obj.getJSONArray(PROTOCOLS); for (int i = 0; i < protocols.length(); i++) { String protocol = protocols.getString(i); if (PROTOCOL_VERSION_7.equals(protocol)) { return true; } } } } catch (JSONException e) { } return false; }
private boolean isSupported(JSONObject obj) { try { if (obj.has(PROTOCOLS)) { JSONArray protocols = obj.getJSONArray(PROTOCOLS); for (int i = 0; i < protocols.length(); i++) { String protocol = protocols.getString(i); if (PROTOCOL_VERSION_7.equals(protocol)) { return true; } } } } catch (JSONException e) { } return false; }
/** * Put or replace an object value in the JSONArray. If the index is greater * than the length of the JSONArray, then null elements will be added as * necessary to pad it out. * @param index The subscript. * @param value The value to put into the array. The value should be a * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the * JSONObject.NULL object. * @return this. * @throws JSONException If the index is negative or if the the value is * an invalid number. */ public JSONArray put(int index, Object value) throws JSONException { JSONObject.testValidity(value); if (index < 0) { throw new JSONException("JSONArray[" + index + "] not found."); } if (index < length()) { this.myArrayList.set(index, value); } else { while (index != length()) { put(JSONObject.NULL); } put(value); } return this; }
private ConfigurationCollectionPersistData parseCollectionConfigData(JSONObject jsonData, ConfigurationMetadata configMetadata) throws JSONException { List<ConfigurationPersistData> items = new ArrayList<>(); JSONArray itemsObject = jsonData.getJSONArray("items"); for (int i = 0; i < itemsObject.length(); i++) { JSONObject item = itemsObject.getJSONObject(i); items.add(parseConfigData(item, configMetadata)); } Map<String, Object> properties = null; JSONObject propertiesObject = jsonData.optJSONObject("properties"); if (propertiesObject != null) { properties = new HashMap<>(); Iterator<String> propertyNames = propertiesObject.keys(); while (propertyNames.hasNext()) { String propertyName = propertyNames.next(); properties.put(propertyName, propertiesObject.get(propertyName)); } } return new ConfigurationCollectionPersistData(items) .properties(properties); }
@Override public Optional<List<Session>> deserialize(String data) { final ArrayList<Session> sessions = new ArrayList<>(); try { final JSONArray array = new JSONArray(data); for (int i = 0; i < array.length(); i++) { final JSONObject jsonObject = array.getJSONObject(i); String name = jsonObject.getString("name"); final ArrayList<FileToOpen> fileToOpens = new ArrayList<>(); final JSONArray filesArray = jsonObject.optJSONArray("filesToOpen"); if (filesArray != null){ for (int j = 0; j < filesArray.length(); j++) { final JSONObject filesToOpen = filesArray.getJSONObject(j); String uri = filesToOpen.getString("uri"); Level level = Level.parse(filesToOpen.getString("level")); OpenMode openMode = OpenMode.valueOf(filesToOpen.getString("openMode")); String logImporter = filesToOpen.optString("logImporter", null); fileToOpens.add(new FileToOpen(uri, openMode, level, Optional.ofNullable(logImporter))); } } sessions.add(new Session(name, fileToOpens)); } } catch (JSONException e) { LOGGER.error("Can't deserialize sessions: ", e); Optional.empty(); } LOGGER.info("Returning deserialized sessions: " + sessions.size()); return Optional.of(sessions); } }
try { JSONArray a = new JSONArray(data); final int length = a.length(); final ArrayList<SearchHistory> searchHistories = new ArrayList<>(length); for (int i = 0; i < length; i++) {
private void decorateEdge(JSONArray inputArray, StreamEdge inEdge, int mappedInputID) throws JSONException { JSONObject input = new JSONObject(); inputArray.put(input); input.put(ID, mappedInputID); input.put(SHIP_STRATEGY, inEdge.getPartitioner()); input.put(SIDE, (inputArray.length() == 0) ? "first" : "second"); }
/** * Support legacy data structures where link metadata is stored as JSON fragment in single HTML5 data attribute. * @param resourceProps ValueMap to write link metadata to * @param element Link element */ private boolean getAnchorLegacyMetadataFromSingleData(ValueMap resourceProps, Element element) { boolean foundAny = false; JSONObject metadata = null; Attribute dataAttribute = element.getAttribute("data"); if (dataAttribute != null) { String metadataString = dataAttribute.getValue(); if (StringUtils.isNotEmpty(metadataString)) { try { metadata = new JSONObject(metadataString); } catch (JSONException ex) { log.debug("Invalid link metadata: " + metadataString, ex); } } } if (metadata != null) { JSONArray names = metadata.names(); for (int i = 0; i < names.length(); i++) { String name = names.optString(i); resourceProps.put(name, metadata.opt(name)); foundAny = true; } } return foundAny; }
/** * Visit each JSON Object in the JSON Array. * * @param jsonArray The JSON Array */ protected final void traverseJSONArray(final JSONArray jsonArray) { if (jsonArray == null) { return; } for (int i = 0; i < jsonArray.length(); i++) { if (jsonArray.optJSONObject(i) != null) { this.accept(jsonArray.optJSONObject(i)); } else if (jsonArray.optJSONArray(i) != null) { this.accept(jsonArray.optJSONArray(i)); } } }
@SuppressWarnings("squid:S3776") private void filter(org.apache.sling.commons.json.JSONObject typeObject, String resourcePath, ResourceResolver resourceResolver) throws org.apache.sling.commons.json.JSONException { final org.apache.sling.commons.json.JSONArray models = typeObject.getJSONArray(KEY_MODELS); final org.apache.sling.commons.json.JSONArray newModels = new org.apache.sling.commons.json.JSONArray(); for (int i = 0; i < models.length(); i++) { final org.apache.sling.commons.json.JSONObject modelObject = models.getJSONObject(i); final String path = modelObject.getString(KEY_MODEL_PATH); final Resource modelResource = resourceResolver.getResource(path); if (modelResource != null) { // we're looking for the appliesTo property on the jcr:content node, the wid value // is the path to the jcr:content/model node. final ValueMap properties = modelResource.getParent().getValueMap(); final String[] allowedPaths = properties.get(PN_ALLOWED_PATHS, String[].class); if (allowedPaths == null) { newModels.put(modelObject); } else { for (final String allowedPath : allowedPaths) { if (resourcePath.matches(allowedPath)) { newModels.put(modelObject); break; } } } } } typeObject.put(KEY_MODELS, newModels); }
private void filter(JSONObject typeObject, String resourcePath, ResourceResolver resourceResolver) throws JSONException { final JSONArray models = typeObject.getJSONArray(KEY_MODELS); final JSONArray newModels = new JSONArray(); for (int i = 0; i < models.length(); i++) { final JSONObject modelObject = models.getJSONObject(i); final String path = modelObject.getString(KEY_MODEL_PATH); final Resource modelResource = resourceResolver.getResource(path); if (modelResource != null) { // we're looking for the appliesTo property on the jcr:content node, the wid value // is the path to the jcr:content/model node. final ValueMap properties = modelResource.getParent().getValueMap(); final String[] allowedPaths = properties.get(PN_ALLOWED_PATHS, String[].class); if (allowedPaths == null) { newModels.put(modelObject); } else { for (final String allowedPath : allowedPaths) { if (resourcePath.matches(allowedPath)) { newModels.put(modelObject); break; } } } } } typeObject.put(KEY_MODELS, newModels); }
this.array(); for (int i = 0; i < a.length(); i++) { JSONObject objVal = a.optJSONObject(i); if (objVal != null) {