Export (0) Print
Expand All

How to: Deserialize Java Serialized Objects from the .NET Framework

Visual Studio 2005

Deserialization is the process of retrieving stored data that an application previously persisted. Once retrieved, the common language runtime (CLR) maps the data to an instantiated object with identical fields.

When you are deserializing:

  • Your class should have a default constructor — a constructor that takes no parameters — in order to be serialized.

  • You must cast the deserialized data to a class that matches the order of the serialized data.

  • Serialization occurs only on public data.

  • A runtime exception occurs when the data types of the deserialized data do not match the class that receives the data.

The following example caches application information by serializing and deseriailizing basic customer information.

To deserialize data

  1. Create a console application called BinaryFormatter.

  2. In Solution Explorer, click Program.jsl, and then replace the codewith the following:

    package BinaryFormatter;
    //Serialize, and then deserialize.
    import System.IO.*;
    import System.Runtime.Serialization.*;
    import System.Runtime.Serialization.Formatters.Binary.*;
    
    /** @attribute System.SerializableAttribute() */
    public class Address
    {
        //Default CTOR Required for serialization.
        public Address() { }
    
        public String Street;
        public String City;
        public String Region;
        public String PostalCode;
       
        public void CacheAddress()
        {        
            try
            {
                // Various address checks go here:
                if (this.Street.length() > 0)
                {
                    FileStream fs = new FileStream("serial.text", FileMode.OpenOrCreate);
                    BinaryFormatter bf = new BinaryFormatter();
                    //Serialization starts here.
                    bf.Serialize(fs, this);
                    fs.Flush();
                    fs.Close();
                }
            }
            catch (IOException ex)
            {
                System.out.println(ex.toString());
            }
        }
        public Address RetrieveAddress()
        {
            Address address2 = new Address();
            try
            {
                FileStream fs = new FileStream("serial.text", FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                //Deserialization starts here.
                address2 = (Address)bf.Deserialize(fs);
                fs.Close();
            }
            catch (Exception ex)
            {
                System.out.println(ex.toString());
            }
            return address2;
        }
            public String toString()
        {
            return "" + Street + " " + City + " " + Region + " " + PostalCode;
        }
    
        public static void main(String[] args)
        {
            Address customerAddress = new Address();
            customerAddress.Street = "1111 White Street";
            customerAddress.City = "Sturtevant";
            customerAddress.Region = "WI";
            customerAddress.PostalCode = "53177";
            customerAddress.CacheAddress();
    
            Address cachedAddress = new Address();
            cachedAddress = customerAddress.RetrieveAddress();
            System.out.println(cachedAddress.toString());    
        }
    }
    
  3. Press F5 to compile and run the program.

    The data successfully stores and then retrieves the address. The output is: 1111 White Street Sturtevant WI 53177.

  4. From the BinaryFormatter\bin\debug\ directory, view the serial.bin file.

See Also

Community Additions

ADD
Show:
© 2015 Microsoft