是使用protostuff将java.util.Map序列化为字节数组的任何方法,我看到protostuff-collectionsschema.jar文件中有一个MapSchema,但不知道如何使用它。 任何人都可以给我一些示例代码, 在此先感谢。如何通过protostuff将映射序列化到字节数组
1
A
回答
0
如果您需要序列化并反序列化Map
,那么您应该将其作为字段包装到包装类中(以创建模式)。
之后,您可以使用RuntimeSchema
将数据序列化/反序列化为二进制格式(或json,如果您想要人类可读的文本)。
public class Foo {
private Map<Integer, String> map;
序列化和反序列化的代码可能是这样的:
private final LinkedBuffer BUFFER = LinkedBuffer.allocate();
private final Schema<Foo> SCHEMA = RuntimeSchema.getSchema(Foo.class);
@Test
public void serializeAndDeserialize() throws Exception {
Foo foo = createFooInstance();
byte[] bytes = serialize(foo);
Foo x = deserialize(bytes);
Assert.assertEquals(foo, x);
}
private byte[] serialize(Foo foo) throws java.io.IOException {
return ProtobufIOUtil.toByteArray(foo, SCHEMA, BUFFER);
}
private Foo deserialize(byte[] bytes) {
Foo tmp = SCHEMA.newMessage();
ProtobufIOUtil.mergeFrom(bytes, tmp, SCHEMA);
return tmp;
}
完整的源代码,这个例子:RuntimeSchemaUsage.java
0
你可以在protostuff - 在测试代码中发现collectionschema。
public class StringMapSchema<V> extends MapSchema<String,V>
{
/**
* The schema for Map<String,String>
*/
public static final StringMapSchema<String> VALUE_STRING = new StringMapSchema<String>(null)
{
protected void putValueFrom(Input input, MapWrapper<String,String> wrapper,
String key) throws IOException
{
wrapper.put(key, input.readString());
}
protected void writeValueTo(Output output, int fieldNumber, String value,
boolean repeated) throws IOException
{
output.writeString(fieldNumber, value, repeated);
}
protected void transferValue(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, true, number, repeated);
}
};
/**
* The schema of the message value.
*/
public final Schema<V> vSchema;
/**
* The pipe schema of the message value.
*/
public final Pipe.Schema<V> vPipeSchema;
public StringMapSchema(Schema<V> vSchema)
{
this(vSchema, null);
}
public StringMapSchema(Schema<V> vSchema, Pipe.Schema<V> vPipeSchema)
{
this.vSchema = vSchema;
this.vPipeSchema = vPipeSchema;
}
protected final String readKeyFrom(Input input, MapWrapper<String,V> wrapper)
throws IOException
{
return input.readString();
}
protected void putValueFrom(Input input, MapWrapper<String,V> wrapper, String key)
throws IOException
{
wrapper.put(key, input.mergeObject(null, vSchema));
}
protected final void writeKeyTo(Output output, int fieldNumber, String value,
boolean repeated) throws IOException
{
output.writeString(fieldNumber, value, repeated);
}
protected void writeValueTo(Output output, int fieldNumber, V value,
boolean repeated) throws IOException
{
output.writeObject(fieldNumber, value, vSchema, repeated);
}
protected void transferKey(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, true, number, repeated);
}
protected void transferValue(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
if(vPipeSchema == null)
{
throw new RuntimeException("No pipe schema for value: " +
vSchema.typeClass().getName());
}
output.writeObject(number, pipe, vPipeSchema, repeated);
}
}
相关问题
- 1. 将键映射为字符串的序列化和反序列化映射
- 2. 映射数组节到新的数组
- 3. 序列化整数收集到字节数组并将其反序列化
- 4. Ruby - 将数组映射到散列图
- 5. 如何将JSON数组映射/反序列化为POJO或POJO列表?
- 6. 如何反序列化简单框架中的数组映射?
- 7. 将唯一组合映射到数字
- 8. 如何通过Java方法序列映射到执行
- 9. 休眠如何将字符串字段映射到数字列
- 10. 将映射列表转换为映射到原始数组
- 11. 如何将数字映射到颜色?
- 12. 序列化数组和映射到斯卡拉json
- 13. 斯卡拉+播放:序列化映射到JSON数组
- 14. 如何反序列化映射对象
- 15. 通过Mmap将数组映射到文件中去
- 16. 将文字地址映射到节
- 17. 如何通过ObservableCollections映射字符串
- 18. 如何迭代通过哈希映射映射列表元素
- 19. 将java映射序列化为db
- 20. 将数组映射到数组C++
- 21. 如何在休眠中将字符串映射到DB序列
- 22. 如何序列化/反序列化映射
- 23. 通过流将映射列表转换为单个映射
- 24. JAXB - 如何将xml数组映射到对象列表
- 25. 如何将内存映射到散列映射到文件
- 26. Jerkson。序列化映射到JsValue
- 27. Java序列化映射到文件
- 28. @ManyToOne hibernate映射序列化
- 29. 通过二维数组映射
- 30. React - 通过对象数组映射