se.fortnox.reactivewizard.jaxrs.params.deserializing.ArrayDeserializer Maven / Gradle / Ivy
package se.fortnox.reactivewizard.jaxrs.params.deserializing;
import java.lang.reflect.Array;
/**
* Deserializes arrays.
*/
public class ArrayDeserializer implements Deserializer {
private final Deserializer inner;
private final Class arrayType;
public ArrayDeserializer(Deserializer inner, Class arrayType) {
this.inner = inner;
this.arrayType = arrayType;
}
@Override
public T[] deserialize(String value) throws DeserializerException {
if (value == null) {
return null;
}
String[] rawValues = value.split(",");
try {
T[] deserialized = (T[])Array.newInstance(arrayType, rawValues.length);
for (int i = 0; i < deserialized.length; i++) {
deserialized[i] = inner.deserialize(rawValues[i]);
}
return deserialized;
} catch (DeserializerException e) {
throw e;
} catch (Exception e) {
throw new DeserializerException("invalid.array");
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy