# Tuesday, 11 August 2009
« IKVM 0.40 Update 1 Release Candidate 1 | Main | Microsoft PDC »
Serialization Interop

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:

  1. Add a [Serializable] attribute to your class.
  2. Add a .NET deserialization constructor that calls the base class constructor and does nothing else.

Here's an example of extending java.util.ArrayList:

[Serializable]
class MyList : java.util.ArrayList
{
  private int exampleField;

  public MyList()
  {
  }

  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.

Tuesday, 11 August 2009 09:24:07 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
Tuesday, 11 August 2009 13:35:55 (W. Europe Daylight Time, UTC+02:00)
What means exactly *are* you a fan of for saving and retrieving object graphs to and from persistent storage?
Tuesday, 11 August 2009 14:32:13 (W. Europe Daylight Time, UTC+02:00)
It's actually the idea that there should be a generic all purpose method for saving and retrieving object graphs that I reject.

Persistence formats and wire protocols should be explicitly designed.
Name
E-mail
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Answer:  
Comment (HTML not allowed)  

Live Comment Preview