4

I have a have a java project that serializes some objects and ints to a file with functions like

ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(RANK_SIZE);
oos.writeObject(_firstArray);
oos.writeObject(_level[3]);
oos.writeObject(_level[4]);
...

Now I have trouble deserializing that file with C# (tried the BinaryFormatter) as it apparently can only deserialize the whole file into a single object (or arrays, but I have different objects with different lenghts).

I tried first to port the generation of these files to C#, but failed miserably. These files are small and I don't must to generate them myself.

Do I need to alter the way those files are generated in Java or can I deserialize it in any way?

2
  • I am not sure java has same binary formatter as .NET does. Why don't you use xml instead? Anyway good question. I would like to hear what people say Commented Aug 24, 2011 at 22:59
  • 3
    You need a cross-platform serialization format; I lean towards protobuf, but xml, json, heck: CSV! anything that isn't tied to the platform Commented Aug 24, 2011 at 23:04

3 Answers 3

10

There is nothing in the standard .NET framework which knows how to deserialize Java objects. You could, in theory, use the Java serialization spec and write your own deserialization code in C#. But it would be a large and complex project, and I don't think you'd find many customers interested in using it.

Far, far easier would be to change the way you serialize the data to use a portable format: JSON, or some form of XML. There are both Java and C# libraries to deal with such formats and the total effort would be orders of magnitude less. I would vastly prefer this second approach.

Sign up to request clarification or add additional context in comments.

3 Comments

C# is a language. You meant no pre distributed package in the Framework can do it. ikvm can do it within C#
Thanks, I created a seperate class in both java and c#, serialized it with Json and had no problems deserializing it.
+1 I just wanted to clarify not be contrary. Json is the way to go.
5

Maybe an at first sight counterintuitive solution might be to serialize them to some commonly understandable format like JSON? I'm not even sure the binary format for serialized objects in Java is guaranteed to remain unchanged between Java versions....

Jackson is my personal favorite when it comes to Java JSON libraries.

Comments

3

http://www.ikvm.net/ IKVM can do it perfectly.

1 Comment

@Ernest Friedman-Hill I am too but yes it works. They have tests that show running Eclipse and JBoss using IKVM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.