Skip to content

Commit 126bcd2

Browse files
committed
Add codec for jsonpath type
PostgreSQL 12 added the `jsonpath` type to represent SQL/JSON values. Add a codec to handle that.
1 parent 22edda5 commit 126bcd2

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

codecs/__init__.pxd

+5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ cdef jsonb_encode(CodecContext settings, WriteBuffer buf, obj)
101101
cdef jsonb_decode(CodecContext settings, FRBuffer * buf)
102102

103103

104+
# JSON path
105+
cdef jsonpath_encode(CodecContext settings, WriteBuffer buf, obj)
106+
cdef jsonpath_decode(CodecContext settings, FRBuffer * buf)
107+
108+
104109
# Text
105110
cdef as_pg_string_and_size(
106111
CodecContext settings, obj, char **cstr, ssize_t *size)

codecs/jsonpath.pyx

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (C) 2016-present the asyncpg authors and contributors
2+
# <see AUTHORS file>
3+
#
4+
# This module is part of asyncpg and is released under
5+
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
6+
7+
8+
cdef jsonpath_encode(CodecContext settings, WriteBuffer buf, obj):
9+
cdef:
10+
char *str
11+
ssize_t size
12+
13+
as_pg_string_and_size(settings, obj, &str, &size)
14+
15+
if size > 0x7fffffff - 1:
16+
raise ValueError('string too long')
17+
18+
buf.write_int32(<int32_t>size + 1)
19+
buf.write_byte(1) # jsonpath format version
20+
buf.write_cstr(str, size)
21+
22+
23+
cdef jsonpath_decode(CodecContext settings, FRBuffer *buf):
24+
cdef uint8_t format = <uint8_t>(frb_read(buf, 1)[0])
25+
26+
if format != 1:
27+
raise ValueError('unexpected jsonpath format: {}'.format(format))
28+
29+
return text_decode(settings, buf)

pgproto.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ include "./codecs/datetime.pyx"
3737
include "./codecs/float.pyx"
3838
include "./codecs/int.pyx"
3939
include "./codecs/json.pyx"
40+
include "./codecs/jsonpath.pyx"
4041
include "./codecs/uuid.pyx"
4142
include "./codecs/numeric.pyx"
4243
include "./codecs/bits.pyx"

0 commit comments

Comments
 (0)