概述
- 专门针对 Java 语言的:Kryo,FST 等
- 跨语言的:Protostuff,ProtoBuf,Thrift,Avro,MsgPack 等
kryo
kryo 是一个 java 序列化库。
入门示例
依赖
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>4.0.1</version>
</dependency>
demo 序列化到文件
public static void main(String[] args) throws FileNotFoundException {
Kryo kryo = new Kryo();
Output output = new Output(new FileOutputStream("/tmp/output.dat"));
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", 1);
map.put("b", 2);
kryo.writeObject(output, map);
output.close();
Input input = new Input(new FileInputStream("/tmp/output.dat"));
Map<String, Object> newmap = kryo.readObject(input, HashMap.class);
input.close();
System.out.println(newmap);
}
demo 序列化到 bytes
public static void main(String[] args) throws IOException {
Kryo kryo = new Kryo();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", 1);
map.put("b", 2);
kryo.writeObject(output, map);
output.close();
baos.flush();
byte[] bytes = baos.toByteArray();
Input input = new Input(new ByteArrayInputStream(bytes));
Map<String, Object> newmap = kryo.readObject(input, HashMap.class);
input.close();
System.out.println(newmap);
}
fst
依赖
<dependency>
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>
<version>2.57</version>
</dependency>
demo 序列化到 bytes
public static void main(String[] args) {
FSTConfiguration fst = FSTConfiguration.createDefaultConfiguration();
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", 1);
map.put("b", 2);
byte[] bytes = fst.asByteArray(map);
System.out.println(bytes.length);
Map<String, Object> newmap = (Map) fst.asObject(bytes);
System.out.println(newmap);
}
Protostuff
依赖
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.6.0</version>
</dependency>
demo 序列化到 bytes
public static class Bean {
private String a;
private int b;
public String getA() {
return a;
}
public int getB() {
return b;
}
public void setA(String a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
}
public static <T> byte[] serializer(T o) {
Schema schema = RuntimeSchema.getSchema(o.getClass());
return ProtobufIOUtil.toByteArray(o, schema, LinkedBuffer.allocate(256));
}
public static <T> T deserializer(byte[] bytes, Class<T> clazz) {
T obj = null;
try {
obj = clazz.newInstance();
Schema schema = RuntimeSchema.getSchema(obj.getClass());
ProtostuffIOUtil.mergeFrom(bytes, obj, schema);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
public static void main(String[] args) {
Bean bean = new Bean();
bean.setA("1");
bean.setB(2);
byte[] bytes = serializer(bean);
System.out.println(bytes.length);
Bean newmap = deserializer(bytes, Bean.class);
System.out.println(newmap);
}
第三方测试结果
fst 是完全兼容 JDK 序列化协议的系列化框架,序列化速度大概是 JDK 的 4-10 倍,大小是 JDK 大小的 1/3 左右。
- 序列化框架性能对比(kryo、hessian、java、protostuff)
- 高性能序列化框架 FST: FST 性能更好,但是体积比 kryo 大
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于