views:

47

answers:

2

I'm using Google App Engine and python for a web service. Some of the models (tables) I have in my web service have several binary data fields in them, and I'd like to present this data to a computer requesting it, all fields at the same time. Now, the problem is I don't know how to write it out in a way that the other computer knows where the first data ends and the other starts. I've been using JSON for all the things that aren't binary, but afaik JSON doesn't work for binary data. So how do you get around this?

You could of course separate the data and put it in its own model, and then reference it back to some metadata model. That would allow you to make a single page that just prints one data field of one of the items, but that is trappy both server and client implementation wise.

Another solution would be to put in some kind of separator, and just split the data on that. I suppose it would work and that's how you do it, but isn't there like a standardized way to do that? Any libraries I could use?

In short, I'd like to be able to do something like this:

binaryDataField1: data data data ...
binaryDataField2: data data data ...

etc

+1  A: 

One solution that would leverage your json investment would be to simply convert the binary data to something that json can support. For example, Base64 encoding might work well for you. You could treat the output of your BAse64 encoder just like you would a normal string in json. it looks like python has Base64 support built in, though i only use java on app engine so I can't guarantee that the linked library work in the sandbox or not.

Peter Recore
+2  A: 

Several easy options:

  • base64 encode your data - meaning you can still use JSON.
  • Use Protocol Buffers.
  • Prefix each field with its length - either as a 4- or 8- byte integer, or as a numeric string.
Nick Johnson