I’m implementing my own JSONEncoder for handling different classes exactly as I need them. Unfortunately, my custom encoder returns somewhat malformed strings. They are surrounded by quotes and certain characters (quotes) are escaped.
Please use the following code to reproduce the behaviour:
import json
class CustomEncoder(json.JSONEncoder):
def default(self, givenObject):
#for testing purposes this always returns the same string
str = '{"id":0,"name":"Peter"}'
return str;
class AnyClass(object):
pass
encoder = CustomEncoder()
dummyClass = AnyClass()
#expected output: {"id":0,"name":"Peter"}
print encoder.encode(dummyClass)
#output: "{\"id\":0,\"name\":\"Peter\"}"
I’m using Python 2.7.
How can I prevent this behaviour? What do I do wrong?
defaultshould return an object, not a chunk of json:http://docs.python.org/library/json.html#json.JSONEncoder.default: