/** * Get the optional JSONObject associated with an index. * Null is returned if the key is not found, or null if the index has * no value, or if the value is not a JSONObject. * * @param index The index must be between 0 and length() - 1. * @return A JSONObject value. */ public JSONObject optJSONObject(int index) { try { return getJSONObject(index); } catch (Exception e) { return null; } }
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 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); } }
for (int i = 0; i < length; i++) { try { final JSONObject o = a.getJSONObject(i); final String searchMde = o.getString(SEARCH_MODE); final String query = o.getString(QUERY);
JSONArray results = json.getJSONArray("results").getJSONObject(0).getJSONArray("results"); StringBuilder builder = new StringBuilder(); for (int i = 0; i < results.length(); i++) { JSONObject result = results.getJSONObject(i); if (result.getBoolean("final")) { JSONObject firstAlternative = result.getJSONArray("alternatives").getJSONObject(0); String line = firstAlternative.getString("transcript"); if (StringUtils.isNotBlank(line)) {
final JSONObject item = jsonArray.getJSONObject(i);
final JSONObject tmp = jsonArray.getJSONObject(i); final String pattern = tmp.optString("pattern");
JSONObject object = data.getJSONObject(i); options.add(new Option(object.getString(value)));
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); }
JSONObject object = data.getJSONObject(i); options.add(new Option(object.getString(value)));
@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); }