In Python I do like this to make dict and return in json:
class Thing:
id = 1
name = 'name'
user = User()
obj = Obj()
upd = time
def to_dict(this, user=False, obj=False):
return {
'id': self.id,
'name': self.name,
'upd': self.upd.strftime('%Y-%m-%d %H:%M:%S'),
'user': self.user.to_dict() if opt else None,
'obj': self.obj.to_dict() if obj else None,
}
and then json.dumps(thing.to_dict(**opts))
. opts
goes from web request.
Ofcourse in real project the fields a lot more. And some fields should be either in dict or not.
Is any solution to don't make different structs only for json response? Or add optional to MarshalJSON()