简体   繁体   English

在 Node.js 服务器环境中处理来自 C# 客户端的序列化 object 的最佳方法

[英]The best way to handle serialized object from C# client in Node.js server env

What's the correct/mainstream way to handle byte arrays sent from C# client code to Node.js server environment?处理从 C# 客户端代码发送到 Node.js 服务器环境的byte arrays的正确/主流方法是什么?

Now I'm using standard C# serialization through BinaryWriter (client-side) and streambuf npm-package (server-side).现在我通过BinaryWriter (客户端)和streambuf npm-package(服务器端)使用标准 C# 序列化。

But I'm pretty sure that there is a more straightforward/native/mainstream/efficient way to do it.但我很确定有一种更直接/本机/主流/有效的方法来做到这一点。

What I have to do?我必须做什么?

Thank you for your advices!谢谢你的建议!

The mainstream way to convert an object to a serialized representation would be to use a serialization framework.将 object 转换为序列化表示的主流方法是使用序列化框架。 If you are using node.js I would assume json would be most appropriate.如果您使用的是 node.js,我认为json是最合适的。 I'm not familiar with Node.js, but my understanding is that it handles json natively.我不熟悉 Node.js,但我的理解是它本机处理 json。 See also newtonsoft json for another very popular library.另请参阅newtonsoft json ,了解另一个非常受欢迎的库。

If you need more a more compact serialization format you might take a look at protobuf since that serializes to a binary format.如果您需要更多更紧凑的序列化格式,您可以查看protobuf ,因为它序列化为二进制格式。 It looks like there are protobuf packages available for Node.js, but again, I have no familiarity with javascript.看起来有 Node.js 可用的 protobuf 包,但同样,我对 javascript 不熟悉。

Using an existing serialization format tend to make it easier to share objects between different systems since there are usually libraries available for most popular formats for the most common platforms.使用现有的序列化格式往往可以更轻松地在不同系统之间共享对象,因为通常有适用于最常见平台的最流行格式的库。 It usually also makes compatibility easier since many formats allow some flexibility for things like optional fields.它通常还使兼容性更容易,因为许多格式允许对诸如可选字段之类的东西具有一定的灵活性。 So if you find that you need to add some more data you can often do so in the confidence that it will not break existing users.因此,如果您发现需要添加更多数据,您通常可以放心这样做,不会破坏现有用户。

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

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