Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XStream <-> Alternative binary formats (e.g. protocol buffers)

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.

like image 479
sehugg Avatar asked Mar 22 '26 16:03

sehugg


1 Answers

(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.

like image 104
Kannan Goundan Avatar answered Mar 24 '26 10:03

Kannan Goundan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!