Just getting started with Python, but the first goal is to create a web service. I plan on using werkzeug for this, however, all I will be doing is writing a client to interface with it. How would I go about exchanging objects between the 2 systems? Is there anything similar to JSON for Python?
EDIT:
A couple mentions of JSON, but my main problem is that I can’t serialize a class? I sort of thought that was what JSON could do?
class User():
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age
user = User("John", "Doe", 25)
json.dumps(user)
TypeError: <__main__.User instance at 0x02ABBEE0> is not JSON serializable
I know what you are serializing in JSON isn’t necessarily a class, it is an object, but you can still reference it almost as such:
var mailingAddress = {
"Address" : "123 Anywhere St.",
"City" : "Springfield",
"PostalCode" : 99999
};
alert("The package will be shipped to postal code " + mailingAddress.PostalCode);
Sounds like you’re looking to use pickle instead of json — pickles are Python specific but can handle user classes as well as dicts:
If you need a variant that is language independent, look at PyYAML and the YAML spec at http://yaml.org