这是一个Kryo将复杂Bean对象序列化与反序列化,通过字符串传递的列子。
具体的bean对象此处没有附加代码,可以自己写一个bean的例子,里面可包含任何对象,集合,基本类型。但要求有geter和setter方法。
本人在本地测试类以下,除了Map需要Setter方法外,其他属性没有setter方法也可序列化与反序列化。
这个例子中使用到了 Class BeanSerializer, 以下是这个类的介绍:
Serializes Java beans using bean accessor methods.
Only bean properties with both a getter and setter are serialized
.
This class is not as fast as FieldSerializer but is much faster and more efficient than Java serialization. Bytecode generation is used to invoke the bean propert methods, if possible.
BeanSerializer does not write header data, only the object data is stored. If the type of a bean property is not final (note primitives are final) then an extra byte is written for that property.
TaskResult的类:
TaskItemResult类:
package bhz.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import bhz.test.task.ExecItemResultIPMI;
import bhz.test.task.IExecItemResult;
import bhz.test.task.ITaskResult;
import bhz.test.task.TaskItemResult;
import bhz.test.task.TaskResult;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.BeanSerializer;
import com.esotericsoftware.kryo.serializers.JavaSerializer;
import com.esotericsoftware.kryo.serializers.MapSerializer;
import com.po.Animal;
import com.po.Dog;
import com.veraxsystems.vxipmi.coding.commands.ResponseData;
import com.veraxsystems.vxipmi.coding.commands.sdr.GetSdrResponseData;
public class KyroSerializableOne {
public static void main(String[] args) throws IOException {
/*
long start = System.currentTimeMillis();
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("zhang0", 2);
map.put("zhang1", 3);
Simple simple = new Simple("zhang" + 2, 2, map);
//System.out.println(simple.getAge() + " " + simple.getName() + " "
// + simple.getMap().toString());
ExecItemResultIPMI ipmiResult = new ExecItemResultIPMI();
Dog dog = (Dog)simple.getDog();
dog.getExecItemResult().add(ipmiResult);
System.out.println("Kryo 序列化时间:" + (System.currentTimeMillis() - start)
+ " ms");
start = System.currentTimeMillis();
Dog dogSerial = (Dog)simple.getDog();
IExecItemResult iExecItemResult = dogSerial.getExecItemResult().get(0);
ExecItemResultIPMI ipmiResultSerial = (ExecItemResultIPMI)iExecItemResult;
GetSdrResponseData getSdrResponseData = (GetSdrResponseData)ipmiResultSerial.getResponseData();
//序列化
String data= setSerializableObject(simple,Simple.class);
//反序列化
Simple simple1 = (Simple)getSerializableObject(Simple.class,data);
System.out.println(simple1.getAge() + " " + simple1.getName() + " "
+ simple1.getMap().toString()+" "+simple1.getDog().getName()+" ipmiResultSerial :"+ipmiResultSerial.getResponseData()+
" getSdrResponseData:" +new String(getSdrResponseData.getSensorRecordData()));
System.out.println("Kryo 反序列化时间:"
+ (System.currentTimeMillis() - start) + " ms");
*/
/*GetSdrResponseData getSdrResponseData = new GetSdrResponseData();
getSdrResponseData.setNextRecordId(111);
getSdrResponseData.setSensorRecordData("getSdrResponseData".getBytes());
ResponseData responseData = getSdrResponseData;
*/
TaskResult taskResult = new TaskResult();
Map<Integer, TaskItemResult> taskItemResult = taskResult.getTaskItemResult();
TaskItemResult itemResult = new TaskItemResult();
List<IExecItemResult> execItemResult = itemResult.getExecItemResult();
ExecItemResultIPMI ipmiResult = new ExecItemResultIPMI();
execItemResult.add(ipmiResult);
taskItemResult.put(111, itemResult);
System.out.println(taskResult);
//ITaskResult result = taskResult;
//System.out.println(result.getTaskItemResult());
String setSerializableObject = setSerializableObject(taskResult,TaskResult.class);
TaskResult resultObject = (TaskResult)getSerializableObject(TaskResult.class, setSerializableObject);
Map<Integer, TaskItemResult> taskItemResult2 = resultObject.getTaskItemResult();
System.out.println(taskItemResult2);
}
public static <T> String setSerializableObject(Object object,Class<T> clazz ) throws FileNotFoundException {
Kryo kryo = new Kryo();
/*MapSerializer serializer = new MapSerializer();
serializer.setKeyClass(Integer.class, new JavaSerializer());
serializer.setKeysCanBeNull(false);
serializer.setValueClass(TaskItemResult.class, new BeanSerializer(kryo, TaskItemResult.class));
serializer.setValuesCanBeNull(true);
kryo.register(HashMap.class, serializer);*/
kryo.register(clazz, new BeanSerializer(kryo, clazz));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo.writeObject(output, object);
output.flush();
output.close();
byte[] bys = baos.toByteArray();
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
String string = new String(new Base64().encode(bys));
//getSerializableObject(string);
System.out.println(string);
return string;
}
public static <T> Object getSerializableObject(Class<T> clazz, String data) {
Kryo kryo = new Kryo();
/* MapSerializer serializer = new MapSerializer();
serializer.setKeyClass(Integer.class, new JavaSerializer());
serializer.setKeysCanBeNull(false);
serializer.setValueClass(TaskItemResult.class, new BeanSerializer(kryo, TaskItemResult.class));
serializer.setValuesCanBeNull(true);
kryo.register(HashMap.class, serializer);*/
kryo.register(clazz, new BeanSerializer(kryo, clazz));
ByteArrayInputStream bais = new ByteArrayInputStream(new Base64().decode(data.getBytes()));
Input input;
Object simple = null;
try {
input = new Input(bais);
input.close();
simple = kryo.readObject(input, clazz);
// if((simple=kryo.readObject(input, Simple.class)) != null){
//System.out.println(simple.getAge() + " " + simple.getName() + " "
// + simple.getMap().toString()+" "+simple.getDog().getName());
// }
} catch (Exception e) {
e.printStackTrace();
}
return simple;
}
}
更新:
以下是经过整理和运行验证过的方法:
将需要序列化的Bean对象 ArrayList、 HashMap 放入方法中序列化,然后在反序列化,强转成自己想要的类型即可。
以下是序列化反序列化的方法:
/**
* 序列化bean对象的方法
* @param object
* @param clazz
* @return
* @throws FileNotFoundException
*/
public static <T> String setSerializableObject(Object object,Class<T> clazz ) throws FileNotFoundException {
Kryo kryo = new Kryo();
kryo.register(clazz, new BeanSerializer<T>(kryo, clazz));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo.writeObject(output, object);
output.flush();
output.close();
byte[] bys = baos.toByteArray();
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
String string = new String(new Base64().encode(bys));
System.out.println(string);
return string;
}
/**
* 反序列化bean对象的方法
* @param clazz
* @param data
* @return
*/
public static <T> Object getSerializableObject(Class<T> clazz, String data) {
Kryo kryo = new Kryo();
kryo.register(clazz, new BeanSerializer<T>(kryo, clazz)); //bean
ByteArrayInputStream bais = new ByteArrayInputStream(new Base64().decode(data.getBytes()));
Input input;
Object simple = null;
try {
input = new Input(bais);
input.close();
simple = kryo.readObject(input, clazz); //bean
//simple = kryo.readObject(input, ArrayList.class, serializer);//list
} catch (Exception e) {
e.printStackTrace();
}
return simple;
}
/**
* 序列化arrayList的方法
* @param object
* @param clazz
* @return
* @throws FileNotFoundException
*/
public static <T> String setSerializableList(Object object,Class<T> clazz ) throws FileNotFoundException {
Kryo kryo = new Kryo();
CollectionSerializer serializer = new CollectionSerializer();
serializer.setElementClass(clazz, new BeanSerializer<T>(kryo, clazz));
serializer.setElementsCanBeNull(false);
kryo.register(clazz, new BeanSerializer<T>(kryo, clazz));
kryo.register(ArrayList.class, serializer);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo.writeObject(output, object);
output.flush();
output.close();
byte[] bys = baos.toByteArray();
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
String string = new String(new Base64().encode(bys));
System.out.println(string);
return string;
}
/**
* 反序列化ArrayList的方法
* @param clazz
* @param data
* @return
*/
public static <T> Object getSerializableList(Class<T> clazz, String data) {
Kryo kryo = new Kryo();
//List
CollectionSerializer serializer = new CollectionSerializer();
serializer.setElementClass(clazz, new BeanSerializer<T>(kryo, clazz));
serializer.setElementsCanBeNull(false);
kryo.register(clazz, new BeanSerializer<T>(kryo, clazz));
kryo.register(ArrayList.class, serializer);
ByteArrayInputStream bais = new ByteArrayInputStream(new Base64().decode(data.getBytes()));
Input input;
Object simple = null;
try {
input = new Input(bais);
input.close();
simple = kryo.readObject(input, ArrayList.class, serializer);//list
} catch (Exception e) {
e.printStackTrace();
}
return simple;
}
/**
* 序列化HashMap<String, clazz>的方法 key为 String类型 value类型为 clazz
* @param object
* @param clazz
* @return
* @throws FileNotFoundException
*/
public static <T> String setSerializableMap(Object object,Class<T> clazz ) throws FileNotFoundException {
Kryo kryo = new Kryo();
//设置序列化map的key和value类型
MapSerializer serializer = new MapSerializer();
serializer.setKeyClass(String.class, new JavaSerializer());
serializer.setKeysCanBeNull(false);
serializer.setValueClass(clazz, new BeanSerializer<T>(kryo, clazz));
serializer.setValuesCanBeNull(true);
//注册要序列化的对象
kryo.register(HashMap.class, serializer);
kryo.register(clazz, new BeanSerializer<T>(kryo, clazz));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo.writeObject(output, object);
output.flush();
output.close();
byte[] bys = baos.toByteArray();
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
String string = new String(new Base64().encode(bys));
System.out.println(string);
return string;
}
/**
* HashMap<String,clazz>类型的map反序列化。
* @param clazz
* @param data
* @return
*/
public static <T> Object getSerializableMap(Class<T> clazz, String data) {
Kryo kryo = new Kryo();
//Map
MapSerializer serializer = new MapSerializer();
serializer.setKeyClass(String.class, new JavaSerializer());
serializer.setKeysCanBeNull(false);
serializer.setValueClass(clazz, new BeanSerializer<T>(kryo, clazz));
serializer.setValuesCanBeNull(true);
kryo.register(HashMap.class, serializer);
kryo.register(clazz, new BeanSerializer<T>(kryo, clazz)); //bean
ByteArrayInputStream bais = new ByteArrayInputStream(new Base64().decode(data.getBytes()));
Input input;
Object simple = null;
try {
input = new Input(bais);
input.close();
simple = kryo.readObject(input, HashMap.class, serializer); //bean
} catch (Exception e) {
e.printStackTrace();
}
return simple;
}