0

I'm working on a project in java which is replacing an old C# program. In the old C# program, users could save out the data they were working on to a GZip file, written by Serializing a wrapper object which contained all the data, and writing it using a GZipStream.

I would like to offer users the option to load in data saved with the old system. Opening the GZip file using Java's GZipInputStream is easy enough, but how do I deserialize the object?

I did a little research and found this question: Can a serialized simple java object be deserialized by C#? The answer there said that there's no library to do this, but there is documentation on how Java serializes objects, and it is possible to write your own code to deserialize them.

Is there a known way to do this from C# to Java? Or should I write my own converter from scratch?

MMAdams
  • 1,508
  • 15
  • 29
  • 13
    Have you considered (as a workaround) creating a converter that would be written in C#, deserialize object and then serialize it in something language-agnostic like protobuff, json, xml...? – Stonehead Aug 09 '17 at 13:33
  • 1
    You need a compatible version of your C# objects to deserialize inside your Java program. This is not easy to get and require experts knowledge to debug. I would suggest reconsidering your approach. – Thorbjørn Ravn Andersen Aug 09 '17 at 13:47
  • @Stonehead That's a really good idea, I may end up doing that. – MMAdams Aug 09 '17 at 13:48

2 Answers2

2

You have to serialize everything in a JSON or XML file. No binary data should be stored in the file at all. Then you can de-serialize it within Java by using an arbitrary library.

You might serialize it with JSON.net and deserialize with Jackson.

UPDATE: In fact you might in fact write a SaveData class, compile it into a .dll and make it available for Com interop. Then, there are some java third party libraries which can make use of the SaveData class inside the dll so that it can interoperate in the java application. Then, serialize this SaveData into a file (binary format is ok). However I'm not sure how serialization is going to work out on byte-level. Are you really going to get the same object when you deserialize it on the java side? This solution might work but it is ugly as hell and at least as "painful" as re-writing the c# app to write to json

ThomasMX
  • 1,643
  • 2
  • 19
  • 35
  • Would that it were true that users could save their old data into a JSON or XML file, but unfortunately this is not the case. – MMAdams Aug 09 '17 at 13:46
  • 1
    Anyhow, the answer to your original question is no, a Java program cannot do such a thing. – ThomasMX Aug 09 '17 at 13:54
  • -1 He doesn't **have** to use text he most certainly **can** use a binary format. And a java program most certainly can do such a thing but unless somebody else already did that it would be way too complicated to do in order to achieve OP's goal. – Oleg Aug 09 '17 at 14:05
  • updated my answer – ThomasMX Aug 10 '17 at 09:07
0

You could embed a tiny .NET application inside your jar and invoke the .NET application using this SO question

The .NET application would deserialize the file into .NET objects and then convert the .NET objects to xml or json. The xml/json could then be deserialized into equivalent java objects in the JVM

lance-java
  • 25,497
  • 4
  • 59
  • 101