Search in sources :

Example 6 with IndexedReadOnlyStringMap

use of org.apache.logging.log4j.util.IndexedReadOnlyStringMap in project logging-log4j2 by apache.

the class JsonWriter method writeValue.

public void writeValue(final Object value) {
    // null
    if (value == null) {
        writeNull();
    } else // map
    if (value instanceof IndexedReadOnlyStringMap) {
        final IndexedReadOnlyStringMap map = (IndexedReadOnlyStringMap) value;
        writeObject(map);
    } else if (value instanceof StringMap) {
        final StringMap map = (StringMap) value;
        writeObject(map);
    } else if (value instanceof Map) {
        @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) value;
        writeObject(map);
    } else // list & collection
    if (value instanceof List) {
        @SuppressWarnings("unchecked") final List<Object> list = (List<Object>) value;
        writeArray(list);
    } else if (value instanceof Collection) {
        @SuppressWarnings("unchecked") final Collection<Object> collection = (Collection<Object>) value;
        writeArray(collection);
    } else // number & boolean
    if (value instanceof Number) {
        final Number number = (Number) value;
        writeNumber(number);
    } else if (value instanceof Boolean) {
        final boolean booleanValue = (boolean) value;
        writeBoolean(booleanValue);
    } else // formattable
    if (value instanceof StringBuilderFormattable) {
        final StringBuilderFormattable formattable = (StringBuilderFormattable) value;
        writeString(formattable);
    } else // arrays
    if (value instanceof char[]) {
        final char[] charValues = (char[]) value;
        writeArray(charValues);
    } else if (value instanceof boolean[]) {
        final boolean[] booleanValues = (boolean[]) value;
        writeArray(booleanValues);
    } else if (value instanceof byte[]) {
        final byte[] byteValues = (byte[]) value;
        writeArray(byteValues);
    } else if (value instanceof short[]) {
        final short[] shortValues = (short[]) value;
        writeArray(shortValues);
    } else if (value instanceof int[]) {
        final int[] intValues = (int[]) value;
        writeArray(intValues);
    } else if (value instanceof long[]) {
        final long[] longValues = (long[]) value;
        writeArray(longValues);
    } else if (value instanceof float[]) {
        final float[] floatValues = (float[]) value;
        writeArray(floatValues);
    } else if (value instanceof double[]) {
        final double[] doubleValues = (double[]) value;
        writeArray(doubleValues);
    } else if (value instanceof Object[]) {
        final Object[] values = (Object[]) value;
        writeArray(values);
    } else // string
    {
        final String stringValue = value instanceof String ? (String) value : String.valueOf(value);
        writeString(stringValue);
    }
}
Also used : IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) StringMap(org.apache.logging.log4j.util.StringMap) StringBuilderFormattable(org.apache.logging.log4j.util.StringBuilderFormattable) Collection(java.util.Collection) List(java.util.List) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) Map(java.util.Map) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) StringMap(org.apache.logging.log4j.util.StringMap)

Example 7 with IndexedReadOnlyStringMap

use of org.apache.logging.log4j.util.IndexedReadOnlyStringMap in project logging-log4j2 by apache.

the class JdbcDatabaseManager method setFields.

private void setFields(final MapMessage<?, ?> mapMessage) throws SQLException {
    final IndexedReadOnlyStringMap map = mapMessage.getIndexedReadOnlyStringMap();
    final String simpleName = statement.getClass().getName();
    // JDBC indices start at 1
    int j = 1;
    for (final ColumnMapping mapping : this.factoryData.columnMappings) {
        if (mapping.getLiteralValue() == null) {
            final String source = mapping.getSource();
            final String key = Strings.isEmpty(source) ? mapping.getName() : source;
            final Object value = map.getValue(key);
            if (logger().isTraceEnabled()) {
                final String valueStr = value instanceof String ? "\"" + value + "\"" : Objects.toString(value, null);
                logger().trace("{} setObject({}, {}) for key '{}' and mapping '{}'", simpleName, j, valueStr, key, mapping.getName());
            }
            setStatementObject(j, mapping.getNameKey(), value);
            j++;
        }
    }
}
Also used : IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) ColumnMapping(org.apache.logging.log4j.core.appender.db.ColumnMapping)

Example 8 with IndexedReadOnlyStringMap

use of org.apache.logging.log4j.util.IndexedReadOnlyStringMap in project logging-log4j2 by apache.

the class JsonWriterTest method test_writeObject_IndexedReadOnlyStringMap.

@Test
void test_writeObject_IndexedReadOnlyStringMap() {
    final IndexedReadOnlyStringMap map = new SortedArrayStringMap(new LinkedHashMap<String, Object>() {

        {
            put("buzz", 1.2D);
            put("foo", "bar");
        }
    });
    final String expectedJson = "{'buzz':1.2,'foo':'bar'}".replace('\'', '"');
    final String actualJson = withLockedWriterReturning(writer -> writer.use(() -> writer.writeObject(map)));
    Assertions.assertThat(actualJson).isEqualTo(expectedJson);
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) Test(org.junit.jupiter.api.Test)

Aggregations

IndexedReadOnlyStringMap (org.apache.logging.log4j.util.IndexedReadOnlyStringMap)8 Collection (java.util.Collection)3 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)2 Filter (org.apache.logging.log4j.core.Filter)2 LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Logger (org.apache.logging.log4j.Logger)1 ColumnMapping (org.apache.logging.log4j.core.appender.db.ColumnMapping)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)1 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)1 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)1 StringBuilderFormattable (org.apache.logging.log4j.util.StringBuilderFormattable)1 StringMap (org.apache.logging.log4j.util.StringMap)1