I have a class looking like the following:
class Sound(SoundJsonSerializer):
def __init__(self, name, length):
self.name = name
self.length = length
where SoundJsonSerializer enabling my custom JSONEncoder to serialize this object into JSON:
{
"name": "foobar",
"length": 23.4
}
Now I want one of my requests to respond exactly with the above JSON.
@app.route('/sounds/<soundid>')
def get_sound(soundid):
s = Sound("foobar", 23.4)
return jsonify(s)
yields an error claiming s was not iterable which is true. How do I make the method return my wanted JSON?
I know I can do it by explicitly creating a dict from my Sound object like this:
return jsonify({"name": s.name, "length": s.length})
But this seems really ugly to me. What's the preferred way to achieve my goal?
dir()andgetattr()).