• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ByteArray in JSON

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic