简体   繁体   中英

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?

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

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;
}

您可以将对象转换为xml,然后将xml传递给C#,然后再次可以将xml转换为C#对象或以其他方式检索的数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM