简体   繁体   中英

My attempt to serialize a non-serializable object list is not working

I have a serializable class which contains a list of a non serializable object. So I marked that list as transient and implemented readObject and writeObject in my class. But when I serialize and deserialize my object, the readObject and writeObject methods are not entered (I have proven it by using a sout). Don't know why it is not working. Here is my code.

My serializable class:

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);
    }

}

I didn't post setters and getters because I don't think that they are relevant.

My non-serializable class:

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() {
}

By the way this is my reader:

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();
    }
}
}

And my writer:

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();
    }
}

}

Your custom serialization functions readObject and writeObject have the wrong signature. Try setting them private and it should work.

From the 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;

As a side note, it is really encouraged to define a SerialVersionUID, more so if your serialized objects are long lived. Explained in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM