"""JSON Util. Util for encoding in JSON a response. Some classes cannot be directly encoded, so this method does this automatically for us. """ from flask.json import JSONEncoder from sukimu import exceptions class CustomJSONEncoder(JSONEncoder): """Custom JSON Encoder.""" def default(self, obj): """Default encoding behavior. Args: obj (object): any object that needs to be json encoded. """ if (isinstance(obj, exceptions.FieldException) and len(obj.args) == 1 and isinstance(obj.args[0], str)): return str(obj) return JSONEncoder.default(self, obj)