popoto.backends.postgres.codec¶
popoto.backends.postgres.codec
¶
jsonb encoding for the collection fields on Postgres (#759 M1.1).
ListField, DictField, SetField and TupleField are stored as
jsonb -- never msgpack (plan ยง8). The contract is that a value comes back
exactly as it comes back from Redis, so the encoding reproduces what msgpack
plus popoto's tagged-dict registry (TYPE_ENCODER_DECODERS) round-trip, and
nothing more:
- The top level is typed by the column: a
list/tuple/setfield is a JSON array, re-typed on the way out by the field's own type (on Redis the registry tags a top-level tuple or set for the same reason). - Nested values behave as msgpack does: a nested
tuplecomes back alist; a tagged dict ({"__Decimal__": True, "as_encodable": ...}, written byCappedListProxy.pushand capped-list saves per element) decodes to its type at any depth, asdecode_custom_typesdoes as msgpack's object hook; and a value msgpack cannot pack (a nestedset,Decimal,datetime...) raisesTypeErrorhere too. - What JSON lacks and msgpack has is tagged so it survives:
bytes(base64), a non-finitefloatand adictwith a non-strkey. These three tags are this module's own and never reach Redis.
Pure: no network, no psycopg.
encode_json_element(value)
¶
One element of a capped ListField: tagged through popoto's type
registry first, exactly as _encode_list_element does for Redis's
per-element RPUSH/LPUSH.
Source code in src/popoto/backends/postgres/codec.py
encode_json(py_type, value, *, capped=False)
¶
The JSON document stored for a collection field's value.
Source code in src/popoto/backends/postgres/codec.py
decode_json(py_type, value)
¶
A stored JSON document back as the field's Python value.