Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing a java serialized file in C#

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?

like image 264
Sven Avatar asked Dec 02 '22 01:12

Sven


2 Answers

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.

like image 109
Ernest Friedman-Hill Avatar answered Dec 07 '22 22:12

Ernest Friedman-Hill


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.

like image 45
fvu Avatar answered Dec 07 '22 22:12

fvu