views:

839

answers:

4

I have two application that need to talk to each other. App1 needs to be able to serialize an object that App2 can then deserialize. Easily done, right? Here's the problem; App1 is C# based, App2 is Java based. So App1 needs to write out the file in the Java binary file format. How can this be done?

The way I see it, I have two options. The first is figure out some way to serialize a Java object in C#, so that App1 just creates the appropriate file. My other option would be to write a converter in Java that reads in a file and populates the object accordingly and serializes the newly populated object. That way the C# app would only have to write out some sort of formatted text file that the converter then interprets.

I can't make any changes to the Java application.

How should this be done?

Update:

The Java application is already in the hands of customers so changing the serialization scheme would cause the customers existing data to be incompatible. The Java App uses the native java serialization when dealing with this object. Modifications to the Java app can't happen.

The C# app uses protocol buffers to serialize its own data.

+4  A: 

The best answer is option 3: use a language-neutral serialization scheme.

I use JavaScript. Thrift is another option, protocol buffers I believe are more focused on RPC, but should be usable for serialization as well. XML or a custom binary format would be other options.

Edit: Sorry, didn't notice that you can't make changes to the Java application. That said, the best way to do it would probably be to create your own well defined format, write a java app that can read that format, then output a serialized java object for the legacy app.

Kevin Peterson
Maybe so, but the result must still be in Java's format. The question says: "I can't make any changes to the Java application."
Michael Myers
I agree with the edit solution. If you can control the C# app, then you could just write a little java program that would read your xml serialized object from the C# and spit out java native serialized files. Your C# app could spawn the java app when you are ready to "export" or whatever your process is there.
digitaljoel
A: 

"IKVM" might be something you could use. This product allows you to convert compiled java bytecode (.jar, etc.) into a .NET DLL. It's super easy to use, and might give you the interop you need.

Other than this, the easiest way to accomplish this without a binary-level interop is to just use a plain text format, such as a CSV or XML.

Andy White
+1  A: 

Just use XML serialization. Both frameworks have good support, and the simplicity will make it easier to debug / maintain. Write a small program in Java that just imports the XML and writes the binary file.

McWafflestix
A: 

Your best bet would be to write something that uses Java Native interface. Not fun, but it'll work.

You can do this directly using JNI (not fun but doable) or there may be some tools out there that will generate code for you -- take a look at SWIG: http://www.swig.org/

You would call Java from C# to do the persistence for you.

Scott Stanchfield