/** * Put a value in the JSONArray, where the value will be a * JSONArray which is produced from a Collection.<p> * * @param value a Collection value * @return this */ public JSONArray put(Collection<Object> value) { put(new JSONArray(value)); return this; }
/** * Put a value in the JSONArray, where the value will be a * JSONArray which is produced from a Collection.<p> * * @param value a Collection value * @return this */ public JSONArray put(Collection value) { put(new JSONArray(value)); return this; }
/** * Put a key/value pair in the JSONObject, where the value will be a * JSONArray which is produced from a Collection.<p> * * @param key a key string * @param value a Collection value * @return this * @throws JSONException if something goes wrong */ public JSONObject put(String key, Collection<Object> value) throws JSONException { put(key, new JSONArray(value)); return this; }
/** * Put a key/value pair in the JSONObject, where the value will be a * JSONArray which is produced from a Collection.<p> * * @param key a key string * @param value a Collection value * @return this * @throws JSONException if something goes wrong */ public JSONObject put(String key, Collection value) throws JSONException { put(key, new JSONArray(value)); return this; }
/** * Put a value in the JSONArray, where the value will be a * JSONArray which is produced from a Collection.<p> * * @param index the index must be between 0 and length() - 1 * @param value a Collection value * @return this * @throws JSONException if the index is negative or if the value is * not finite */ public JSONArray put(int index, Collection<Object> value) throws JSONException { put(index, new JSONArray(value)); return this; }
/** * Put a value in the JSONArray, where the value will be a * JSONArray which is produced from a Collection.<p> * * @param index the index must be between 0 and length() - 1 * @param value a Collection value * @return this * @throws JSONException if the index is negative or if the value is * not finite */ public JSONArray put(int index, Collection value) throws JSONException { put(index, new JSONArray(value)); return this; }
/** * Produce a JSONArray containing the names of the elements of this JSONObject.<p> * * @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 containing the names of the elements of this JSONObject.<p> * * @return a JSONArray containing the key strings, or null if the JSONObject is empty. */ public JSONArray names() { JSONArray ja = new JSONArray(); Iterator keys = keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
/** * Decodes a parameter which has been encoded from a string list using encodeStringsAsBase64Parameter.<p> * * @param data the data to decode * @return the list of strings */ public static List<String> decodeStringsFromBase64Parameter(String data) { data = StringUtils.replaceChars(data, BASE64_EXTRA_REPLACEMENTS, BASE64_EXTRA); byte[] bytes = deobfuscateBytes(Base64.decodeBase64(data)); try { JSONArray json = new JSONArray(new String(bytes, "UTF-8")); List<String> result = Lists.newArrayList(); for (int i = 0; i < json.length(); i++) { result.add(json.getString(i)); } return result; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { throw new IllegalArgumentException("Decoding failed: " + data, e); } return null; }
/** * Converts the given parameter map into an JSON object.<p> * * @param params the parameters map to convert * * @return the JSON representation of the given parameter map */ public static JSONObject getJsonParameterMap(Map<String, String[]> params) { JSONObject result = new JSONObject(); for (Map.Entry<String, String[]> entry : params.entrySet()) { String paramKey = entry.getKey(); JSONArray paramValue = new JSONArray(); for (int i = 0, l = entry.getValue().length; i < l; i++) { paramValue.put(entry.getValue()[i]); } try { result.putOpt(paramKey, paramValue); } catch (JSONException e) { // should never happen LOG.warn(e.getLocalizedMessage(), e); } } return result; }
/** * Encode a list of strings as base64 data to be used in a request parameter.<p> * * @param strings the strings to encode * @return the resulting base64 data */ public static String encodeStringsAsBase64Parameter(List<String> strings) { JSONArray array = new JSONArray(); for (String string : strings) { array.put(string); } byte[] bytes; try { // use obfuscateBytes here to to make the output look more random bytes = obfuscateBytes(array.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { // should never happen e.printStackTrace(); throw new RuntimeException(e); } String result = Base64.encodeBase64String(bytes); result = StringUtils.replaceChars(result, BASE64_EXTRA, BASE64_EXTRA_REPLACEMENTS); return result; }
/** * Writes the user quick launch apps setting to the user additional info.<p> * * @param cms the cms context * @param apps the app ids * * @throws CmsException in case writing the user fails */ protected void setUserQuickLaunchApps(CmsObject cms, List<String> apps) throws CmsException { JSONArray appIds = new JSONArray(apps); CmsUser user = cms.getRequestContext().getCurrentUser(); user.setAdditionalInfo(QUICK_LAUCH_APPS_KEY, appIds.toString()); cms.writeUser(user); }
/** * Accumulate values under a key.<p> * * It is similar to the put method except * that if there is already an object stored under the key then a * JSONArray is stored under the key to hold all of the accumulated values. * If there is already a JSONArray, then the new value is appended to it. * In contrast, the put method replaces the previous value.<p> * * @param key a key string * @param value an object to be accumulated under the key * @return this * @throws JSONException if the value is an invalid number or if the key is null */ public JSONObject accumulate(String key, Object value) throws JSONException { testValidity(value); Object o = opt(key); if (o == null) { put(key, value instanceof JSONArray ? new JSONArray().put(value) : value); } else if (o instanceof JSONArray) { ((JSONArray)o).put(value); } else { put(key, new JSONArray().put(o).put(value)); } return this; }
/** * @see org.opencms.ui.apps.I_CmsAppSettings#getSettingsString() */ public String getSettingsString() { JSONObject json = new JSONObject(); try { json.put(SORT_ORDER_KEY, m_sortAscending); json.put(SORT_COLUMN_KEY, m_sortColumnId.getId()); List<String> collapsed = Lists.newArrayList(); for (CmsResourceTableProperty column : m_collapsedColumns) { collapsed.add(column.getId()); } json.put(COLLAPSED_COLUMNS_KEY, new JSONArray(collapsed)); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return json.toString(); }
/** * Produce a JSONArray of JSONObjects from a comma delimited text string * using a supplied JSONArray as the source of element names.<p> * * @param names A JSONArray of strings * @param x A JSONTokener of the source text * @return A JSONArray of JSONObjects * @throws JSONException if something goes wrong */ 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.<p> * * @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; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject.<p> * * @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(opt(names.getString(i))); } return ja; }
/** * Returns the quick launch apps set for the current user.<p> * * @param cms the cms context * * @return the quick launch app configurations */ protected List<I_CmsWorkplaceAppConfiguration> getUserQuickLauchConfigurations(CmsObject cms) { String apps_info = (String)cms.getRequestContext().getCurrentUser().getAdditionalInfo(QUICK_LAUCH_APPS_KEY); String[] appIds = null; if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(apps_info)) { try { JSONArray ids = new JSONArray(apps_info); appIds = new String[ids.length()]; for (int i = 0; i < appIds.length; i++) { appIds[i] = ids.getString(i); } } catch (JSONException e) { LOG.error("Error parsing user quick launch apps setting.", e); appIds = null; } } return getAppConfigurations(appIds != null ? appIds : DEFAULT_USER_APPS); }
/** * Transforms the response content as a string.<p> * * @param content the content * @return the transformed content */ private String transformContent(String content) { try { List<CmsJsonPart> parts = CmsJsonPart.parseJsonParts(content); JSONArray keys = new JSONArray(); JSONObject output = new JSONObject(); for (CmsJsonPart part : parts) { if (output.has(part.getKey())) { LOG.warn("Duplicate key for JSON parts: " + part.getKey()); } keys.put(part.getKey()); output.put(part.getKey(), part.getValue()); } output.put(KEY_PARTS, keys); return output.toString(); } catch (Exception e) { LOG.error(e.getLocalizedMessage(), e); return content; } } }
/** * Append values to the array under a key.<p> * * If the key does not exist in the * JSONObject, then the key is put in the JSONObject with its value being a * JSONArray containing the value parameter. If the key was already * associated with a JSONArray, then the value parameter is appended to it.<p> * * @param key a key string * @param value an object to be accumulated under the key * @return this * @throws JSONException if the key is null or if the current value * associated with the key is not a JSONArray */ public JSONObject append(String key, Object value) throws JSONException { testValidity(value); Object o = opt(key); if (o == null) { put(key, new JSONArray().put(value)); } else if (o instanceof JSONArray) { put(key, ((JSONArray)o).put(value)); } else { throw new JSONException("JSONObject[" + key + "] is not a JSONArray."); } return this; }