in johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MappingGeneratorImpl.java [580:693]
private void writeArray(Class<?> type, Adapter itemConverter, String key, Object arrayValue, Collection<String> ignoredProperties, JsonPointerTracker jsonPointer) {
final int length = ArrayUtil.getArrayLength(arrayValue);
if (length == 0 && config.isSkipEmptyArray()) {
return;
}
if(config.isTreatByteArrayAsBase64() && (type == byte[].class /*|| type == Byte[].class*/)) {
String base64EncodedByteArray = Base64.getEncoder().encodeToString((byte[]) arrayValue);
if (key != null) {
generator.write(key, base64EncodedByteArray);
} else {
generator.write(base64EncodedByteArray);
}
return;
}
if(config.isTreatByteArrayAsBase64URL() && (type == byte[].class /*|| type == Byte[].class*/)) {
if (key != null) {
generator.write(key, Base64.getUrlEncoder().encodeToString((byte[]) arrayValue));
} else {
generator.write(Base64.getUrlEncoder().encodeToString((byte[]) arrayValue));
}
return;
}
if (key != null) {
generator.writeStartArray(key);
} else {
generator.writeStartArray();
}
// some specialised arrays to speed up conversion.
// Needed since Array.get is rather slow :(
if (type == byte[].class) {
byte[] tArrayValue = (byte[]) arrayValue;
for (int i = 0; i < length; i++) {
final byte o = tArrayValue[i];
generator.write(o);
}
} else if (type == short[].class) {
short[] tArrayValue = (short[]) arrayValue;
for (int i = 0; i < length; i++) {
final short o = tArrayValue[i];
generator.write(o);
}
} else if (type == int[].class) {
int[] tArrayValue = (int[]) arrayValue;
for (int i = 0; i < length; i++) {
final int o = tArrayValue[i];
generator.write(o);
}
} else if (type == long[].class) {
long[] tArrayValue = (long[]) arrayValue;
for (int i = 0; i < length; i++) {
final long o = tArrayValue[i];
generator.write(o);
}
} else if (type == float[].class) {
float[] tArrayValue = (float[]) arrayValue;
for (int i = 0; i < length; i++) {
final float o = tArrayValue[i];
generator.write(o);
}
} else if (type == double[].class) {
double[] tArrayValue = (double[]) arrayValue;
for (int i = 0; i < length; i++) {
final double o = tArrayValue[i];
generator.write(o);
}
} else if (type == char[].class) {
char[] tArrayValue = (char[]) arrayValue;
for (int i = 0; i < length; i++) {
final char o = tArrayValue[i];
generator.write(String.valueOf(o));
}
} else if (type == boolean[].class) {
boolean[] tArrayValue = (boolean[]) arrayValue;
for (int i = 0; i < length; i++) {
final boolean o = tArrayValue[i];
generator.write(o);
}
} else if (type == Byte[].class ||
type == Short[].class ||
type == Integer[].class ||
type == Long[].class ||
type == Float[].class ||
type == Double[].class ||
type == Character[].class ||
type == Boolean[].class) {
// Wrapper types do not not need deduplication
Object[] oArrayValue = (Object[]) arrayValue;
for (int i = 0; i < length; i++) {
final Object o = oArrayValue[i];
writeItem(itemConverter != null ? itemConverter.from(o) : o, ignoredProperties,
isDedup() ? new JsonPointerTracker(jsonPointer, i) : null);
}
} else {
// must be object arrays
for (int i = 0; i < length; i++) {
Object[] oArrayValue = (Object[]) arrayValue;
final Object o = oArrayValue[i];
String valJsonPointer = jsonPointers == null ? null : jsonPointers.get(o);
if (valJsonPointer != null) {
// write the JsonPointer as String natively
generator.write(valJsonPointer);
} else if (o instanceof JsonValue) {
generator.write((JsonValue) o);
} else {
writeItem(itemConverter != null ? itemConverter.from(o) : o, ignoredProperties,
isDedup() ? new JsonPointerTracker(jsonPointer, i) : null);
}
}
}
generator.writeEnd();
}