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