Skip to content

Compatibility with orjson and msgspec #569

@lionel-panhaleux

Description

@lionel-panhaleux

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions