File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes ByteArray in JSON Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "ByteArray in JSON" Watch "ByteArray in JSON" New topic
Author

ByteArray in JSON

Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
Hi all!
I'm trying to send a byte array (converted to a String) as a property in a JSON message, but it seems this is not quite valid - as reported by validators as JSONLint.com

I get something like:
syntax error, unexpected TINVALID at line 10
Parsing failed


Do you know if there's a standard for placing a byte array in a json message.

The way I encode it right now is something like:


Thanks in advance!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
Since many byte values turn into illegal characters if you try String conversion, you will have to use base64 encoding to create a String of legal characters. The same thing happens when trying to send arbitrary bytes in a SOAP message.

Bill
Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
Thanks William! That's exactly what I'll do.
However I'm a little surprised that this is not valid. There's nothing in the JSON documentation to say that the strings can contain only printable characters.
[ December 24, 2008: Message edited by: Cristian Vrabie ]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2986
    
    9
[Christian]: There's nothing in the JSON documentation to say that the strings can contain only printable characters.

From www.json.org:



Aside from the prohibition on control characters (only some of which have replacements using \ to escape), there's the fact that a string is assumed to consist of Unicode - and you need an encoding to convert bytes to Unicode chars. Some common encodings have gaps, byte values which have no unicode value. This isn't part of the JSON spec per se, but it's part of Unicode.

Backing up a bit:

[Christian]: Do you know if there's a standard for placing a byte array in a json message.

Well, JSON itself does define a standard for arrays, and for number. A byte array is just an array of numbers, so it would look like

[1,2,-7,0,42]

Using Base64 will be more compact however. Use whichever seems to meet your needs better.
Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
Thanks Mike. I don't know where my head was when I was looking on that image Base64 sound good.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ByteArray in JSON
 
Similar Threads
byteValue() method of Integer Class
Struts2 mapping query
Javascript array to JSON object via jquery
Convert String Array to Byte Array
NX: US-ASCII confusion