是使用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 - 通過對象數組映射