Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put byte array to JSON and vice versa

Is it possible to put a byte[] (byte array) to JSON?

if so, how can I do that in java? then read that JSON and convert that field again to byte[]?

like image 493
Amin Sh Avatar asked Dec 20 '13 15:12

Amin Sh


People also ask

Can you put byte array in JSON?

json now lets you put a byte[] object directly into a json and it remains readable. you can even send the resulting object over a websocket and it will be readable on the other side.

Can you convert array to JSON?

You convert the whole array to JSON as one object by calling JSON. stringify() on the array, which results in a single JSON string. To convert back to an array from JSON, you'd call JSON. parse() on the string, leaving you with the original array.


1 Answers

Here is a good example of base64 encoding byte arrays. It gets more complicated when you throw unicode characters in the mix to send things like PDF documents. After encoding a byte array the encoded string can be used as a JSON property value.

Apache commons offers good utilities:

 byte[] bytes = getByteArr();  String base64String = Base64.encodeBase64String(bytes);  byte[] backToBytes = Base64.decodeBase64(base64String); 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding

Java server side example:

public String getUnsecureContentBase64(String url)         throws ClientProtocolException, IOException {              //getUnsecureContent will generate some byte[]     byte[] result = getUnsecureContent(url);              // use apache org.apache.commons.codec.binary.Base64             // if you're sending back as a http request result you may have to             // org.apache.commons.httpclient.util.URIUtil.encodeQuery     return Base64.encodeBase64String(result); } 

JavaScript decode:

//decode URL encoding if encoded before returning result var uriEncodedString = decodeURIComponent(response);  var byteArr = base64DecToArr(uriEncodedString);  //from mozilla function b64ToUint6 (nChr) {    return nChr > 64 && nChr < 91 ?       nChr - 65     : nChr > 96 && nChr < 123 ?       nChr - 71     : nChr > 47 && nChr < 58 ?       nChr + 4     : nChr === 43 ?       62     : nChr === 47 ?       63     :       0;  }  function base64DecToArr (sBase64, nBlocksSize) {    var     sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,     nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);    for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {     nMod4 = nInIdx & 3;     nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;     if (nMod4 === 3 || nInLen - nInIdx === 1) {       for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {         taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;       }       nUint24 = 0;      }   }    return taBytes; } 
like image 115
Sam Nunnally Avatar answered Sep 22 '22 15:09

Sam Nunnally