Serializable对象的成员变量的序列化和反序列化规则

时间:2016-04-21 05:14:53

标签: java serialization deserialization

我已阅读Constructor Calls while serializationSerialization Rules但无法找到会员变量的所有规则。

我有以下问题:

Student.java

class Person {
}
class Student extends Person implements Serialization {
   List<Book> books; 
   Student(){}
   Student(List<Book> books) {
      this.books = books;
   }
}

class Book implements Serialization {
  Book() {}
  String bookId;
  Book(String bookId) {
      this.bookId = bookId;
  }

}

这里Student是可序列化的,Book类也是可序列化的,那么构造函数调用可序列化和反序列化的规则是什么。

  1. Book类中是必需的默认构造函数吗? 如果是,则为什么在反序列化时需要默认构造函数。
  2. 构造函数在序列化和反序列化时如何调用?

2 个答案:

答案 0 :(得分:0)

请参阅下面的答案:

Is required default constructor in Book class?

- &GT;是的,它是强制性的,否则序列化将失败。

How constructor calls while serialization and deserialization?

- &GT;序列化使用反射,并且必须使用默认构造函数来使用反射。

答案 1 :(得分:0)

实际上,您应该将a no-arg constructor放在Person的类中,Studentjava.io.InvalidClassException的超类。

如果不是,反序列化时将抛出运行时{ "ProductId": 20, "ProductName": "LCD", "ProductDescription": "Sony", "BuyingPrice": "15000 ", "VendorId": 46, "CategoryId": 25, "CreatedBy": 0, "UpdateBy": null, "IsActive": true, "IsDeleted": false, "CreatedDate": "2018-03-14", "UpdatedDate": null, "WareHouseProducts": [ { "WareHouseProductsId": 11, "TotalQuantity": 15, "AvailableQuantity": 8, "ProductId": 20, "WareHouseId": 4, "CreatedBy": null, "UpdateBy": null, "IsActive": true, "IsDeleted": false, "CreatedDate": "2018-03-14", "UpdatedDate": "2018-03-15", "WarehouseName": "Warehouse 1" } ] }