码迷,mamicode.com
首页 > 其他好文 > 详细

关于数据序列化(1)protobuf 示例

时间:2015-04-01 00:30:16      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

最近用protobuf来做对象的序列化(不知道我这么表达对不对),用的是百度的jprotobuf,使用注解,当然还有不使用注解的经典方式,本人没用过,发现工作中的竟然在JDK7报错(真怀疑为啥生产用JDK6,现在JDK8了都)只好在github下了最新的,当然google的jar包仍然是必须的



不过有个疑问,protobuf的好处仅仅是为了提供一个兼容跨平台的格式吗,既然最后都是讲对象存为byte[],那直接用流操作使用writeByte(),writeInt(),writeUTf(),写进byte[]里存入文件或数据库有区别么?什么情况下使用protobuf时最佳应用场景?客户端服务器通信?还是其他?

------------------------------------------------------依赖的包---------------------------------------------------------------

jprotobuf-1.2.9.jar

地址点击打开链接

protobuf-0.0.3-beta1.jar
protobuf-java-2.6.1.jar
jprotobuf-1.2.9.jar

---------------------------------------------------------------------------------------------------------------------

package a_serial.protobuf;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.baidu.bjf.remoting.protobuf.FieldType;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;



public class GoodsRecord {

	@Protobuf(fieldType = FieldType.INT32, order = 1)
	private int goodsId;
	@Protobuf(fieldType = FieldType.INT32, order = 2)
	private int num;
	
	public GoodsRecord(){
		super();
	}
	
	public GoodsRecord(int goodsId, int num) {
		super();
		this.goodsId = goodsId;
		this.num = num;
	}
	/*
	 * getters and setters
	 */
	public int getGoodsId() {
		return goodsId;
	}
	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}

-----------------------------------------------------------------------------

package a_serial.protobuf;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.FieldType;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;


public class Shop{

	@Protobuf(fieldType = FieldType.STRING, order = 1)
	private String shopId;
	private byte[] data;
	
	@Protobuf(fieldType = FieldType.OBJECT, order = 2)
	private List<GoodsRecord> goodsRecordList;

	public Shop(){
		super();
	}
	
	public byte[] getData(){
		return data;
	}
	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public List<GoodsRecord> getGoodsRecordList() {
		return goodsRecordList;
	}

	public void setGoodsRecordList(List<GoodsRecord> goodsRecordList) {
		this.goodsRecordList = goodsRecordList;
	}

	private Shop parseData(byte[] targetData)
			throws IOException {
		Codec<Shop> codec = ProtobufProxy.create(Shop.class);
		return codec.decode(targetData) ;
	}

	private byte[] buildData() {
		try {
			Codec<Shop> codec = ProtobufProxy.create(Shop.class);
			return codec.encode(this);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public void preToData() {
		this.data = buildData();
	}

	public Shop postFromData() {
		Shop s = null;
		try {
			s = parseData(this.data);
			this.shopId = s.getShopId();
			this.goodsRecordList = s.getGoodsRecordList();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return s;
	}
	public Shop initFromData(byte[] data) {
		Shop s = null;
		try {
			s = parseData(data);
			this.shopId = s.getShopId();
			this.goodsRecordList = s.getGoodsRecordList();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return s;
	}
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
-------------------------------------------------------------------------------------------------

package a_serial.protobuf;

import java.io.IOException;
import java.util.ArrayList;

import a_serial.ByteUtil;

import com.google.common.collect.Lists;
/**
 * 写
 */
public class ProtoBufTest {

	public static void main(String[] args) {
		Shop shop = new Shop();
		shop.setShopId("10010");
		GoodsRecord r = new GoodsRecord(3, 22);
		GoodsRecord r2 = new GoodsRecord(4, 22);
		ArrayList<GoodsRecord> goodsRecordList = Lists.newArrayList(r, r2);
		shop.setGoodsRecordList(goodsRecordList);
		
		shop.preToData();
		byte[] data = shop.getData();
		try {
			ByteUtil.writeFile(data, "protoBuf.os");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

--------------------------------------------------------------------------------

package a_serial.protobuf;

import a_serial.ByteUtil;
/**
 * 这个是为了读
 */
public class ProtoBufTest2 {

	public static void main(String[] args) {
		Shop shop = new Shop();
		try {
			byte[] data = ByteUtil.getData("protoBuf.os");
			shop.initFromData(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(shop);
	}

}



关于数据序列化(1)protobuf 示例

标签:

原文地址:http://blog.csdn.net/kkgbn/article/details/44786693

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!