simpleJSON pack binary data

Discussion in 'Python' started by Andrey, Jul 22, 2007.

  1. Andrey

    Andrey Guest

    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
     
    Andrey, Jul 22, 2007
    #1
    1. Advertising

  2. 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
     
    Marc 'BlackJack' Rintsch, Jul 22, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Jones
    Replies:
    0
    Views:
    381
    Tim Jones
    Jan 31, 2004
  2. Robin Becker

    simplejson setup problem

    Robin Becker, Dec 11, 2007, in forum: Python
    Replies:
    0
    Views:
    319
    Robin Becker
    Dec 11, 2007
  3. Replies:
    11
    Views:
    700
  4. woof

    pack, Win32 registry & binary data

    woof, Dec 1, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    170
    Ben Liddicott
    Dec 2, 2003
  5. Alexander Farber

    pack 'C3U*' not same as pack 'C3(xC)*'

    Alexander Farber, Jun 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    137
    Ilmari Karonen
    Jun 23, 2005
Loading...

Share This Page