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;
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations