1

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

1
  • Note that JSON.stringify and JSON.parse are not present in all JavaScript implementations. Many older browsers do not support that. Commented Oct 10, 2010 at 18:49

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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.
+1 on JSON. simplejson is a python package that can help you doing that.
@Pointy: Thats right, but i do not know of a format that has a smaller overhead than JSON. At least no native one.
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.
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.
|
1

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]}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.