@Dumper.text(Json)
+@Dumper.binary(Json)
class JsonDumper(_JsonDumper):
_oid = JSON_OID
@Dumper.text(JsonB)
class JsonBDumper(_JsonDumper):
_oid = JSONB_OID
+
+
+@Dumper.binary(JsonB)
+class BinaryJsonBDumper(JsonBDumper):
+ def dump(
+ self, obj: _JsonWrapper, __encode: EncodeFunc = _encode_utf8
+ ) -> bytes:
+ return b"\x01" + __encode(obj.dumps())[0]
import pytest
from psycopg3.types.json import Json, JsonB
+from psycopg3.adapt import Format
samples = [
"null",
@pytest.mark.parametrize("val", samples)
-def test_json_dump(conn, val):
+@pytest.mark.parametrize("fmt_in", [Format.TEXT, Format.BINARY])
+def test_json_dump(conn, val, fmt_in):
+ ph = "%s" if fmt_in == Format.TEXT else "%b"
obj = json.loads(val)
cur = conn.cursor()
- cur.execute("select pg_typeof(%s) = 'json'::regtype", (Json(obj),))
+ cur.execute(f"select pg_typeof({ph}) = 'json'::regtype", (Json(obj),))
assert cur.fetchone()[0] is True
- cur.execute("select %s::text = %s::json::text", (Json(obj), val))
+ cur.execute(f"select {ph}::text = %s::json::text", (Json(obj), val))
assert cur.fetchone()[0] is True
+@pytest.mark.parametrize("fmt_in", [Format.TEXT, Format.BINARY])
@pytest.mark.parametrize("val", samples)
-def test_jsonb_dump(conn, val):
+def test_jsonb_dump(conn, val, fmt_in):
+ ph = "%s" if fmt_in == Format.TEXT else "%b"
obj = json.loads(val)
cur = conn.cursor()
- cur.execute("select %s = %s::jsonb", (JsonB(obj), val))
+ cur.execute(f"select {ph} = %s::jsonb", (JsonB(obj), val))
assert cur.fetchone()[0] is True