Search in sources :

Example 96 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class SubstringJSONObjectFilter method toJSONObject.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public JSONObject toJSONObject() {
    final LinkedHashMap<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(6));
    fields.put(FIELD_FILTER_TYPE, new JSONString(FILTER_TYPE));
    if (field.size() == 1) {
        fields.put(FIELD_FIELD_PATH, new JSONString(field.get(0)));
    } else {
        final ArrayList<JSONValue> fieldNameValues = new ArrayList<>(field.size());
        for (final String s : field) {
            fieldNameValues.add(new JSONString(s));
        }
        fields.put(FIELD_FIELD_PATH, new JSONArray(fieldNameValues));
    }
    if (startsWith != null) {
        fields.put(FIELD_STARTS_WITH, new JSONString(startsWith));
    }
    if (!contains.isEmpty()) {
        if (contains.size() == 1) {
            fields.put(FIELD_CONTAINS, new JSONString(contains.get(0)));
        } else {
            final ArrayList<JSONValue> containsValues = new ArrayList<>(contains.size());
            for (final String s : contains) {
                containsValues.add(new JSONString(s));
            }
            fields.put(FIELD_CONTAINS, new JSONArray(containsValues));
        }
    }
    if (endsWith != null) {
        fields.put(FIELD_ENDS_WITH, new JSONString(endsWith));
    }
    if (caseSensitive) {
        fields.put(FIELD_CASE_SENSITIVE, JSONBoolean.TRUE);
    }
    return new JSONObject(fields);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.unboundid.util.json.JSONArray) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 97 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class JSONLogFieldSyntax method tokenizeValue.

/**
 * Retrieves a tokenized representation of the provided JSON value.
 *
 * @param  value  The value to be tokenized.
 * @param  pepper  A pepper used to provide brute-force protection for the
 *                 resulting token.  The pepper value should be kept secret so
 *                 that it is not available to unauthorized users who might be
 *                 able to view log information, although the same pepper
 *                 value should be consistently provided when tokenizing
 *                 values so that the same value will consistently yield the
 *                 same token.  It must not be {@code null} and should not be
 *                 empty.
 *
 * @return  A tokenized representation of the provided JSON value.
 */
@NotNull()
private JSONValue tokenizeValue(@NotNull final JSONValue value, @NotNull final byte[] pepper) {
    if (value instanceof JSONObject) {
        final Map<String, JSONValue> originalFields = ((JSONObject) value).getFields();
        final Map<String, JSONValue> tokenizedFields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(originalFields.size()));
        for (final Map.Entry<String, JSONValue> e : originalFields.entrySet()) {
            final String fieldName = e.getKey();
            final JSONValue fieldValue = e.getValue();
            if (shouldRedactOrTokenize(fieldName)) {
                final String tokenizedValue = tokenize(fieldValue.toNormalizedString(), pepper);
                tokenizedFields.put(fieldName, new JSONString(tokenizedValue));
            } else {
                tokenizedFields.put(fieldName, tokenizeValue(fieldValue, pepper));
            }
        }
        return new JSONObject(tokenizedFields);
    } else if (value instanceof JSONArray) {
        final List<JSONValue> originalValues = ((JSONArray) value).getValues();
        final List<JSONValue> tokenizedValues = new ArrayList<>(originalValues.size());
        for (final JSONValue v : originalValues) {
            tokenizedValues.add(tokenizeValue(v, pepper));
        }
        return new JSONArray(tokenizedValues);
    } else {
        return sanitize(value);
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JSONString(com.unboundid.util.json.JSONString) LinkedHashMap(java.util.LinkedHashMap) NotNull(com.unboundid.util.NotNull)

Example 98 with JSONArray

use of com.unboundid.util.json.JSONArray in project ldapsdk by pingidentity.

the class JSONLogMessage method valueToStrings.

/**
 * Retrieves a list of the string representations of the values represented by
 * the provided JSON value.
 *
 * @param  value  The JSON value for which to obtain the string
 *                representations.  It must not be {@code null}.
 *
 * @return  A list of the string representations of the values represented by
 *          the provided JSON value.
 */
@NotNull()
static List<String> valueToStrings(@NotNull final JSONValue value) {
    if (value instanceof JSONArray) {
        final JSONArray a = (JSONArray) value;
        final List<JSONValue> valueList = a.getValues();
        final List<String> valueStrings = new ArrayList<>(valueList.size());
        for (final JSONValue v : valueList) {
            if (v instanceof JSONString) {
                valueStrings.add(((JSONString) v).stringValue());
            } else {
                valueStrings.add(v.toSingleLineString());
            }
        }
        return Collections.unmodifiableList(valueStrings);
    } else if (value instanceof JSONString) {
        return Collections.singletonList(((JSONString) value).stringValue());
    } else {
        return Collections.singletonList(value.toSingleLineString());
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONArray(com.unboundid.util.json.JSONArray) ArrayList(java.util.ArrayList) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Aggregations

JSONArray (com.unboundid.util.json.JSONArray)98 JSONObject (com.unboundid.util.json.JSONObject)89 JSONString (com.unboundid.util.json.JSONString)77 Test (org.testng.annotations.Test)72 JSONField (com.unboundid.util.json.JSONField)68 JSONValue (com.unboundid.util.json.JSONValue)27 JSONNumber (com.unboundid.util.json.JSONNumber)22 NotNull (com.unboundid.util.NotNull)20 ArrayList (java.util.ArrayList)19 LinkedHashMap (java.util.LinkedHashMap)18 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)11 PasswordQualityRequirement (com.unboundid.ldap.sdk.unboundidds.extensions.PasswordQualityRequirement)7 Entry (com.unboundid.ldap.sdk.Entry)6 Map (java.util.Map)6 LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)5 JSONBoolean (com.unboundid.util.json.JSONBoolean)5 JSONException (com.unboundid.util.json.JSONException)5 Date (java.util.Date)4 List (java.util.List)4 LogField (com.unboundid.ldap.sdk.unboundidds.logs.v2.LogField)3