Search in sources :

Example 16 with JSONValue

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

the class EqualsJSONObjectFilter method toJSONObject.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public JSONObject toJSONObject() {
    final LinkedHashMap<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(4));
    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));
    }
    fields.put(FIELD_VALUE, value);
    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 17 with JSONValue

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

the class RegularExpressionJSONObjectFilter method toJSONObject.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public JSONObject toJSONObject() {
    final LinkedHashMap<String, JSONValue> fields = new LinkedHashMap<>(StaticUtils.computeMapCapacity(4));
    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));
    }
    fields.put(FIELD_REGULAR_EXPRESSION, new JSONString(regularExpression.toString()));
    if (matchAllElements) {
        fields.put(FIELD_MATCH_ALL_ELEMENTS, 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 18 with JSONValue

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

the class ScrambleAttributeTransformation method scrambleObjectsInArray.

/**
 * Creates a new JSON array that will have all the same elements as the
 * provided array except that any values in the array that are JSON objects
 * (including objects contained in nested arrays) will have any appropriate
 * scrambling performed.
 *
 * @param  a  The JSON array for which to scramble any values.
 *
 * @return  The array with any appropriate scrambling performed.
 */
@NotNull()
private JSONArray scrambleObjectsInArray(@NotNull final JSONArray a) {
    final List<JSONValue> originalValues = a.getValues();
    final ArrayList<JSONValue> scrambledValues = new ArrayList<>(originalValues.size());
    for (final JSONValue arrayValue : originalValues) {
        if (arrayValue instanceof JSONArray) {
            scrambledValues.add(scrambleObjectsInArray((JSONArray) arrayValue));
        } else if (arrayValue instanceof JSONObject) {
            scrambledValues.add(scrambleJSONValue(arrayValue, false));
        } else {
            scrambledValues.add(arrayValue);
        }
    }
    return new JSONArray(scrambledValues);
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.unboundid.util.json.JSONArray) NotNull(com.unboundid.util.NotNull)

Example 19 with JSONValue

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

the class ModifiablePasswordPolicyStateJSONTestCase method createState.

/**
 * Creates a password policy state JSON object with the provided fields.
 *
 * @param  fields  The fields to include in the JSON object.
 *
 * @return  The password policy state JSON object that was created.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
private ModifiablePasswordPolicyStateJSON createState(final Map<ModifiablePasswordPolicyStateJSONField, ?> fields) throws Exception {
    final Map<String, JSONValue> jsonFields = new LinkedHashMap<>();
    for (final ModifiablePasswordPolicyStateJSONField field : fields.keySet()) {
        final String name = field.getFieldName();
        final Object value = fields.get(field);
        if (value instanceof Boolean) {
            final Boolean b = (Boolean) value;
            jsonFields.put(name, new JSONBoolean(b));
        } else if (value instanceof String) {
            final String s = (String) value;
            jsonFields.put(name, new JSONString(s));
        } else if (value instanceof Date) {
            final Date d = (Date) value;
            jsonFields.put(name, new JSONString(StaticUtils.encodeRFC3339Time(d)));
        } else if (value instanceof JSONValue) {
            jsonFields.put(name, (JSONValue) value);
        } else {
            fail("Unexpected field value " + value + " of type " + value.getClass().getName());
        }
    }
    final JSONObject o = new JSONObject(jsonFields);
    final Entry entry = new Entry("dn: uid=test.user,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: test.user", "givenName: Test", "sn: User", "cn: Test User");
    entry.addAttribute("ds-pwp-modifiable-state-json", o.toSingleLineString());
    final ModifiablePasswordPolicyStateJSON state = ModifiablePasswordPolicyStateJSON.get(entry);
    assertNotNull(state);
    assertNotNull(state.getModifiablePasswordPolicyStateJSONObject());
    assertFalse(state.getModifiablePasswordPolicyStateJSONObject().getFields().isEmpty());
    assertEquals(state.getModifiablePasswordPolicyStateJSONObject().getFields().size(), jsonFields.size());
    assertNotNull(state.toString());
    assertFalse(state.toString().isEmpty());
    return state;
}
Also used : ModifiablePasswordPolicyStateJSONField(com.unboundid.ldap.sdk.unboundidds.ModifiablePasswordPolicyStateJSONField) JSONString(com.unboundid.util.json.JSONString) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) JSONValue(com.unboundid.util.json.JSONValue) Entry(com.unboundid.ldap.sdk.Entry) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONObject(com.unboundid.util.json.JSONObject) JSONBoolean(com.unboundid.util.json.JSONBoolean) JSONString(com.unboundid.util.json.JSONString)

Example 20 with JSONValue

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

the class JSONAccessLogReaderTestCase method testReadObjectWithInvalidOperationType.

/**
 * Tests the behavior when trying to read a file containing a JSON object
 * for an operation message that has an invalid operation type.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testReadObjectWithInvalidOperationType() throws Exception {
    final JSONObject minimalMessageObject = createMinimalMessageObject(REQUEST, ABANDON);
    final Map<String, JSONValue> fieldsWithInvalidOperationType = new LinkedHashMap<>(minimalMessageObject.getFields());
    assertNotNull(fieldsWithInvalidOperationType.put(OPERATION_TYPE.getFieldName(), new JSONString("invalid")));
    final JSONObject objectWithoutMessageType = new JSONObject(fieldsWithInvalidOperationType);
    final File logFile = createTempFile(objectWithoutMessageType.toSingleLineString());
    try (JSONAccessLogReader reader = new JSONAccessLogReader(logFile)) {
        reader.readMessage();
        fail("Expected an exception for a file that contains a JSON object " + "with an invalid operation type");
    } catch (final LogException e) {
    // This was expected.
    }
}
Also used : JSONValue(com.unboundid.util.json.JSONValue) JSONObject(com.unboundid.util.json.JSONObject) JSONString(com.unboundid.util.json.JSONString) File(java.io.File) JSONString(com.unboundid.util.json.JSONString) LogException(com.unboundid.ldap.sdk.unboundidds.logs.LogException) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

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