1

is any method to serialize java.util.Map to byte array when using protostuff, I saw there is a MapSchema in the protostuff-collectionsschema.jar file, but don't know how to use it. can anyone give me some sample code, thanks in advance.

eonezhang
  • 11
  • 4

2 Answers2

0

If you need to serialize and deserialize Map, then you should wrap it as a field into a wrapper class (to create schema).

After that you can serialize/deserialize data to a binary format (or json, if you want human-readable text) with RuntimeSchema.

public class Foo {
    private Map<Integer, String> map;

Serialization and deserialization code might look like this:

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

Full source code for this example: RuntimeSchemaUsage.java

Kostiantyn
  • 935
  • 8
  • 13
  • Why is the example using ProtobufIOUtil instead of ProtostuffIOUtil? – sema Mar 18 '18 at 10:18
  • 1
    There are few differences - they generate different bytecode, and ProtostuffIOUtil supports cyclic references. But in this example both can be used in the same way. – Kostiantyn Mar 19 '18 at 14:13
0

You can be in protostuff - collectionschema found in the test code.

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

}
Jamie
  • 1,096
  • 2
  • 19
  • 38