Search in sources :

Example 41 with JSONValue

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

the class GreaterThanJSONObjectFilter method matchesJSONObject.

/**
 * {@inheritDoc}
 */
@Override()
public boolean matchesJSONObject(@NotNull final JSONObject o) {
    final List<JSONValue> candidates = getValues(o, field);
    if (candidates.isEmpty()) {
        return false;
    }
    for (final JSONValue v : candidates) {
        if (v instanceof JSONArray) {
            boolean matchOne = false;
            boolean matchAll = true;
            for (final JSONValue arrayValue : ((JSONArray) v).getValues()) {
                if (matches(arrayValue)) {
                    if (!matchAllElements) {
                        return true;
                    }
                    matchOne = true;
                } else {
                    matchAll = false;
                    if (matchAllElements) {
                        break;
                    }
                }
            }
            if (matchAllElements && matchOne && matchAll) {
                return true;
            }
        } else if (matches(v)) {
            return true;
        }
    }
    return false;
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONArray(com.unboundid.util.json.JSONArray)

Example 42 with JSONValue

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

the class JSONObjectFilter method decode.

/**
 * Decodes the provided JSON object as a JSON object filter.
 *
 * @param  o  The JSON object to be decoded as a JSON object filter.
 *
 * @return  The JSON object filter decoded from the provided JSON object.
 *
 * @throws  JSONException  If the provided JSON object cannot be decoded as a
 *                         JSON object filter.
 */
@NotNull()
public static JSONObjectFilter decode(@NotNull final JSONObject o) throws JSONException {
    // Get the value of the filter type field for the object and use it to get
    // a filter instance we can use to decode filters of that type.
    final JSONValue filterTypeValue = o.getField(FIELD_FILTER_TYPE);
    if (filterTypeValue == null) {
        throw new JSONException(ERR_OBJECT_FILTER_MISSING_FILTER_TYPE.get(String.valueOf(o), FIELD_FILTER_TYPE));
    }
    if (!(filterTypeValue instanceof JSONString)) {
        throw new JSONException(ERR_OBJECT_FILTER_INVALID_FILTER_TYPE.get(String.valueOf(o), FIELD_FILTER_TYPE));
    }
    final String filterType = StaticUtils.toLowerCase(((JSONString) filterTypeValue).stringValue());
    final JSONObjectFilter decoder = FILTER_TYPES.get(filterType);
    if (decoder == null) {
        throw new JSONException(ERR_OBJECT_FILTER_INVALID_FILTER_TYPE.get(String.valueOf(o), FIELD_FILTER_TYPE));
    }
    // Validate the set of fields contained in the provided object to ensure
    // that all required fields were provided and that no disallowed fields were
    // included.
    final HashSet<String> objectFields = new HashSet<>(o.getFields().keySet());
    objectFields.remove(FIELD_FILTER_TYPE);
    for (final String requiredField : decoder.getRequiredFieldNames()) {
        if (!objectFields.remove(requiredField)) {
            throw new JSONException(ERR_OBJECT_FILTER_MISSING_REQUIRED_FIELD.get(String.valueOf(o), decoder.getFilterType(), requiredField));
        }
    }
    for (final String remainingField : objectFields) {
        if (!decoder.getOptionalFieldNames().contains(remainingField)) {
            throw new JSONException(ERR_OBJECT_FILTER_UNRECOGNIZED_FIELD.get(String.valueOf(o), decoder.getFilterType(), remainingField));
        }
    }
    return decoder.decodeFilter(o);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) HashSet(java.util.HashSet) NotNull(com.unboundid.util.NotNull)

Example 43 with JSONValue

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

the class JSONObjectFilter method getValues.

/**
 * Retrieves the set of values that match the provided field name specifier.
 *
 * @param  o               The JSON object to examine.
 * @param  fieldName       The field name specifier for the values to
 *                         retrieve.
 * @param  fieldNameIndex  The current index into the field name specifier.
 * @param  values          The list into which matching values should be
 *                         added.
 */
private static void getValues(@NotNull final JSONObject o, @NotNull final List<String> fieldName, final int fieldNameIndex, @NotNull final List<JSONValue> values) {
    final JSONValue v = o.getField(fieldName.get(fieldNameIndex));
    if (v == null) {
        return;
    }
    final int nextIndex = fieldNameIndex + 1;
    if (nextIndex < fieldName.size()) {
        // objects.
        if (v instanceof JSONObject) {
            getValues((JSONObject) v, fieldName, nextIndex, values);
        } else if (v instanceof JSONArray) {
            getValuesFromArray((JSONArray) v, fieldName, nextIndex, values);
        }
        return;
    }
    // If we've gotten here, then there is no more of the field specifier, so
    // the value we retrieved matches the specifier.  Add it to the list of
    // values.
    values.add(v);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray)

Example 44 with JSONValue

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

the class ORJSONObjectFilter method toJSONObject.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public JSONObject toJSONObject() {
    final LinkedHashMap<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(3));
    fields.put(FIELD_FILTER_TYPE, new JSONString(FILTER_TYPE));
    final ArrayList<JSONValue> filterValues = new ArrayList<>(orFilters.size());
    for (final JSONObjectFilter f : orFilters) {
        filterValues.add(f.toJSONObject());
    }
    fields.put(FIELD_OR_FILTERS, new JSONArray(filterValues));
    if (exclusive) {
        fields.put(FIELD_EXCLUSIVE, 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 45 with JSONValue

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

the class ObjectMatchesJSONObjectFilter method decodeFilter.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
protected ObjectMatchesJSONObjectFilter decodeFilter(@NotNull final JSONObject filterObject) throws JSONException {
    final List<String> fieldPath = getStrings(filterObject, FIELD_FIELD_PATH, false, null);
    final JSONValue v = filterObject.getField(FIELD_FILTER);
    if (v == null) {
        throw new JSONException(ERR_OBJECT_FILTER_MISSING_REQUIRED_FIELD.get(String.valueOf(filterObject), FILTER_TYPE, FIELD_FILTER));
    }
    if (!(v instanceof JSONObject)) {
        throw new JSONException(ERR_OBJECT_FILTER_VALUE_NOT_OBJECT.get(String.valueOf(filterObject), FILTER_TYPE, FIELD_FILTER));
    }
    try {
        return new ObjectMatchesJSONObjectFilter(fieldPath, JSONObjectFilter.decode((JSONObject) v));
    } catch (final JSONException e) {
        Debug.debugException(e);
        throw new JSONException(ERR_OBJECT_FILTER_VALUE_NOT_FILTER.get(String.valueOf(filterObject), FILTER_TYPE, FIELD_FILTER, e.getMessage()), e);
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONException(com.unboundid.util.json.JSONException) JSONString(com.unboundid.util.json.JSONString) NotNull(com.unboundid.util.NotNull)

Aggregations

JSONValue (com.unboundid.util.json.JSONValue)50 JSONString (com.unboundid.util.json.JSONString)45 JSONObject (com.unboundid.util.json.JSONObject)40 NotNull (com.unboundid.util.NotNull)33 LinkedHashMap (java.util.LinkedHashMap)31 JSONArray (com.unboundid.util.json.JSONArray)27 ArrayList (java.util.ArrayList)27 Test (org.testng.annotations.Test)9 LogException (com.unboundid.ldap.sdk.unboundidds.logs.LogException)8 JSONBoolean (com.unboundid.util.json.JSONBoolean)6 Map (java.util.Map)6 JSONNumber (com.unboundid.util.json.JSONNumber)5 File (java.io.File)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 JSONException (com.unboundid.util.json.JSONException)4 JSONField (com.unboundid.util.json.JSONField)4 Date (java.util.Date)4 List (java.util.List)4 Entry (com.unboundid.ldap.sdk.Entry)3 PasswordPolicyStateJSONField (com.unboundid.ldap.sdk.unboundidds.PasswordPolicyStateJSONField)2