Home > other >  Send XML Document and JSON as bytes over web socket in Java
Send XML Document and JSON as bytes over web socket in Java

Time:10-04

What are the benefits of using ByteArrayOutputStream to convert the XML or JSON data to send over web socket instead of sending these values as Strings?

CodePudding user response:

  1. Security: JSON and XML easy to decode.(mostly for WS / compare to WSS)

  2. Efficiency: In traffic usage and in most case encode/decode processing. Byte-Arrays could be very compact compare to string, specially with data that are not string by nature (compare 4-bytes Boolean array of size 32 with over 128 (32*4) byte need for string representation, both data usage and encode/decode CPU usage). check THIS link

  3. Generality: Sending all type of data including any objects with complex hierarchical inheritances between them. In order to decode JSON with complex Tree-Like inheritance, you need very complex parsing method.

  4. Simplicity: Enable to chunk data meaningfully. suppose we always use first 2 byte of data as it's type. (to decode the rest). Normally additional libraries do that for us.

  5. Integrity: Easily recognizing corrupted data. Even without checksum, 1-bit data-corruption could be detected in most case.

  6. Compatibility: Using serialized object with version to control compatibility. (version control)-Although you could add version in JSON, it could cause many difficulty, inefficiency and trouble. check THIS

And probably other reasons in special cases.

  • Related