By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,387 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,387 IT Pros & Developers. It's quick & easy.

simpleJSON pack binary data

P: n/a
Hi

I donno if this is the right place to ask for this question, anyway....

Is it possible to pack binary data into simplejson?

d={}
d['date'] = xxxxxx
d['name'] = xxxxxx
d['size'] = xxxxx

d['imageBinaryDataJpeg'] = jpegBinaryDataFromcStringIOStringIO

simplejson.dumps(d)

when i do this, it raises a UTF8 decode error, probably about the binary
image data

My question is, anyone will suggest a workaround to this error?
i really like to pack my raw image data into the JSON, so my other
programming script can read the array easily

Thanks
An K


Jul 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sat, 21 Jul 2007 19:13:22 -0700, Andrey wrote:
My question is, anyone will suggest a workaround to this error?
i really like to pack my raw image data into the JSON, so my other
programming script can read the array easily
JSON is a text format so you have to encode the binary data somehow. I'd
use base64. It's available as codec for `str.encode()`/`str.decode()`.

In [10]: '\x00\xff\xaa'
Out[10]: '\x00\xff\xaa'

In [11]: '\x00\xff\xaa'.encode('base64')
Out[11]: 'AP+q\n'

In [12]: _.decode('base64')
Out[12]: '\x00\xff\xaa'

Ciao,
Marc 'BlackJack' Rintsch
Jul 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.