eu.mais_h.mathsync.serialize.StringDeserializer Maven / Gradle / Ivy
package eu.mais_h.mathsync.serialize;
import java.io.UnsupportedEncodingException;
import eu.mais_h.mathsync.util.Function;
/**
* Deserializer going through a string representation of items.
*
* {@link StringSerializer} should be used as the corresponding {@link Serializer}.
*/
public class StringDeserializer implements Deserializer {
private Function toObject;
private StringDeserializer(Function toObject) {
this.toObject = toObject;
}
@Override
public T deserialize(byte[] item) {
String stringified;
try {
stringified = new String(item, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError("JVM does not support UTF-8 encoding");
}
T result = toObject.apply(stringified);
return result;
}
/**
* Retrieves an instance of this deserializer.
*
* @param toObject the function to convert items from the intermediate string to an actual object.
* @return an instance of this deserializer kind.
*/
public static Deserializer create(Function toObject) {
return new StringDeserializer<>(toObject);
}
public static Deserializer create() {
return create(new Function() {
@Override
public String apply(String t) {
return t;
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy