Export (0) Print
Expand All

Support for .NET Serialization

Visual Studio 2005

Visual J# 2005 supports .NET serialization for JDK equivalent classes in the J# class library that are Java serializable. Every J# Java-language type that is serializable (using either the Serializable interface or the Externalizable interface) is now automatically .NET Framework serializable. The J# classes that ship with Visual J# can be involved in serialization using .NET Framework APIs.

All user-defined classes must explicitly follow the .NET Framework serialization model to be serialized with the .NET Framework serialization APIs.

Enabling .NET Framework serialization for these Java-language serializable types enables the following behavior:

  • Enables the use of BinaryFormatter or SoapFormatter when serializing.

  • Enables objects of these types to participate in .NET Framework remoting.

  • Allows limited support for XMLFormatter. Support is limited because private fields will not be serialized as they normally are in the .NET Framework serialization model.

  • Allows user-defined classes to have members of these types and participate in .NET Framework serialization.

You can also write your own custom serialization. For more information, see Custom Serialization.

In the following example, an object is created which implements Serializable and uses J# class library types as fields. The Serializable attribute is also explicitly attached, which enables it be serializable using both the Java-language serialization model and the .NET Framework serialization model. In previous versions, this type could not be serialized using .NET Framework APIs because the J# class library types were not .NET Framework serializable. The object is serialized in both formats and then read back in again.

// Serialize.jsl
// Serialization Example

import java.io.*;
import System.IO.*;
import System.Runtime.Serialization.Formatters.Binary.*;
import System.Runtime.Serialization.*;
import System.SerializableAttribute;

// This class implements the Java language Serializable interface
// thus making it a serializable class
// The user explicitly marks this class to be .NET Framework serializable.
// using the System.Serializable attribute.
// The class uses J# class library types as fields.
/** @attribute Serializable() */
public class MyClass implements Serializable
{
    public Integer i, j, k;

    public MyClass()
    {
      i = j = k = new Integer(0);
    }

    public MyClass(int _i, int _j, int _k)
    {
      i = new Integer(_i);
      j = new Integer(_j);
      k = new Integer(_k);
    }

    public void Write()
    {
       System.Console.WriteLine("i: " + i + " j: " + j + " k: " + k);
    }
}

public class CMain
{

   {
       System.out.println("Static ctor for CMain");
   }

   public static void WriteObjectNET(Object obj)
   {
       IFormatter formatter = new BinaryFormatter();
       Stream stream = new FileStream("MyFile.bin", FileMode.Create,
           FileAccess.Write, FileShare.None);
       formatter.Serialize(stream, obj);
       stream.Close();
   }

   public static void WriteObjectJava(Object obj) throws 
   java.io.IOException
   {
       FileOutputStream fos = new FileOutputStream("serialized_Object");
       ObjectOutputStream os = new ObjectOutputStream(fos);
       os.writeObject(obj);
       os.flush();
       os.close();
   }

   public static Object ReadObjectNET()
   {
       IFormatter formatter = new BinaryFormatter();
       Stream stream = new FileStream("MyFile.bin", FileMode.Open,
           FileAccess.Read, FileShare.None);
       Object o = formatter.Deserialize(stream);
       stream.Close();
       return o;
   }

   public static Object ReadObjectJava() throws java.io.IOException, 
   ClassNotFoundException
   {
       FileInputStream fis = new FileInputStream("serialized_Object");
       ObjectInputStream ois = new ObjectInputStream(fis);
       Object o = ois.readObject();
       ois.close();
       return o;
   }

   public static void main()
   {
       MyClass obj1 = new MyClass(100, 200, 300);
       try
       {
          WriteObjectNET(obj1);
          WriteObjectJava(obj1);
          obj1 = (MyClass) ReadObjectNET();
          obj1.Write();
          obj1 = (MyClass) ReadObjectJava();
          obj1.Write();
       }
       catch(Exception e)
       {
          System.Console.WriteLine("Exception!");
          System.Console.WriteLine(e.get_Message());
       }
      
   }
}

i: 100 j: 200 k: 300
i: 100 j: 200 k: 300

Community Additions

ADD
Show:
© 2014 Microsoft