Skip to content

# `` nquads

Classes:

- `NQuadsSerializer` –

  NQuads RDF graph serializer.

## `` __all__ `module-attribute`

    __all__ = ['NQuadsSerializer']

## `` NQuadsSerializer

    NQuadsSerializer(store: Graph)

Bases: `Serializer`

NQuads RDF graph serializer.

Methods:

- `serialize` –

Attributes:

- `store` (`ConjunctiveGraph`) –

Source code in `rdflib/plugins/serializers/nquads.py`

    17
    18
    19
    20
    21
    22
    23
    24

    def __init__(self, store: Graph):
        if not store.context_aware:
            raise Exception(
                "NQuads serialization only makes " "sense for context-aware stores!"
            )

        super(NQuadsSerializer, self).__init__(store)
        self.store: ConjunctiveGraph

### `` store `instance-attribute`

    store: ConjunctiveGraph

### `` serialize

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

Source code in `rdflib/plugins/serializers/nquads.py`

    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46

    def serialize(
        self,
        stream: IO[bytes],
        base: Optional[str] = None,
        encoding: Optional[str] = None,
        **kwargs: Any,
    ) -> None:
        if base is not None:
            warnings.warn("NQuadsSerializer does not support base.")
        if encoding is not None and encoding.lower() != self.encoding.lower():
            warnings.warn(
                "NQuadsSerializer does not use custom encoding. "
                f"Given encoding was: {encoding}"
            )
        encoding = self.encoding
        for context in self.store.contexts():
            for triple in context:
                stream.write(
                    _nq_row(triple, context.identifier).encode(encoding, "replace")
                )
        stream.write("\n".encode("latin-1"))