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?

Sven
  • 2,839
  • 7
  • 33
  • 53
  • 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 – Tae-Sung Shin Aug 24 '11 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 – Marc Gravell Aug 24 '11 at 23:04

3 Answers3

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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
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.

fvu
  • 32,488
  • 6
  • 61
  • 79
3

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

Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49