simpleJSON pack binary data

A

Andrey

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
 
M

Marc 'BlackJack' Rintsch

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top