如何通过protostuff序列化映射到字节数组

时间:2015-10-07 13:49:18

标签: protostuff

是使用protostuff时将java.util.Map序列化为字节数组的任何方法, 我看到protostuff-collectionsschema.jar文件中有一个MapSchema,但不知道如何使用它。 任何人都可以给我一些示例代码, 提前谢谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);
    }

}