views:

57

answers:

2

OK so im using websockets to let javascript talk to python and that works very well BUT the data i need to send often has several parts like an array, (username,time,text) but how could i send it ? I originally though to encode each one in base64 or urlencode then use a character like | which those encoding methods will never use and then split the information. Unfortunately i cant find a method which both python and javascript can both do.

So the question, is there a encoding method which bath can do OR is there a different better way i can send the data because i havent really done anything like this before. (I have but AJAX requests and i send that data URL encoded). Also im not sending miles of text, about 100bytes at a time if that. thankyou !

edit

Most comments point to JSON,so, Whats the best convert to use for javascript because javascript stupidly cant convert string to JSON,or the other way round.

Finished

Well jaascript does have a native way to convert javascript to string, its just hidden form the world. JSON.stringify(obj, [replacer], [space]) to convert it to a string and JSON.parse(string, [reviver]) to convert it back

+7  A: 

JSON is definitely the way to go. It has a very small overhead and is capable of storing almost any kind of data. I am not a python expert, but i am sure that there is some kind of en/decoder available.

elusive
I like JSON too, but it really doesn't have "small" overhead unless you're comparing to something a lot worse (XML). For example, in an array of objects of the same type, the object field names are repeated in each one. Of course it's possible to re-work the data structure to factor out the field names, but it'd be nice to have an encoding format that would do it automatically.
Pointy
+1 on JSON. simplejson is a python package that can help you doing that.
poulejapon
@Pointy: Thats right, but i do not know of a format that has a smaller overhead than JSON. At least no native one.
elusive
Ok, i wouldnt say json has a small over head. And whats the best JSON converter in javascript because for some reason javascript cant natively convert string to JSON array.
kizzie33
Google Protocol Buffers would be one example of a format with significantly lower overhead, as would any binary format. That's not to say there's anything wrong with JSON, which seems perfectly adequate for this.
Kylotan
@kizzie33: JSON is a native JavaScript notation (JSON means _"JavaScript Object Notation"_). You can simply output the generated JSON in the JavaScript code like this: `var data = YOUR_GENERATED_JSON_GOES_HERE;`. I recommend FireBug's `console.log(data)` command to inspect the resulting object.
elusive
no if i do a = "{'a':1,'b':2}";, a is now a string containing {'a':1,'b':2} not an array. I know what JSON is but javascript has no way to export and import it
kizzie33
@kizzle33: You can use the JSON2 library at http://json.org/json2.js. On modern browsers, it will use a native JSON parser.
Mike Axiak
I just looked it over and it uses the eval funcation....... i am not using that.
kizzie33
A: 

Use json module (or simplejson prior to Python 2.6).

You'd only need to remember two functions: json.dumps and json.loads.

>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> json.loads('["foo", {"bar": ["baz", null, 1.0, 2]}]')
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
OTZ