Saturday, 31 August 2013

How to make a JSON object serializable

How to make a JSON object serializable

Is there a way to serialize a python class without using a custom encoder?
I've tried the following approach but I get the error: TypeError: hello is
not JSON serializable which is strange since "hello" is a string.
class MyObj(object):
def __init__(self, address):
self.address = address
def __repr__(self):
return self.address
x = MyObj("hello")
print json.dumps(x)

No comments:

Post a Comment