序列化容量,jdk pb json kryo
package serial; import com.alibaba.fastjson.JSONObject; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.UnsupportedEncodingException; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Output; import io.protostuff.LinkedBuffer; import io.protostuff.ProtobufIOUtil; import io.protostuff.Schema; import io.protostuff.runtime.RuntimeSchema; /** * https://www.cnblogs.com/silyvin/articles/11724664.html * protoc-3.6.1-osx-x86_64/bin/protoc --proto_path ./ --java_out ./ msg.proto * Created by sunyuming on 19/8/7. */ public class SerialMain { public static void main(String []f ) throws UnsupportedEncodingException { // pb MyBaseProto.BaseProto.Builder builder = MyBaseProto.BaseProto.newBuilder(); builder.setCode(1); builder.setMsg("test"); byte [] pb = builder.build().toByteArray(); // System.out.println(builder.build().toString()); System.out.println(pb.length); // jdk MyBaseBean myBaseBean = new MyBaseBean(); byte[] bytes = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(myBaseBean); oos.flush(); bytes = bos.toByteArray (); oos.close(); bos.close(); } catch (Exception ex) { ex.printStackTrace(); } System.out.println(bytes.length); // json String json = JSONObject.toJSONString(myBaseBean); // System.out.println(json); System.out.println(json.getBytes("utf-8").length); // protostuff Schema schema = RuntimeSchema.getSchema(myBaseBean.getClass()); byte [] protostuffBytes = ProtobufIOUtil.toByteArray(myBaseBean, schema, LinkedBuffer.allocate(256)); System.out.println(protostuffBytes.length); // kyo ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); Kryo kryo = new Kryo(); kryo.setReferences(false); kryo.setRegistrationRequired(false); kryo.register(MyBaseBean.class); Output output = new Output(bos2); kryo.writeObject(output, myBaseBean); output.flush(); output.close(); try { bytes = bos2.toByteArray (); bos2.close(); } catch (Exception ex) { ex.printStackTrace(); } System.out.println(bytes.length); } }
pb 8
jdk 83
json 23
protostaff 8
kryo 5