简体   繁体   English

Java-.Net对象交换,不是基于Web的

[英]Java - .Net object interchange, not web-based

I have a client-server system implemented in C#, and the client and server exchange .Net objects via serialization / deserialization and communicating via TCP/IP. 我有一个用C#实现的客户端-服务器系统,客户端和服务器通过序列化/反序列化并通过TCP / IP进行通信来交换.Net对象。 This runs on a local network, it is not web-based or Internet-based. 它在本地网络上运行,而不是基于Web或Internet的。

Now I want to include Android clients connected by wifi. 现在,我想包括通过wifi连接的Android客户端。 Again, this is local network only, not via the Internet and not web-based. 同样,这仅是本地网络,不通过Internet也不基于Web。 The Android programming will be in Java. Android编程将使用Java。 (I am aware of Mono for Android, but prefer not to get into that now.) (我知道Android版Mono,但现在不建议加入。)

Is there some fairly simple way to implement object to object interchange between Java and .Net objects, provided, of course, that they are compatible? 是否存在一些相当简单的方法来实现Java和.Net对象之间的对象到对象的交换,当然,前提是它们是兼容的?

I've looked a bit at JSON (Jackson on the Java end and Json.Net on the .Net end), and I'm guessing it can probably be done, but only with major efforts on remapping things at each end as soon as the objects become fairly complicated. 我对JSON有所了解(Java端是Jackson,.Net端是Json.Net),我猜它可能可以实现,但是只有在尽最大努力重新映射两端的情况下对象变得相当复杂。

Any other suggestions? 还有其他建议吗? JSON-based or otherwise? 基于JSON还是其他?

PS. PS。 My question is somewhat related to this one Mapping tool for converting Java's JSON to/from C# , but it never got a suitable answer, perhaps due to insufficient info in the question. 我的问题与该用于将Java的JSON转换为C#或从C#转换Java的Mapping工具有关 ,但它可能没有得到合适的答案,这可能是因为问题中的信息不足。 Also, I don't care whether I end up using a JSON-based transport or XML or something else. 另外,我不在乎最终使用基于JSON的传输还是XML或其他方式。

I would suggest either JSON or XML (which is based on a .xsd file) because these are independent of their respective implementations (instead of something like an ObjectOutputStream in java). 我建议使用JSON或XML(基于.xsd文件),因为它们与各自的实现无关(而不是像Java中的ObjectOutputStream那样)。

The problem of having this format between the two components (client and server) is that they need to be at the same version. 在两个组件(客户端和服务器)之间使用这种格式的问题是它们需要具有相同的版本。 My best practice is to have one underlying definition of the format (i use xml with an xsd file which specifies how the xml has to look like), then use jaxb to generated java classes. 我的最佳实践是对格式进行一个基础定义(我将xml与xsd文件一起使用,该文件指定xml的外观),然后使用jaxb生成Java类。 That way you can (un)marshal from/to xml in the java part. 这样,您就可以在Java部分中将XML从/解组到XML。

I am very sure a similar thing exists in the world of .NET. 我非常确信.NET世界中也存在类似的情况。

JSON is smaller than xml in size, i find xml to be more readable. JSON的大小比xml小,我发现xml更具可读性。

SO user "default locale" should get the honor for this, but he/she has only answered via a comment. SO用户“默认语言环境”应该为此感到荣幸,但是他/她只是通过评论来回答。 So just to make it very clear what my choice was I'll answer my own question. 因此,为了清楚说明我的选择,我将回答自己的问题。

I've decided to go with Google Protocol Buffers, which in my opinion has much better support for moving objects back and forth between Java and .Net than JSON. 我已经决定使用Google协议缓冲区,我认为它比JSON对在Java和.Net之间来回移动对象提供了更好的支持。 Because I have a lot of experience with C#, and a lot of existing C#-defined classes, I've selected Marc Gravell's protobuf-net program for the .Net end, and Google's own support for the Android end (no - see edit). 因为我在C#方面有丰富的经验,并且有很多现有的C#定义的类,所以我选择了Marc Gravell的.Net端protobuf-net程序,以及Google自己对Android端的支持(请参见编辑) 。 This implies that I'm defining the objects in C#, not in .proto files - protobuf-net generates the .proto files from which I then generate the Java code. 这意味着我是在C#中定义对象,而不是在.proto文件中定义-protobuf-net生成.proto文件,然后从中生成Java代码。

Incidentally, as the transport mechanism I'm using a little-known program called naga on the Android end. 顺便说一句,作为传输机制,我在Android端使用了一个鲜为人知的程序naga。 http://code.google.com/p/naga/ Naga seems to work fine, and is well-documented and has sample programs, and should be better known in my opinion. http://code.google.com/p/naga/ Naga似乎运行良好,并且文档齐全,并具有示例程序,在我看来,应该更知名。

EDIT: 编辑:

OK, I've got it working now to my satisfaction. 好的,我已经满意了。 Here's what I'm using: 这是我正在使用的:

Google Protocol buffers as the interchange format: https://developers.google.com/protocol-buffers/ Google协议缓冲区作为交换格式: https : //developers.google.com/protocol-buffers/

Marc Gravell's protobuf-net at the C# end: http://code.google.com/p/protobuf-net/ Marc Gravell在C#端的protobuf-net: http : //code.google.com/p/protobuf-net/

A program called called protostuff at the Java end: http://code.google.com/p/protostuff/ Java端称为protostuff的程序: http : //code.google.com/p/protostuff/

(I prefer protostuff to the official Google Java implementation of protocol buffers due to Google's implementation being based on the Java objects being immutable.) (由于Google的实现是基于不可变的Java对象,因此我更喜欢使用Proststuff而不是协议缓冲区的官方Google Java实现)。

Actually, I'm not using pure protocol buffers as the interchange format - I prefix the data with the name of the (outermost) class being transmitted. 实际上,我没有使用纯协议缓冲区作为交换格式-我为数据加上要传输的(最外部)类的名称。 This makes the data self-identifying for deserializing at the other end. 这使得数据可以在另一端自识别以进行反序列化。

您也可以尝试wox( https://github.com/codelion/wox ),它是一个基于XML的Java和C#跨平台序列化库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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