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:
Security: JSON and XML easy to decode.(mostly for WS / compare to WSS)
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
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.
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.
Integrity: Easily recognizing corrupted data. Even without checksum, 1-bit data-corruption could be detected in most case.
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.