XStream <->替代二进制格式(例如协议缓冲区)

发布于 2024-08-25 00:01:03 字数 620 浏览 13 评论 0 原文

目前,我们使用 XStream 以 XML 形式对 Web 服务输入/输出进行编码。然而,我们正在考虑切换到带有多种语言(protobuf、Thrift、Hessian 等)代码生成器的二进制格式,以便更轻松地支持新客户端并减少对手动编码的依赖(也是为了更好地支持包含二进制数据的消息格式) 。

然而,服务器上的大多数对象都是 POJO,XStream 通过反射和注释处理序列化,并且大多数这些库假设它们将自己生成 POJO。我可以想到几种连接替代库的方法:

  1. 为目标格式编写 XStream 封送拆收器。

  2. 编写自定义代码以将 POJO 编组到替代库生成的类或从替代库生成的类编组 POJO。

    编写自定义代码以将
  3. 子类化生成的类以实现 POJO 逻辑。可能需要一些重写。 (我是否还提到过我们要使用 Terracotta?)

  4. 使用另一个同时支持反射(如 XStream)和代码生成的库。

但是我不确定序列化库将是最适合上述技术。

We currently use XStream for encoding our web service inputs/outputs in XML. However we are considering switching to a binary format with code generator for multiple languages (protobuf, Thrift, Hessian, etc) to make supporting new clients easier and less reliant on hand-coding (also to better support our message formats which include binary data).

However most of our objects on the server are POJOs with XStream handling the serialization via reflection and annotations, and most of these libraries assume they will be generating the POJOs themselves. I can think of a few ways to interface an alternative library:

  1. Write an XStream marshaler for the target format.

  2. Write custom code to marshal the POJOs to/from the classes generated by the alternative library.

  3. Subclass the generated classes to implement the POJO logic. May require some rewriting. (Also did I mention we want to use Terracotta?)

  4. Use another library that supports both reflection (like XStream) and code generation.

However I'm not sure which serialization library would be best suited to the above techniques.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晒暮凉 2024-09-01 00:01:03

(1) 可能不需要那么多工作,因为许多序列化库都包含知道如何读/写原始值和分隔符的帮助程序 API。

(2) 可能为您提供最广泛的工具选择: https://github.com/eishay /jvm-serializers/wiki/ToolBehavior(有些是语言中立的)。有缺陷但希望不是完全无用的基准: https://github.com/eishay/jvm-serializers/wiki

其中许多工具都会生成类,这需要编写代码来与 POJO 进行转换。直接使用 POJO 的工具通常不是语言中立的。

(3) 似乎是一个坏主意(不了解您的具体项目)。我通常让我的消息类不受任何其他逻辑的影响。

(4) Protostuff 库(支持 Protocol Buffer 格式)让您编写一个“架构”来描述您希望如何序列化 POJO。但是,与仅仅编写在 POJO 和某些工具生成的类之间进行转换的代码相比,编写此架构可能会需要更多工作,并且更容易出错。

Protostuff 还可以通过反射自动生成架构,但这可能会产生消息格式感觉有点以 Java 为中心。

(1) might not be that much work since many serialization libraries include a helper API that knows how to read/write primitive values and delimiters.

(2) probably gives you the widest choice of tools: https://github.com/eishay/jvm-serializers/wiki/ToolBehavior (some are language-neutral). Flawed but hopefully not totally useless benchmarks: https://github.com/eishay/jvm-serializers/wiki

Many of these tools generate classes, which would require writing code to convert to/from your POJOs. Tools that work with POJOs directly typically aren't language-neutral.

(3) seems like a bad idea (not knowing anything about your specific project). I normally keep my message classes free of any other logic.

(4) The Protostuff library (which supports the Protocol Buffer format) lets you write a "schema" to describe how you want your POJOs serialized. But writing this schema might end up being more work and more error-prone than just writing code to convert between your POJOs and some tool's generated classes.

Protostuff can also automatically generate a schema via reflection, but this might yield a message format that feels a bit Java-centric.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文