File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ByteArray in JSON

 
Cristian Vrabie
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic