Smit
Smit

Reputation: 65

Serializing a Java object and deserializing in C#

I am writing an application which requires many state variables to be passed from java to C#. I need to serialize the whole object for this and then somehow deserialize it in C# and initiate corresponding object in C#. Is this possible?

Can I use default java serialization for the purpose? If yes, how?

PS : The object in question is from an abstract class. The class extending this abstract class is the one that needs to be serialized and its definition can vary as per user preferences.

PPS: After being redirected to google developers' page innumerable times, I am considering the usage of WOX (mainly because it is extremely "easy" to use), but it seems pretty outdated and there has been no new update in last few years. Is there any alternative, with comparable ease of usage?

Upvotes: 0

Views: 2026

Answers (3)

gladiator
gladiator

Reputation: 1363

You can convert your object to xml and then pass the xml to C# and there again xml can be converted to C# object or data retrieved in other ways.

Upvotes: 0

david
david

Reputation: 185

No you can't use the default serialization, since java and c# do not have a compatible serializer. You have to implement the class twice and use your own code to translate between both languages.

To move your class instance from java to c# you need to collect all properties of the java class and use them to instantiate an new c# object.

You could do this by hand or use some helper like googles protocol-buffers.

Edit: Example for protobuf: Protobuf can just hold your data not your class logic. It does not know object orientated concepts. An abstract java class could be translated like this:

Javaclass:

abstract class Abs{
   int i;
}
class Real extends Abs{
   int k;
}

Protobuf:

message Abs{
    optional int32 i = 1;
}
message Real{
    optional Abs   base = 1;
    optional int32 k    = 2;
}

Upvotes: 0

Mrinal Kamboj
Mrinal Kamboj

Reputation: 11482

Can I use default java serialization for the purpose?

What does default Java serialization means, is it binary or text. If its binary then No, you cannot due to different binary standards between two frameworks, Java (Byte Code), .Net (IL). It would not be easy to make them compatible. If its text like Json, Xml or may be a custom text format, then shall be easily feasible and that's what Web services and Rest API use to communicate between varied systems, they use text based serialization over Http as a transport protocol.

Web Services - Soap (Http + Xml), Json is retrofitted, not part of original specification.

Rest API - Http + Json / Xml, in fact integrates with Java script seamlessly

Upvotes: 2

Related Questions