Skip to content

# `` serializer

Serializer plugin interface.

The RDF serialization plugins used during RDFlib’s common serialization of a graph is described in this module.

TODO: info for how to write a serializer that can plugin to rdflib. See also `rdflib.plugin`

All builtin RDF Serializer are listed under `rdflib.plugins.serializers`.

Classes:

- `Serializer` –

  Base class for RDF Serializer.

## `` __all__ `module-attribute`

    __all__ = ['Serializer']

## `` Serializer

    Serializer(store: Graph)

Base class for RDF Serializer.

New Serializer can be registered as plugin as described in `rdflib.plugin`.

Methods:

- `relativize` –
- `serialize` –

  Abstract method. Print Graph.

Attributes:

- `base` (`Optional[str]`) –
- `encoding` (`str`) –
- `store` (`Graph`) –

Source code in `rdflib/serializer.py`

    36
    37
    38
    39

    def __init__(self, store: Graph):
        self.store: Graph = store
        self.encoding: str = "utf-8"
        self.base: Optional[str] = None

### `` base `instance-attribute`

    base: Optional[str] = None

### `` encoding `instance-attribute`

    encoding: str = 'utf-8'

### `` store `instance-attribute`

    store: Graph = store

### `` relativize

    relativize(uri: _StrT) -> Union[_StrT, URIRef]

Source code in `rdflib/serializer.py`

    60
    61
    62
    63
    64
    65

    def relativize(self, uri: _StrT) -> Union[_StrT, URIRef]:
        base = self.base
        if base is not None and uri.startswith(base):
            # type error: Incompatible types in assignment (expression has type "str", variable has type "Node")
            uri = URIRef(uri.replace(base, "", 1))  # type: ignore[assignment]
        return uri

### `` serialize

    serialize(stream: IO[bytes], base: Optional[str] = None, encoding: Optional[str] = None, **args: Any) -> None

Abstract method. Print Graph. Used in Graph.serialize

Serialize Graph

Parameters:

- #### `stream`

  (`IO[bytes]`) –

  The destination to serialize the graph to.

- #### `base`

  (`Optional[str]`, default: `None` ) –

  The base IRI for formats that support it.

- #### `encoding`

  (`Optional[str]`, default: `None` ) –

  Encoding of output.

- #### `args`

  (`Any`, default: `{}` ) –

  Additional arguments to pass to the Serializer that will be used.

Source code in `rdflib/serializer.py`

    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58

    def serialize(
        self,
        stream: IO[bytes],
        base: Optional[str] = None,
        encoding: Optional[str] = None,
        **args: Any,
    ) -> None:
        """Abstract method. Print [Graph][rdflib.graph.Graph].
        Used in [Graph.serialize][rdflib.graph.Graph.serialize]

        Serialize Graph

        Args:
            stream: The destination to serialize the graph to.
            base: The base IRI for formats that support it.
            encoding: Encoding of output.
            args: Additional arguments to pass to the Serializer that will be used.
        """