I'm not a big fan of serialization, but there is one use that makes sense to me; intra-process, cross-AppDomain serialization. If you have ever done any cross-AppDomain work with IKVM you've probably run into the situation where a Java exception couldn't be serialized across the AppDomain boundary.
I've finally addressed this by building automatic (oneway) serialization interop support into IKVM. This means that most Java classes that are serializable should now automatically become .NET serializable. There are, however, some important caveats:
- Serialized streams will not be compatible between different IKVM releases. This is intended *only* for cross-AppDomain serialization between different instances of the same IKVM code.
- ObjectOutputStream.writeUnshared() and ObjectInputStream.readUnshared() have not been implemented. So any classes that rely on those will fail to serialize/deserialize.
- Instances of dynamically loaded Java classes and statically (ikvmc) compiled classes that implement readResolve() will fail deserialization if they (directly or indirectly) serialize self references.
- Deserialization ordering may be different, meaning that if a class has a custom deserialization method, it may encounter objects that have not been completely deserialized.
- Under some circumstances, a class that implements readResolve() may have its readResolve() method called twice on the same object.
- When a ghost array is serialized, it is serialized as an object (i.e. it loses its specific type).
Again, any Java class that is serializable (i.e. that implements java.io.Serializable or java.io.Externalizable and follows the associated rules) is generally automatically .NET serializable. There are a couple of exceptions where ikvmc and the runtime will assume that your class wants to handle its own .NET serialization:
Using the .NET custom serialization custom attributes OnDeserializedAttribute, OnDeserializingAttribute, OnSerializedAttribute or OnSerializingAttribute does not interfere with getting automatic serialization support (and the .NET serialization engine will call the annotated methods at the appropriate times).
Inheritance - Extending Java classes in .NET
When you want to subclass a serializable Java class in (e.g.) C# and make your subclass serializable as well, you need to do two simple things:
- Add a [Serializable] attribute to your class.
- Add a .NET deserialization constructor that calls the base class constructor and does nothing else.
Here's an example of extending java.util.ArrayList:
class MyList : java.util.ArrayList
private int exampleField;
protected MyList(SerializationInfo info, StreamingContext context)
: base(info, context)
MyList is now .NET serializable and exampleField will automatically be serialized/deserialized.
Inheritance - Extending .NET classes in Java
For this scenario, nothing has really changed. You still need to follow the standard .NET rules for creating serializable types.
Serializing .NET objects in Java
The automatic serialization interop is only one way, so you won't be able to serialize .NET objects using Java serialization (unless they happen to implement java.io.Serializable.__Interface or java.io.Externalizable). I currently have no plans to implement this functionality.