inconsistent between list serialization and deserialization by using protostuff

67 vistas
Ir al primer mensaje no leído

musicaud...@gmail.com

no leída,
8 ene 2018, 4:06:00 a.m.8/1/18
para protostuff
When i use protostuff to deserialize a list object, it seems more items are added into the list. I write a test code for this issue, could any one help? thanks. 

this is a running result snapshot of below code:



import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisStd;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Test {
   
public static void main(String[] args) {
       
testMethod_01();
       
testMethod_02();
   
}

   
// the method seems ok.
    private static void testMethod_01(){
        B b
= new B();

       
System.out.println("testMethod_02: before  serialize");
       
for(String item : b.list()){
           
System.out.println(item);
       
}

       
byte[] test=SerializationUtil.serialize(b);
        B newB
=SerializationUtil.deserialize(test, B.class);

       
System.out.println("testMethod_02: after  serialize");
       
for(String item : newB.list()){
           
System.out.println(item);
       
}

       
System.out.println();
   
}

   
// the method makes me frustrated, object does not keep consistent after deserialization.
    private static void testMethod_02(){
        A a
=new A(new B());

       
System.out.println("testMethod_01: before  serialize");

       
for(String item : a.get().list()){
           
System.out.println(item);
       
}

       
byte[] test=SerializationUtil.serialize(a);

        A newA
=SerializationUtil.deserialize(test, A.class);

       
System.out.println("testMethod_01: after  serialize");
       
for(String item : newA.get().list()){
           
System.out.println(item);
       
}

       
System.out.println();
   
}

   
public static class A{
        B
obj;

       
public A(B obj) {
           
this.obj = obj;
       
}

       
public B get() {
           
return obj;
       
}
   
}

   
public static class B {
       
private List<String> list;

       
public B() {
           
list = new ArrayList<String>();
           
list.add("item in list");
       
}

       
public List<String> list() {
           
return list;
       
}
   
}

   
/**
     * protostuf serializaion util.
     */
    public static class SerializationUtil {
       
private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
       
private static Objenesis objenesis = new ObjenesisStd(true);

       
private static <T> Schema<T> getSchema(Class<T> cls) {
           
Schema<T> schema = (Schema<T>) cachedSchema.get(cls);
           
if (schema == null) {
                schema
= RuntimeSchema.createFrom(cls);
               
if (schema != null) {
                   
cachedSchema.put(cls, schema);
               
}
           
}
           
return schema;
       
}

       
public static <T> byte[] serialize(T obj) {
           
Class<T> cls = (Class<T>) obj.getClass();
           
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
           
try {
               
Schema<T> schema = getSchema(cls);
               
return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
           
} catch (Exception e) {
               
throw new IllegalStateException(e.getMessage(), e);
           
} finally {
                buffer
.clear();
           
}
       
}

       
public static <T> T deserialize(byte[] data, Class<T> cls) {
           
try {
               
T message = (T) objenesis.newInstance(cls);
               
Schema<T> schema = getSchema(cls);
               
ProtostuffIOUtil.mergeFrom(data, message, schema);
               
return message;
           
} catch (Exception e) {
               
throw new IllegalStateException(e.getMessage(), e);
           
}
       
}
   
}
}

David Yu

no leída,
8 ene 2018, 4:11:08 a.m.8/1/18
para protostuff
1.  Do not use objenesis.
2. Set -Dprotostuff.runtime.always_use_sun_reflection_factory=true

If you do those two, B's constructor that adds an item to the list does not get called.

--
You received this message because you are subscribed to the Google Groups "protostuff" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protostuff+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
When the cat is away, the mouse is alone.
dyuproject.com
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos