java - 我尝试序列化不可序列化的对象列表不起作用

标签 java serialization deserialization java-11

我有一个可序列化的类,其中包含不可序列化对象的列表。因此,我将该列表标记为 transient ,并在我的类中实现了 readObject 和 writeObject 。但是当我序列化和反序列化我的对象时,不会输入 readObject 和 writeObject 方法(我已经通过使用 sout 证明了这一点)。不知道为什么它不起作用。 这是我的代码。

我的可序列化类:

public class Product implements Serializable
{
private String reference;
private String name;
private int stock;
private double defaultPrice;
private transient List<Sale> sales = new ArrayList<>();

public Product(String reference, String name, int stock, double defaultPrice) 
{
    this.reference = reference;
    this.name = name;
    this.stock = stock;
    this.defaultPrice = defaultPrice;

}

public Product() {}

public void addSale(Date from, Date to, int minQuantity, int maxQuantity, double specialPrice)
{
    sales.add(new Sale(from, to, minQuantity, maxQuantity, specialPrice));
}

/*public double findPrice(Date date, int quantity)
{

}*/

public List<Sale> getSales()
{
    return sales;
}

 void writeObject(ObjectOutputStream oos) throws IOException
{
    System.out.println("ENTRO AQUI");
    oos.defaultWriteObject();
    oos.writeInt( sales.size() );
    for(Sale sale: this.sales)
    {

       oos.writeObject( sale.getFrom());
       oos.writeObject( sale.getTo() );
       oos.writeInt( sale.getMinQuantity() );
       oos.writeInt( sale.getMaxQuantity() );
       oos.writeDouble( sale.getSpecialPrice() );
    }
}

public void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException
{
    ois.defaultReadObject();
    int salesSize = ois.readInt();
    for(int i = 0; i < salesSize; ++i)
    {
        Date from = (Date) ois.readObject();
        Date to = (Date) ois.readObject();;
        int minQuantity = (int) ois.readObject();
        int maxQuantity = (int) ois.readObject();
        double specialPrice = (double) ois.readObject();
        Sale sale = new Sale(from, to, minQuantity, maxQuantity, specialPrice);
        this.sales.add(sale);
    }

}

我没有发布 setter 和 getter,因为我认为它们不相关。

我的不可序列化类:

public class Sale 
{
private Date from;
private Date to;
private int minQuantity;
private int maxQuantity;
private double specialPrice;

public Sale(Date from, Date to, int minQuantity, int maxQuantity, double specialPrice) 
{
    this.from = from;
    this.to = to;
    this.minQuantity = minQuantity;
    this.maxQuantity = maxQuantity;
    this.specialPrice = specialPrice;
}

public Sale() {
}

顺便说一句,这是我的读者:

public class BusinessBinaryReader 
{
public Business read(File file) throws IOException, ClassNotFoundException
{
    try(FileInputStream fis = new FileInputStream( file );
        BufferedInputStream bis = new BufferedInputStream( fis );
        ObjectInputStream ois = new ObjectInputStream( bis )
        )
    {

        return (Business) ois.readObject();
    }
}
}

还有我的作家:

public class BusinessBinaryWriter 
{
public void write(File file, Business business) throws IOException
{
    try(FileOutputStream fos = new FileOutputStream( file );
        BufferedOutputStream bos = new BufferedOutputStream( fos );
        ObjectOutputStream oos = new ObjectOutputStream( bos )
        )
    {
        oos.writeObject(business);
        oos.flush();
    }
}

}

最佳答案

您的自定义序列化函数 readObjectwriteObject 的签名错误。尝试将它们设置为私有(private),它应该可以工作。

来自doc :

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void writeObject(java.io.ObjectOutputStream out)
 throws IOException

private void readObject(java.io.ObjectInputStream in)
 throws IOException, ClassNotFoundException;

作为旁注,确实鼓励定义 SerialVersionUID,如果您的序列化对象生命周期较长,则更是如此。在此解释answer .

关于java - 我尝试序列化不可序列化的对象列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59861006/

相关文章:

java - 遍历二叉搜索树

java - 按顺序单击多个按钮,robotium 不起作用

serialization - Rdflib.js,如何将数据序列化为turtle(.ttl)格式?

java - 如何根据json中的属性编写jackson反序列化器

java - 将整数打印到文本字段区域

Java vector/列表元素删除

java - 使用 hashMap 序列化对象

javascript - 文件系统访问 API : is it possible to store the fileHandle of a saved or loaded file for later use?

algorithm - 将传入的串行数据放入结构中的优雅而有效的方法

java - jackson 反序列化为pojos