-
-
Notifications
You must be signed in to change notification settings - Fork 208
Description
The fashionable orjson and msgspec libraries differ slightly from the standard and ujson libraries in the way they implement the dumps function: it returns bytes directly instead of a str object that requires UTF-8 encoding (which makes sense in terms of optimization).
They are very fast and efficient JSON libraries but right now to use them with psycopg, one needs to register a custom dumps function that returns a str, something like:
import orjson
import psycopg
def orjson_dumps(obj) → str:
return orjson.dumps(obj).decode()
psycopg.types.json.set_json_dumps(orjson_dumps)Which leads to a perfectly avoidable decode-encode chain in the end.
Psycopg already allows for a Union[str, bytes] parameter for loads, it would make sense to allow the same union for the return type of dumps, so that using orjson, msgspec would be more efficient and as simple as:
import orjson
import psycopg
psycopg.types.json.set_json_dumps(orjson.dumps)See PR #568 for an implementation proposal