Custom Kryo Serialize and Deserialize

738 views
Skip to first unread message

Umamaheswari G

unread,
Jun 10, 2015, 1:37:48 PM6/10/15
to kryo-...@googlegroups.com
Hi, I am absolute new bee to Kryo Serialization. I wanted to custom serialize and de-serialize my object. I cannot find relevant example. I have developed some code, but it throws exception.  


 Exception in thread "main" java.lang.NoClassDefFoundError: org/objenesis/strategy/InstantiatorStrategy at KryoSample.main(KryoSample.java:63) Caused by: java.lang.ClassNotFoundException



import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoSerializable;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.MapSerializer;

public class KryoSample implements KryoSerializable{


int number = 12345;
String name = "Siemens";
boolean good = false;

String type = "Serializer ";
public void someValues(){      
   
String name = " Kryo ";
   
String type = "Serializer ";
}

public KryoSample(){}

public KryoSample(String name, String type){

     name
= " Kryo ";
     type
= "Serializer ";

}


@Override
public void read(Kryo kryo, Input input) {
   
// TODO Auto-generated method stub      
   
try{

   
String name  = input.readString();
   
int number = input.readInt();
   
boolean good = input.readBoolean();

   
System.out.println(name+": "+number+" : "+good);

   
}catch(Exception e){
       
System.out.println(" Read Exception  "+e.getMessage());
   
}      
}


@Override
public void write(Kryo kryo, Output output) {
   
// TODO Auto-generated method stub      
   
try{
    output
.writeString(name);
    output
.writeBoolean(good);
    output
.write(number);
   
}catch(Exception e){
       
System.out.println("  Write Exception "+e.getMessage());
   
}

}
public static void main(String args[]){
   
try{
   
Kryo kryoObj = null;
   
//kryoObj = new Kryo();
    kryoObj
.setReferences(false);
    kryoObj
.register(KryoSample.class, new MapSerializer());

   
System.out.println(" TRY: ");

   
//Kryo kryoObj = new Kryo();
   
Output outputObj = new Output();        
   
Input inputObj = new Input();      
   
KryoSample kryoSample = new KryoSample();

    kryoSample
.write(kryoObj, outputObj);
    kryoSample
.read(kryoObj, inputObj);
   
}catch(Exception e){
       
System.out.println("Kryo Exeption "+e.getMessage());
       
}
}

..
Please help me correct it and provide some samples if you have any.

Thanks in Advance.
Uma
Reply all
Reply to author
Forward
0 new messages