2015-10-07 48 views

回答

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

}