Page 1 of 1

customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Thu Feb 05, 2015 8:25 am
by itsriaz
Hi,
I am trying to use the customized serialization of the following example but no luck so for. Could you please write the full example? How to use this code in an example? Thanks in advance.

Code: Select all

class Bond  // does not implement Serializable
{
    String ticker = "bac"; double coupon = 8.3; java.util.Date maturity = new Date();
}

class Portfolio implements Serializable
{
    String accountName;
    transient Bond[] bonds = new Bond[]{ }; // must be transient because Bond class does not implement Serializable
    
    private void writeObject(ObjectOutputStream os) throws Exception{
	os.defaultWriteObject();
	os.writeInt(bonds.length);
	//write the state of bond objects
	for(int i=0; i<bonds.length; i++) {
		os.writeObject(bonds[i].ticker); 
		os.writeDouble(bonds[i].coupon);
		os.writeObject(bonds[i].maturity);
	}
    }

    private void readObject(ObjectInputStream os) throws Exception{
	os.defaultReadObject();
	int n = os.readInt();
	this.bonds = new Bond[n];
	//read the state of bond objects.
	for(int i=0; i<bonds.length; i++) {
		bonds[i] = new Bond();
		bonds[i].ticker = (String) os.readObject();	
		bonds[i].coupon = os.readDouble();
		bonds[i].maturity = (java.util.Date) os.readObject();
	}
	
    }    
}

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Thu Feb 05, 2015 9:21 am
by admin
It is complete code for the serialization/deserialization part. Where exactly are you facing a problem? Please post the exact code that you are actually trying to execute.

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Sat Feb 07, 2015 7:51 am
by itsriaz
ya, it is complete code for the serialization/deserialization part.But the problem is, i don't know how to execute it? write the data to file and read from the file. I tried this code but too many errors and don't know how to fix the code. Thanking you in advance.

Code: Select all

 
class Bond  // does not implement Serializable
{
    String ticker = "bac"; double coupon = 8.3; java.util.Date maturity = new Date();
}

class Portfolio implements Serializable
{
	private static final long serialVersionUID = -5778962857540547749L;
	String accountName = "xyz";  //added xyz
   transient Bond[] bonds = new Bond[]{ }; // must be transient because Bond class does not implement Serializable
    
    //added code ******************************************************
    
   public static void main(String[] args)
   {
   
	   Portfolio pfolio = new Portfolio();
   //write to a file
   try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myMap.data")))
   {
	   ((ObjectOutput) pfolio).writeObject(oos);
   } 
   
   //read to output
   try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myMap.data")))
	{
	    ((ObjectOutput) pfolio).readObject(ois);
	}
   
    //added code************************************************
    
    
    
    private void writeObject(ObjectOutputStream os) throws Exception{
	os.defaultWriteObject();
	os.writeInt(bonds.length);
	//write the state of bond objects
	for(int i=0; i<bonds.length; i++) {
		os.writeObject(bonds[i].ticker); 
		os.writeDouble(bonds[i].coupon);
		os.writeObject(bonds[i].maturity);
	}
    }

    private void readObject(ObjectInputStream os) throws Exception{
	os.defaultReadObject();
	int n = os.readInt();
	this.bonds = new Bond[n];
	//read the state of bond objects.
	for(int i=0; i<bonds.length; i++) {
		bonds[i] = new Bond();
		bonds[i].ticker = (String) os.readObject();	
		bonds[i].coupon = os.readDouble();
		bonds[i].maturity = (java.util.Date) os.readObject();
	}
	
    }
    
   } 
}


Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Sat Feb 07, 2015 12:10 pm
by admin
This is how your main method should be:

Code: Select all

    public static void main(String[] args) throws Exception{
        Portfolio pf = new Portfolio();
        FileOutputStream fos = new FileOutputStream("c:\\temp\\test.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(pf);
        oos.flush(); oos.close();fos.close();
        
        FileInputStream fis = new FileInputStream("c:\\temp\\test.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj = ois.readObject();
        System.out.println(obj);
        
        
    }
HTH,
Paul.

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Sat Feb 07, 2015 1:03 pm
by itsriaz
Excellent.. Thanks very much...

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Tue Apr 07, 2015 6:59 am
by XpressJava
Hi
I was just wondering if you guys can help me out.
I tried to implement this example but I doesn't print the Bond class array values.
In the writeObject() method, the bond.length = 0.
Any help will be very much appreciated.
Thanks in advance

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Tue Apr 07, 2015 7:19 am
by admin
Please post exact code that you are trying to run.

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Sun Apr 12, 2015 12:58 pm
by XpressJava
Hi I don't have my own code. I have been trying to implement the above code but when i check the bonds array length it's always 0.

Object obj = ois.readObject();
System.out.println(obj);
// how to print values of obj, it just prints "ioStream.Enthware_SerializationExample@60354949"


Code: Select all

package ioStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

class Bond {
	String ticker = "ticker"; double coupon = 8.3;  java.util.Date maturity = new Date();
}

class Enthware_SerializationExample implements Serializable {

	private static final long serialVersionUID = 1L;
	String client = "Client XYZ";
	transient Bond[] bonds= new Bond[]{};
	
	
	public static void main(String[] args) throws ClassNotFoundException, IOException 
	{
		Enthware_SerializationExample  pf = new Enthware_SerializationExample();
	
		
		FileOutputStream fos = new FileOutputStream("test.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(pf);
        oos.flush(); oos.close();fos.close();
        
        FileInputStream fis = new FileInputStream("test.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj = ois.readObject();
        System.out.println(obj);
        
	}
	
	private void writeObject(ObjectOutputStream os) throws Exception{
		 
		System.out.println("writing object " + bonds.length);  // why bonds.length=0 ?
		 os.defaultWriteObject();
		   os.writeInt(bonds.length);
		   //write the state of bond objects
		   for(int i=0; i<bonds.length; i++) {
		      os.writeObject(bonds[i].ticker); 
		      os.writeDouble(bonds[i].coupon);
		      os.writeObject(bonds[i].maturity);
		   }
		    }

		    private  void readObject(ObjectInputStream os) throws Exception{
		   os.defaultReadObject();
		   
		   System.out.println("Reading object");
		   
		   int n = os.readInt();
		   this.bonds = new Bond[n];
		   //read the state of bond objects.
		   for(int i=0; i<bonds.length; i++) {
		      bonds[i] = new Bond();
		      bonds[i].ticker = (String) os.readObject();   
		      bonds[i].coupon = os.readDouble();
		      bonds[i].maturity = (java.util.Date) os.readObject();
		      
		   }
		
		    }

}

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Sun Apr 12, 2015 9:24 pm
by admin
Well, what is the length of bonds array before saving it to the file? You will get the same length back. You have to change the code to create the bonds array of different length and initialize its elements with Bond object before serialization to get it back after deserialization. In the above code, your bonds array is of length zero, so you are getting 0 back!

To print a meaningful value using println(obj), you need to override the toString method in your class. This is OCAJP stuff :)
Paul.

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Mon Apr 13, 2015 6:01 am
by XpressJava
Ahaaa....Okay-doke.... I got it.... Thanks very much...

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Mon May 18, 2015 12:43 am
by pfilaretov
Q: What can be done so that a Portfolio object can be serialized while preserving the state of the Bond objects contained in Portfolio? 
A:
  • Just have Bond class implement Serializable.
  • Make bonds array transient in Portfolio and implement readObject and writeObject methods to read and write the state of Bond objects explicitly.
Question doesn't say "independently", so it's confusing why I need to do something else if I already make "Bond class implement Serializable"..

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Posted: Mon May 18, 2015 12:56 am
by admin
Fixed.
thank you for your feedback!