protostuff java_protostuff 及其注意事项

google开发的开源的序列化方案protocol buffer(简称protobuf),它的好处很多,独立于语言,独立于平台,最最重要的是它的效率相当高,用protobuf序列化后的大小是json的10分之一,xml格式的20分之一,是二进制序列化的10分之一。

protostuff是一个基于protobuf实现的序列化方法,它较于protobuf最明显的好处是,在几乎不损耗性能的情况下做到了不用我们写.proto文件来实现序列化

io.protostuff

protostuff-runtime

1.3.8

io.protostuff

protostuff-core

1.3.8

public class ProtolStuffSerializeDemo {

public static void main(String[] args) {

Divide divide = new Divide();

System.err.println(divide);

Schema divideSchema = RuntimeSchema.getSchema(Divide.class);

LinkedBuffer buffer = LinkedBuffer.allocate(512);

ByteArrayOutputStream byteArrayOutputStream;

byte[] bytes = null;

try {

byteArrayOutputStream = new ByteArrayOutputStream();

ProtostuffIOUtil.writeTo(byteArrayOutputStream, divide, divideSchema, buffer);

bytes = byteArrayOutputStream.toByteArray();

System.err.println(String.format("length : %d value %s" ,bytes.length , new String(bytes)));

for(byte b: bytes) {

System.err.print("[" + (char)b + "]");

System.err.print(b + " ");

}

System.err.println();

} catch (IOException e) {

e.printStackTrace();

}

Lily lily = new Lily();

ProtostuffIOUtil.mergeFrom(bytes, lily, divideSchema);

System.err.println(lily);

}

}

@Data

class Divide {

String name = "divide";

String age = "a";

String age1 = "b";

String age2 = "c";

String age3 = "d";

String age4 = "e";

}

@Data

class Lily {

int age;

String name;

}

「招俊坚:「张兴业:cellar 序列化使用了 protostuff,总结了一下需要注意的事项:

// 不允许以下行为出现:

// 不允许用泛型 java.lang.Object

// 不允许继承接口、继承类 interfaces

// 不允许使用数组 arrays

// 不允许使用枚举 enum

// 不允许使用泛型的集合。比如:允许 List 但是不允许 List

// 不允许使用两层以上容器这种复杂结构。比如:不允许 List

// ==========

// (1)字段使用 @Tag 注解。表示第几个字段。值不能重复。

// (2)不能删除字段。使用 @Deprecated 注解代替。

// (3)不序列化某个字段。使用 transient java 关键字声明。」

——————————

Cellar的protostuff的@Tag是无效的」

——————————

嗯,我们也是自己用新版的 protostuff 序列化的。直接用 cellar 的 byte[] 接口

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值