5 ms·
For example, AFAIK there is still no agreement on how to handle binary data, e.g. what do you do with: >>> json.dumps(["\xb8\xc3\xb6\xbb"]) Strings in javasc
by dsp1234 10y ago
For example, AFAIK there is still no agreement on how to handle binary data, e.g. what do you do with:
>>> json.dumps(["\xb8\xc3\xb6\xbb"])
Strings in javascript/json are UTF-8, UTF-16, or UTF-32 encoded[0]. So providing a non-Unicode string is going to fail.
Instead, use an array of numbers limited to the byte range (as you would for any other binary data).
console.log(JSON.stringify({buffer: [0xb8, 0xc3, 0xb6, 0xbb]}))
//{"buffer":[184,195,182,187]}
[0] - http://rfc7159.net/rfc7159#rfc.section.8.1 http://rfc7159.net/rfc7159#rfc.section.8.1
- MichaelGG 10y agoIt seems like Base64 or beyond would be a vastly better way.
- dsp1234 10y agoI would personally use Base64 or Hex encoded binary data too if I controlled both sides of the communication. However, the original commenter was worried about stuff "that's outside the JSON standard,". Storing byte-sized numbers in an array is well within the JSON standard, and any system that could consume json would be able to consume it without issue, and without additional dependencies.
- anorwell 10y agoIt seems to me that an array of ints depends on agreed convention (beyond the JSON standard) just as much as a base64 string does. In general, interpreting the meaning of a JSON primitive (int, string, boolean) is something that is being agreed upon by the users of the format.
- tracker1 10y agoJSON is UTF8