Skip to content

Serializer

Overview

Serializer converts data into textual, binary or any other alternative representation. For example, if you want to transform records produced by the data service of the collection into MessagePack, you should probably use serializer.

Serializers are main users of columns service, because it contains details about specific data columns. And serializers often iterate data service directly(ignoring range method), to serialize all available records.

The only required method for serializer is serialize. This method must return an data from data service transformed into format provided by serializer. For example, JsonSerializer returns string with JSON-encoded data.

You are not restricted by textual or binary formats. Serializer that transforms data into pandas' DataFrame is completely valid version of the serializer.

Example

from array import array

class ArraySerializer(serialize.Serializer):
    def serialize(self):
        result = array("i")
        for item in self.attached.data:
            result.append(item)

        return result

col = collection.StaticCollection(
    serializer_factory=ArraySerializer,
    data_settings={"data": [1, 2, 3]}
)
assert col.serializer.serialize() == array("i", [1,2,3])

Available serializer factories

Serializer

Base collection serializer.

For any derived implementation, serialize must transfrom data of the collection into expected format.

Example
>>> class MySerializer(serialize.Serializer):
>>>     def stream(self):
>>>         for record in self.attached.data:
>>>             yield yaml.dump(record)
>>>
>>>     def serialize(self):
>>>         return "---\n".join(self.stream())

DictListSerializer

Convert data into a list of dictionaries.

Example
>>> col = collection.Collection(
>>>     serializer_factory=serialize.DictListSerializer,
>>>     columns_factory=columns.Columns.with_attributes(
>>>         names=["name", "sysadmin"],
>>>     ),
>>>     data_factory=data.ModelData,
>>>     data_settings={"model": model.User},
>>> )
>>> col.serializer.serialize()
[{'name': 'default', 'sysadmin': True}]

CsvSerializer

Serialize collection into CSV document.

Example
>>> col = collection.Collection(
>>>     serializer_factory=serialize.CsvSerializer,
>>>     columns_factory=columns.Columns.with_attributes(
>>>         names=["name", "sysadmin"],
>>>     ),
>>>     data_factory=data.ModelData,
>>>     data_settings={"model": model.User},
>>> )
>>> col.serializer.serialize()
name,sysadmin
default,True

JsonlSerializer

Serialize collection into JSONL lines.

ATTRIBUTE DESCRIPTION
encoder

json.JSONEncoder instance used for serialization

Example
>>> col = collection.Collection(
>>>     serializer_factory=serialize.JsonlSerializer,
>>>     columns_factory=columns.Columns.with_attributes(
>>>         names=["name", "sysadmin"],
>>>     ),
>>>     data_factory=data.ModelData,
>>>     data_settings={"model": model.User},
>>> )
>>> col.serializer.serialize()
{"name: "default", "sysadmin": true}
{"name: "normal_user", "sysadmin": false}

JsonSerializer

Serialize collection into single JSON document.

ATTRIBUTE DESCRIPTION
encoder

json.JSONEncoder instance used for serialization

Example
>>> col = collection.Collection(
>>>     serializer_factory=serialize.JsonSerializer,
>>>     columns_factory=columns.Columns.with_attributes(
>>>         names=["name", "sysadmin"],
>>>     ),
>>>     data_factory=data.ModelData,
>>>     data_settings={"model": model.User},
>>> )
>>> col.serializer.serialize()
[{"name: "default", "sysadmin": true},
 {"name: "normal_user", "sysadmin": false}]

HtmlSerializer

Serialize collection into HTML document.

ATTRIBUTE DESCRIPTION
main_template

path to Jinja2 template for the collection

TYPE: str

record_template

path to Jinja2 template for the individual record

TYPE: str

Example
>>> col = collection.Collection(
>>>     serializer_factory=serialize.HtmlSerializer,
>>>     data_factory=data.StaticData,
>>>     data_settings={"data": range(3)},
>>> )
>>> col.serializer.serialize()
<ul>
  <li>0</li>
  <li>1</li>
  <li>2</li>
</ul>

TableSerializer

Bases: HtmlSerializer[TDataCollection]

Serialize collection into HTML table.

ATTRIBUTE DESCRIPTION
main_template

path to template for the collection

TYPE: str

main_template

path to template for the table wrapper

TYPE: str

record_template

path to template for the individual record

TYPE: str

counter_template

path to template for the item counter

TYPE: str

pager_template

path to template for the table pagination widget

TYPE: str

form_template

path to template for the search form

TYPE: str

filter_template

path to template for filters block inside search form

TYPE: str

Example
>>> col = collection.Collection(
>>>     serializer_factory=serialize.TableSerializer,
>>>     columns_factory=columns.Columns.with_attributes(
>>>         names=["name", "sysadmin"],
>>>     ),
>>>     data_factory=data.ModelData,
>>>     data_settings={"model": model.User},
>>> )
>>> col.serializer.serialize()
<table>...</table>

HtmxTableSerializer

Bases: TableSerializer[TDataCollection]

Serialize collection into HTML table powered by HTMX.

Example
>>> col = collection.Collection(
>>>     serializer_factory=serialize.HtmxTableSerializer,
>>>     columns_factory=columns.Columns.with_attributes(
>>>         names=["name", "sysadmin"],
>>>     ),
>>>     data_factory=data.ModelData,
>>>     data_settings={"model": model.User},
>>> )
>>> col.serializer.serialize()
<table>...</table>