Best-performing serialization deserialization, PROTOBUF usage (MAVEN project)

Source: Internet
Author: User
Tags serialization

First, add the Protobuf jar package to the Pom.xml .

     <dependency>
          <groupId>com.dyuproject.protostuff</groupId>
          <artifactId> protostuff-core</artifactid>
          <version>1.0.8</version>
      </dependency>

      < dependency>
          <groupId>com.dyuproject.protostuff</groupId>
          <artifactId> protostuff-runtime</artifactid>
          <version>1.0.8</version>
      </dependency>

serialized entity Class User.java (no need to implement Serializable interface)

Package com.serialize.entity;

public class User {
	private String ID;
	Private String username;
	private String password;
	
	Public user () {
	} public
	User (string ID, string username, string password) {
		this.id = ID;
		This.username = Username;
		This.password = password;
	}
	Public String getId () {
		return ID;
	}
	public void SetId (String id) {
		this.id = ID;
	}
	Public String GetUserName () {
		return username;
	}
	public void Setusername (String username) {
		this.username = username;
	}
	Public String GetPassword () {
		return password;
	}
	public void SetPassword (String password) {
		this.password = password;
	}
	@Override public
	String toString () {
		return "User [id=" + ID + ", username=" + Username + ", password="
				+ P Assword + "]";
	}
	

}
serialized Tool class Serializeutils.java (using a generic method)

Package com.serialize.utils;

Import Com.dyuproject.protostuff.LinkedBuffer;
Import Com.dyuproject.protostuff.ProtobufIOUtil;
Import Com.dyuproject.protostuff.runtime.RuntimeSchema;

public class serializeutils{public
	static <T> byte[] Serialize (T t,class<t> clazz) {
		return Protobufioutil.tobytearray (t, Runtimeschema.createfrom (clazz),
				linkedbuffer.allocate (LinkedBuffer.DEFAULT_ buffer_size));
	}
	public static <T> T deserialize (byte[] data,class<t> clazz) {
		runtimeschema<t> Runtimeschema = Runtimeschema.createfrom (clazz);
		T t = runtimeschema.newmessage ();
		Protobufioutil.mergefrom (data, T, Runtimeschema);
		return t;
	}
}
Test main class App.java

Package com.serialize;

Import Com.serialize.entity.User;
Import Com.serialize.utils.SerializeUtils;

public class App {public
	static void Main (string[] args) {	
		user user=new User ("1", "Xiaobao", "123456");
		System.out.println ("serialization");
		byte[] data = Serializeutils.serialize (User,user.class);
		for (byte b:data) {
			System.out.print (b);
		}
		System.out.println ();
		System.out.println ("deserialization");
		User user2 = serializeutils.deserialize (data,user.class);
		System.out.println (User2);
	}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.