扩展序列化算法
配置文件 config.properties
serialize.type=JSON
工具类
public class Config {
private static final Properties PROPERTIES = new Properties();
static {
try (InputStream input = Config.class.getResourceAsStream("/config.properties")) {
PROPERTIES.load(input);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Serialize getConfigSerialize() {
String type = PROPERTIES.getProperty("serialize.type");
if (type == null || "JAVA".equalsIgnoreCase(type)) {
return Serialize.JAVA;
} else if ("JSON".equalsIgnoreCase(type)) {
return Serialize.JSON;
}
throw new RuntimeException("UnKnown type: " + type);
}
}
接口
public interface Serialize {
<T> T deSerialize(Class<T> clazz, byte[] bytes);
<T> byte[] serialize(T object);
byte getOrdinary();
JavaSerialize JAVA = new JavaSerialize();
JsonSerialize JSON = new JsonSerialize();
Serialize[] SERIALIZES = {JAVA, JSON};
}
实现类
public class JavaSerialize implements Serialize {
protected JavaSerialize() {}
@Override
public <