3

Are there any compatibility issues to take care of when serailizing an object in .NET and then deserializing in Java?

I am facing problems in de-serializing an object in java which has been serialized in .NET

Here is the detailed problem statement:

In .NET platform i have a cookie. 1. Cookie is serialized 2. then it is encrypted using Triple DES algo. 3. Send it across to Java application

In Java platform 1. Decrypt the cookie using Triple DES which gives some bytes 2. Deserialize the bytes using something like

new ObjectInputStream( new ByteArrayInputStream(byte[] decryptedCookie)).readObject();

The exception stack trace I get is: java.io.StreamCorruptedException: invalid stream header: 2F774555 at java.io.ObjectInputStream.readStreamHeader(Unknown Source) at java.io.ObjectInputStream.(Unknown Source)

3 Answers3

5

The WOX serializer provides interoperable serialization for .Net and Java.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Does that mean C#/.NET and Java will have to use the WOX serializer only in order to be compatible? Is there no other option? –  Aug 20 '09 at 07:33
  • The only other option I know if is to handle serialization / deserialization yourself on either .Net or Java to replicate the format of the other platform. That's the whole reason the WOX project came about. – Eric J. Aug 20 '09 at 16:10
  • Update: JSON serialization has become a popular, cross-platform serialization mechanism since my original post. It's a fairly compact text representation, but not as compact as a binary format. – Eric J. Jan 05 '12 at 04:02
  • Hi Eric, Is it possible to handle serialization/deserialization on our own. how this is possible? – Aada Aug 23 '13 at 11:36
  • @Aada: Sure, you can write your own cross-platform serializer. That is not a trivial thing to do. – Eric J. Sep 10 '13 at 23:14
1

If you serialize in xml then you shouldnt face any problems de-serializing in java since at worse you have to write your own bit of code to reconstruct the objects.

Daniel
  • 22,363
  • 9
  • 64
  • 71
  • How is serializing in xml different from serializing a String in .NET? Ultimately what matters is how i am de-serializing in java. Isn't it?? What i understand is that ObjectInputStream is the only way to deserialize in java and that would not work here. Please clarify ?? –  Aug 20 '09 at 07:32
  • I never desearialized in java, all I am saying is that worse case, if u have XML of ur objects , U'll have to write some code which will parse th XML and build ur objects back up in java – Daniel Aug 20 '09 at 12:51
0

The way java and .Net serialise to binary differs. How does one know the objects of the other e.g. .Net will have Dictionaries and Java Maps? (plus the bnary representation of a string might differ.

You have to use some data format that both understand and code to do the object mappings. Thus the above answers mentioning XML and WOX. I have worked with internal company produces as well.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117