Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# to serialize a Java deserializable object

I have two application that need to talk to each other. App1 needs to be able to serialize an object that App2 can then deserialize. Easily done, right? Here's the problem; App1 is C# based, App2 is Java based. So App1 needs to write out the file in the Java binary file format. How can this be done?

The way I see it, I have two options. The first is figure out some way to serialize a Java object in C#, so that App1 just creates the appropriate file. My other option would be to write a converter in Java that reads in a file and populates the object accordingly and serializes the newly populated object. That way the C# app would only have to write out some sort of formatted text file that the converter then interprets.

I can't make any changes to the Java application.

How should this be done?

Update:

The Java application is already in the hands of customers so changing the serialization scheme would cause the customers existing data to be incompatible. The Java App uses the native java serialization when dealing with this object. Modifications to the Java app can't happen.

The C# app uses protocol buffers to serialize its own data.

like image 849
Dan Vogel Avatar asked May 04 '09 21:05

Dan Vogel


People also ask

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What does %C do in C?

Roughly speaking, %c prints the ASCII representation of the character. %d prints its decimal value. Save this answer.

Is C good for beginners?

While C is one of the more difficult languages to learn, it's still an excellent first language pick up because almost all programming languages are implemented in it. This means that once you learn C, it'll be simple to learn more languages like C++ and C#.

Is C useful nowadays?

C is one of the oldest and most fundamental programming languages, and it is extensively used all over the world. C is a fast, portable language with a large library. It is a middle-level language with the advantages of both low-level and high-level languages.


2 Answers

The best answer is option 3: use a language-neutral serialization scheme.

I use JavaScript. Thrift is another option, protocol buffers I believe are more focused on RPC, but should be usable for serialization as well. XML or a custom binary format would be other options.

Edit: Sorry, didn't notice that you can't make changes to the Java application. That said, the best way to do it would probably be to create your own well defined format, write a java app that can read that format, then output a serialized java object for the legacy app.

like image 133
Kevin Peterson Avatar answered Nov 02 '22 23:11

Kevin Peterson


"IKVM" might be something you could use. This product allows you to convert compiled java bytecode (.jar, etc.) into a .NET DLL. It's super easy to use, and might give you the interop you need.

Other than this, the easiest way to accomplish this without a binary-level interop is to just use a plain text format, such as a CSV or XML.

like image 37
Andy White Avatar answered Nov 02 '22 22:11

Andy White