• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read a JSON byte array into a java object, e.g byte[]  RSS feed

 
Paul Kaatranen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to read a byte array passed from a web browser to the server.

The client reads a file then converts it to a string to pass to the server like this:

var bytes = new Uint8Array(arrayBuffer);
var s_bytes = JSON.stringify(bytes);

s_bytes now contains this for example (a txt file containing hello world) :

["{\"0\":104,\"1\":101,\"2\":108,\"3\":108,\"4\":111,\"5\":32,\"6\":119,\"7\":111,\"8\":114,\"9\":108,\"10\":100}"]

On the server side I get the string and try to convert it back to an array that I can then write to a file on the server, but this isn't as simple as I thought it should be. This example would then create a file containg the words hello world - of course the idea is to support other data also! So, my question is, is there an efficient way to convert the string to an array. Sure I could build a parser myself for this, but my guess is there are very fast packages out there that can do the job far better than I ever could!
com.fasterxml.jackson classes seems promising, but haven't got them to work yet.

Thanks in advance!
 
Stephan van Hulst
Saloon Keeper
Posts: 7968
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you even using JSON to send binary data? Just use an application/octet-stream MIME type and send the data directly.
 
Stephan van Hulst
Saloon Keeper
Posts: 7968
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to send other parameters as well, you can probably use multipart/form-data too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!