<!-- pom依赖 2020-8-13最新版本 -->
<!-- https://mvnrepository.com/artifact/com.dyuproject.protostuff/protostuff-core -->
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.dyuproject.protostuff/protostuff-runtime -->
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.1.6</version>
</dependency>
<!--lomboc-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
简单demo
package test;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
public class ProtoStuffTest {
public static void main(String[] args) {
RuntimeSchema<Bean> schema = RuntimeSchema.createFrom(Bean.class);
//构建测试数据
Bean child = new Bean("child1", null,null),
child2 = new Bean("child2", null,null),
child3 = new Bean("child3", null,null),
message = new Bean("parent", null,null);
List<Bean> children = Stream.of(child,child2,child3).collect(Collectors.toList());
message.setChildren(children);
message.setMap(children.stream().collect(Collectors.toMap(Bean::getName, i->i)));
//序列化
byte[] data=ProtostuffIOUtil.toByteArray(message, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
//反序列化
Bean test = new Bean();
ProtostuffIOUtil.mergeFrom(data, test, schema);
System.out.println(test);
System.out.println(test.equals(message));//结果:false
System.out.println(test.toString().equals(message.toString()));//结果:true
}
/**
* 序列化测试类
**/
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
static class Bean{
private String name;
private List<Bean> children;
private Map<String,Bean> map;
}
}