/** * 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; }
/** * Get the multi-value keywords property as a JSON string. * * @return The multi-value keyword property as a JSON string. */ public String getKeywordsJSON() { JSONArray jsonArray = null; if (keywords != null) { jsonArray = new JSONArray(Arrays.asList(keywords)); } else { jsonArray = new JSONArray(); } return jsonArray.toString(); }
@Override protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); final JSONArray jsonArray = new JSONArray(); try { for (Map.Entry<String, TagDataConverter> entry : this.tagDataConverters.entrySet()) { final JSONObject jsonObject = new JSONObject(); jsonObject.put("label", entry.getValue().getLabel()); jsonObject.put("value", entry.getKey()); jsonArray.put(jsonObject); } response.getWriter().print(jsonArray.toString()); } catch (JSONException e) { response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
@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); }
@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); } }
/** * 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; }
return new JSONObject(map); }).collect(Collectors.toList()); final JSONArray jsonArray = new JSONArray(collect); return jsonArray.toString(); }; JSONArray a = new JSONArray(data); final int length = a.length(); final ArrayList<SearchHistory> searchHistories = new ArrayList<>(length); 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);
/** * Put a key/value pair in the JSONObject, where the value will be a * JSONArray which is produced from a Collection. * @param key A key string. * @param value A Collection value. * @return this. * @throws JSONException */ public JSONObject put(String key, Collection<?> value) throws JSONException { put(key, new JSONArray(value)); return this; }
/** * Append a boolean value. This increases the array's length by one. * * @param value A boolean value. * @return this. */ public JSONArray put(boolean value) { put(value ? Boolean.TRUE : Boolean.FALSE); return this; }
/** * 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; }
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 Object toArray(JSONObject properties, String propertyName, Class propertyType) throws JSONException { JSONArray array = properties.getJSONArray(propertyName); if (propertyType.equals(String.class)) { String[] values = new String[array.length()]; for (int i = 0; i < values.length; i++) { values[i] = array.getString(i); int[] values = new int[array.length()]; for (int i = 0; i < values.length; i++) { values[i] = array.getInt(i); long[] values = new long[array.length()]; for (int i = 0; i < values.length; i++) { values[i] = array.getLong(i); double[] values = new double[array.length()]; for (int i = 0; i < values.length; i++) { values[i] = array.getDouble(i); boolean[] values = new boolean[array.length()]; for (int i = 0; i < values.length; i++) { values[i] = array.getBoolean(i);
/** * 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(); }
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; }
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)) { double firstTimestamp = firstAlternative.getJSONArray("timestamps").getJSONArray(0).getDouble(1); builder.append("[").append(firstTimestamp).append("s]: ").append(line).append("\n");
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); }
/** * Get the JSONArray associated with an index. * @param index The index must be between 0 and length() - 1. * @return A JSONArray value. * @throws JSONException If there is no value for the index. or if the * value is not a JSONArray */ public JSONArray getJSONArray(int index) throws JSONException { Object o = get(index); if (o instanceof JSONArray) { return (JSONArray) o; } else if (o instanceof String) { JSONTokener tokener = new JSONTokener((String) o); try { return new JSONArray(tokener); } catch (JSONException ignore) { // will throw the appropriate exception below } } throw new JSONException("JSONArray[" + index + "] is not a JSONArray."); }