4. Custom Encoder
A Custom Encoder in Flask is typically used to customize the way objects are serialized into JSON format. By default, Flask (through the jsonify function and the json module) can only handle certain types of objects. If you need to serialize custom objects (like classes or special data types), you need to extend the default JSON encoder.
Here’s a step-by-step explanation with an example:
Step-by-Step Explanation
Default JSON Encoder:
The default encoder in Flask can handle basic Python data types like strings, lists, dictionaries, and numbers.
Custom Objects:
When you have custom objects (like a class instance), you need a way to tell Flask how to convert these objects into JSON-compatible data types.
Custom JSON Encoder:
You create a custom encoder by subclassing
flask.json.JSONEncoderand overriding thedefaultmethod. This method should return a serializable version of your custom object or call the superclass'sdefaultmethod if it can't handle the object.
Using the Custom Encoder:
You tell Flask to use your custom encoder by setting it in the app configuration.
Example
Let's say you have a User class that you want to serialize to JSON.
from flask import Flask, jsonify
from flask.json import JSONEncoder
import datetime
app = Flask(__name__)
# Custom User class
class User:
def __init__(self, id, username, email, created_at):
self.id = id
self.username = username
self.email = email
self.created_at = created_at
# Custom JSON Encoder
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, User):
return {
'id': obj.id,
'username': obj.username,
'email': obj.email,
'created_at': obj.created_at.isoformat()
}
elif isinstance(obj, datetime.datetime):
return obj.isoformat()
return super().default(obj)
# Tell Flask to use the custom encoder
app.json_encoder = CustomJSONEncoder
@app.route('/user')
def get_user():
user = User(1, 'johndoe', 'john@example.com', datetime.datetime.utcnow())
return jsonify(user)
if __name__ == '__main__':
app.run(debug=True)Explanation of the Example
User Class:
A simple class with
id,username,email, andcreated_atattributes.
CustomJSONEncoder:
Subclassed from
JSONEncoder.The
defaultmethod checks if the object is an instance ofUser. If so, it returns a dictionary representing theUserobject.It also checks for
datetimeobjects to serialize them in ISO format.Calls the superclass's
defaultmethod for any other types it can't handle.
Setting the Custom Encoder:
app.json_encoder = CustomJSONEncodertells Flask to useCustomJSONEncoderinstead of the default one.
Endpoint:
The
/userendpoint creates aUserobject and returns it usingjsonify.jsonifyuses the custom encoder to convert theUserobject into JSON format.
Running the Example
When you run the Flask app and navigate to /user, you'll get a JSON response like this:
This response includes the serialized User object, demonstrating how the custom encoder converts the custom object to JSON.
Last updated