我有一个序列化的对象,需要转换为'blob'对象并存储到数据库中。以前我们存储的对象是由其他项目对象定义的,但不遵循序列化规则,因此遇到了许多问题,因此我们决定更改结构“blob”对象,它现在只包含基本对象(字符串、布尔值、整数等)。到目前为止,我们可以使所有属性都期望两个。
private byte[] encode(ScheduledReport schedSTDReport)
{
byte[] bytes = null;
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(schedSTDReport);
oos.flush();
oos.close();
bos.close();
//byte [] data = bos.toByteArray();
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//GZIPOutputStream out = new GZIPOutputStream(baos);
//XMLEncoder encoder = new XMLEncoder(out);
//encoder.writeObject(schedSTDReport);
//encoder.close();
bytes = bos.toByteArray();
//GZIPOutputStream out = new GZIPOutputStream(bos);
//out.write(bytes);
//bytes = bos.toByteArray();
}
Blob包含
public class ScheduledReport extends ScheduledReportInfo implements Serializable {
private SupervisoryScope _scope = null;
private Report _attributes = null;
private ScheduledReportScheduleBase _schedule = null;
private HashMap selectionList = new HashMap();
private EmailInfo _emailInfo = null;
private String _pdfFileName = null;
private short _baseDateOffset = -1;
在report对象中有以下属性
private String deflt = null;
private Object guiValue = null;
protected Object serverValue = null;
变量对象可以有任何类似的数组列表、字符串、布尔值或类对象。
但一旦解码成实际的对象,它就需要类型转换成它是什么类型,我们的目标是将这个对象转换成任何原始类型,并存储和检索回原始值。本质上,我们认为每个对象都是字符串,对象类型附加在字符串上,如“1_integer”、“Y_Boolean”并转换为blob,同时恢复拆分字符串并将字符串用作反射的一部分,以获取要转换的对象类型。但这不可行也不正确。任何想法。