| 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: 12266
|
|
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
|
Java Resources at www.wbrogden.com
|
 |
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: 2770
|
|
[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.
|
 |
 |
|
|
subject: ByteArray in JSON
|
|
|