471,330 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

simpleJSON pack binary data

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
1 4803
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.

Similar topics

2 posts views Thread by ashokbellur | last post: by
1 post views Thread by Niko Korhonen | last post: by
10 posts views Thread by Dan | last post: by
2 posts views Thread by Ron Snyder | last post: by
6 posts views Thread by | last post: by
reply views Thread by Wescotte | last post: by
3 posts views Thread by stockblaster | last post: by
9 posts views Thread by thorley | last post: by
8 posts views Thread by brainflakes.org | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.