Why JSONField doesn't use Django's own JSON encoder?

While working on a project, I had to deal with storing some JSON data in my database. Data was produced by DRF’s Serializer, so resulting data was represented by dict, which then was stored in a JSONField. It worked fine until I had to serialize Decimal and UUID values in a JSONField. I found a solution, that was to simply set the JSONField.encoder to DjangoJSONEncoder (as suggested by documentation).

I assume it was a conscious design choice, but I’ve noticed that JSONField is using Python’s native json.JSONEncoder by default, which doesn’t support serializing Decimal and UUID values. I understand the differences between Python native json.JSONEncoder and DjangoJSONEncoder, and I’ve read its source code and documentation page, but so far I can’t exactly understand the “why” part, especially since the data types supported by DjangoJSONEncoder are used fairly frequently.

What is the reason for json.JSONEncoder being the default encoder instead of Django’s own encoder? What was the motivation behind this design choice?

1 Like

The most I could find was in the PR that added it, specifically this comment.

1 Like