Skip to content

Commit 2f2df7a

Browse files
committed
Add I/O support for jsonb type
1 parent 80934b7 commit 2f2df7a

4 files changed

Lines changed: 32 additions & 0 deletions

File tree

postgresql/test/test_driver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@
284284
['00:00:00:00:00:01', '00:00:00:00:00:00', 'ff:ff:ff:ff:ff:ff', '10:00:00:00:00:00'],
285285
],
286286
),
287+
('jsonb', [
288+
'{"foo": "bar", "spam": ["ham"]}'
289+
])
287290
]
288291

289292
try:

postgresql/types/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
REGDICTIONARYOID = 3769
3232

3333
JSONOID = 114
34+
JSONBOID = 3802
3435
XMLOID = 142
3536

3637
MACADDROID = 829

postgresql/types/io/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
pg_types.XMLOID,
8080
),
8181

82+
'stdlib_jsonb' : (
83+
pg_types.JSONBOID,
84+
),
85+
8286
# Must be db.typio.identify(contrib_hstore = 'hstore')'d
8387
'contrib_hstore' : (
8488
'contrib_hstore',
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from ...types import JSONBOID
2+
3+
4+
def jsonb_pack(x, typeio):
5+
jsonb = typeio.encode(x)
6+
return b'\x01' + jsonb
7+
8+
9+
def jsonb_unpack(x, typeio):
10+
if x[0] != 1:
11+
raise ValueError('unexpected JSONB format version: {!r}'.format(x[0]))
12+
return typeio.decode(x[1:])
13+
14+
15+
def _jsonb_io_factory(oid, typeio):
16+
_pack = lambda x: jsonb_pack(x, typeio)
17+
_unpack = lambda x: jsonb_unpack(x, typeio)
18+
19+
return (_pack, _unpack, str)
20+
21+
22+
oid_to_io = {
23+
JSONBOID: _jsonb_io_factory
24+
}

0 commit comments

Comments
 (0)