In java how do I serialize a class that is not marked Serializable?

前端 未结 4 1906
醉梦人生
醉梦人生 2020-12-07 01:09

There is a specific class in a third party library that I want to serialize. How would I go about doing this?

I\'m assuming I will have to write a method that tak

4条回答
  •  不思量自难忘°
    2020-12-07 01:32

    Another possible solution may be to define a set of private methods inside your Serializable class that uses the instances of the third party class.These special methods are part of a special callback contract the serialization system offers.These methods will be called during the serialization/deserialization process. Their signatures must be like:

    private void writeObject(ObjectOutputStream os) {
    // your code for saving the third party variables
    }
    private void readObject(ObjectInputStream is) {
    // your code to read the third party state, create a new ThirdParty instance,
    // and assign it to your class.
    }
    

    This example clarifies this idea further:

    public class MyClass implements Serializable 
    {
       transient private ThirdParty thirdPartyInstance ;
        private int  myClassVariable ;
        private void writeObject(ObjectOutputStream oos)
        {
            try
            {
    
                oos.defaultWriteObject();
                oos.writeInt(thirdPartyInstance.getThirdPartyVariable());
                oos.writeInt(thirdPartyInstance.getFourthPartyInstance().getFourthPartyVariable());
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        private void readObject(ObjectInputStream ois)
        {
            try
            {
                ois.defaultReadObject(); //the call to defaultReadObject method must always be before any other code in the try block
    
                //Reconstructing thirdPartyInstance 
    thirdPartyInstance =new ThirdParty(ois.readInt(),new FourthParty(ois.readInt()));
    
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        MyClass(int myClassVariable, ThirdParty thirdPartyInstance)
        {
            this.myClassVariable=myClassVariable;
            this.thirdPartyInstance=thirdPartyInstance;
        }
        ThirdParty getThirdPartyInstance()
        {
            return thirdPartyInstance;
        }
    
        int getMyClassVariable()
        {
            return myClassVariable;
        }
    
        public static void main(String args[])
        {
            FourthParty fourthPartyInstance=new FourthParty(45);
            ThirdParty thirdPartyInstance=new ThirdParty(13,fourthPartyInstance);
            MyClass myClassInstance=new MyClass(7,thirdPartyInstance);
            System.out.println("Before: ThirdParty variable value is "+myClassInstance.getThirdPartyInstance().getThirdPartyVariable());
            System.out.println("Before: FourthParty variable value is "+myClassInstance.getThirdPartyInstance().getFourthPartyInstance().getFourthPartyVariable());
            System.out.println("Before: MyClass variable value is "+myClassInstance.getMyClassVariable());
            try
            {       
                FileOutputStream fios=new FileOutputStream("D://TestFileq.ser");
                ObjectOutputStream oos=new ObjectOutputStream(fios);
                oos.writeObject(myClassInstance);
                oos.close();
    
    
                FileInputStream fi = new FileInputStream("D://TestFileq.ser");
                ObjectInputStream objectIn = new ObjectInputStream(fi);
                MyClass myClassInst = (MyClass)objectIn.readObject();
                System.out.println("After: ThirdParty variable value is "+myClassInst.getThirdPartyInstance().getThirdPartyVariable());
                System.out.println("After: FourthParty variable value is "+myClassInst.getThirdPartyInstance().getFourthPartyInstance().getFourthPartyVariable());
                System.out.println("After:MyClass variable value is  "+myClassInst.getMyClassVariable());
    
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
        }
    
    }
    class ThirdParty
    {
        private int thirdPartyVariable;
        private FourthParty fourthPartyInstance;
        ThirdParty(int thirdPartyVariable,FourthParty fourthPartyInstance)
        {
            this.thirdPartyVariable=thirdPartyVariable;
            this.fourthPartyInstance=fourthPartyInstance;
        }
        int getThirdPartyVariable()
        {
            return thirdPartyVariable;
        }
        FourthParty getFourthPartyInstance()
        {
            return fourthPartyInstance;
        }
    
    
    }
    class FourthParty
    {
        private int fourthPartyVariable;
        FourthParty(int fourthPartyVariable)
        {
            this.fourthPartyVariable=fourthPartyVariable;
        }
        int getFourthPartyVariable()
        {
            return fourthPartyVariable;
        }
    
    
    }
    

    Note that the thirdPartyInstance in MyClass must be declared transient otherwise an exception of type 'java.io.NotSerializableException' occurs. For more explanation see: SCJP Sun Certified Programmer for Java 6 by 'Cathy Sierra',Page Number 497

提交回复
热议问题