1

是什么方法在使用protostuff时将java.util.Map序列化为字节数组,我看到protostuff-collectionsschema.jar文件中有一个MapSchema,但是不知道怎么用。谁能给我一些示例代码,在此先感谢。

4

2 回答 2

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);
    }

}
于 2016-04-03T07:12:24.003 回答
0

如果您需要序列化和反序列化Map,则应将其作为字段包装到包装类中(以创建模式)。

之后,您可以使用RuntimeSchema.

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

于 2015-11-02T22:09:26.207 回答