二、JSON 序列化
User user = new User(); user.setUserId(10001); user.setUserName("序列化first"); user.setUserBirth(new Date()); byte[] bytes = null; long start = System.currentTimeMillis(); // 序列化 ObjectMapper objectMapper = new ObjectMapper(); for (int i = 0 ; i< 10000 ;i++){ bytes = objectMapper.writeValueAsBytes(user); } long end = System.currentTimeMillis(); System.out.println("消耗时间:"+ (end - start) +"ms" ); System.out.println(bytes.length); // 反序列化 User user1 = objectMapper.readValue(bytes, User.class); System.out.println(user1);
三、Gson 序列化
User user = new User(); user.setUserId(10001); user.setUserName("序列化first"); user.setUserBirth(new Date()); long start = System.currentTimeMillis(); String text = ""; Gson gson = new Gson(); // 序列化 for (int i = 0 ; i< 10000 ;i++){ text = gson.toJson(user); } long end = System.currentTimeMillis(); System.out.println("消耗时间:"+ (end - start) +"ms" ); System.out.println(text.getBytes().length); // 反序列化 User user1 = gson.fromJson(text, User.class); System.out.println(user1);
四、Fastjson 序列化
User user = new User(); user.setUserId(10001); user.setUserName("序列化first"); user.setUserBirth(new Date()); byte[] bytes = null; long start = System.currentTimeMillis(); // 序列化 String text = null; for (int i = 0 ; i< 10000 ;i++){ text = JSON.toJSONString(user); } long end = System.currentTimeMillis(); System.out.println("消耗时间:"+ (end - start) +"ms" ); System.out.println(text.getBytes().length); // 反序列化 User user1 = JSON.parseObject(text, User.class); System.out.println(user1);
五、百度 ProtoBuff 序列化
注意:使用这个的时候 user 实体类需要加上 @Protobuff
User user = new User(); user.setUserId(10001); user.setUserName("序列化first"); user.setUserBirth(new Date()); long start = System.currentTimeMillis(); byte[] encode = null; Gson gson = new Gson(); Codec<User> userCodec = ProtobufProxy.create(User.class); // 序列化 for (int i = 0 ; i< 10000 ;i++){ encode = userCodec.encode(user); } long end = System.currentTimeMillis(); System.out.println("消耗时间:"+ (end - start) +"ms" ); System.out.println(encode.length); // 反序列化 User decode = userCodec.decode(encode); System.out.println(decode);
六、Kryo 序列化
注意: 原来的旧版本不需要,但是新版本 需要加上 这行进行注册, kryo.register(User.class, new JavaSerializer());
不然会有报错信息
java.lang.IllegalArgumentException: Class is not registered: org.example.nativenal.domian.User
Note: To register this class use: kryo.register(org.example.nativenal.domian.User.class);
User user = new User(); user.setUserId(10001); user.setUserName("序列化first"); // user.setUserBirth(new Date()); long start = System.currentTimeMillis(); Kryo kryo = new Kryo(); kryo.register(User.class, new JavaSerializer()); Output output = null; output = new Output(new FileOutputStream("kryo")); kryo.writeObject(output, user); long end = System.currentTimeMillis(); System.out.println("消耗时间:"+ (end - start) +"ms" ); System.out.println(new File("kryo").length()); // 反序列化 Input Input = new Input(new FileInputStream("kryo")); User user1 = kryo.readObject(Input, User.class); System.out.println(user1);
在网络传输中文件越小传输的速度越快。
总结