# rdflib.plugins.serializers package

## Submodules

## rdflib.plugins.serializers.hext module

HextuplesSerializer RDF graph serializer for RDFLib. See <https://github.com/ontola/hextuples> for details about the format.

class rdflib.plugins.serializers.hext.HextuplesSerializer(store)

  Bases: `Serializer`

  Serializes RDF graphs to NTriples format.

  Parameters:

    store (`Union`[`Graph`, `Dataset`, `ConjunctiveGraph`])

  __annotations__ = {'contexts': 'List[Union[Graph, IdentifiedNode]]', 'dumps': 'Callable'}
  __init__(store)
    Parameters:

      store (`Union`[`Graph`, `Dataset`, `ConjunctiveGraph`])

  __module__ = 'rdflib.plugins.serializers.hext'
  static __new__(cls, store)
    Parameters:

      store (`Union`[`Graph`, `Dataset`, `ConjunctiveGraph`])

  contexts: `List`[`Union`[`Graph`, `IdentifiedNode`]]
  dumps: `Callable`
  serialize(stream, base=None, encoding='utf-8', **kwargs)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

## rdflib.plugins.serializers.jsonld module

This serialiser will output an RDF Graph as a JSON-LD formatted document. See:

> http://json-ld.org/

Example usage:

    >>> from rdflib import Graph
    >>> testrdf = '''
    ... @prefix dc: <http://purl.org/dc/terms/> .
    ... <http://example.org/about>
    ...     dc:title "Someone's Homepage"@en .
    ... '''

    >>> g = Graph().parse(data=testrdf, format='n3')

    >>> print(g.serialize(format='json-ld', indent=2))
    [
      {
        "@id": "http://example.org/about",
        "http://purl.org/dc/terms/title": [
          {
            "@language": "en",
            "@value": "Someone's Homepage"
          }
        ]
      }
    ]

class rdflib.plugins.serializers.jsonld.JsonLDSerializer(store)

  Bases: `Serializer`

  Parameters:

    store (`Graph`)

  __annotations__ = {}
  __init__(store)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.jsonld'
  serialize(stream, base=None, encoding=None, **kwargs)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

rdflib.plugins.serializers.jsonld.from_rdf(graph, context_data=None, base=None, use_native_types=False, use_rdf_type=False, auto_compact=False, startnode=None, index=False)

## rdflib.plugins.serializers.longturtle module

LongTurtle RDF graph serializer for RDFLib. See <http://www.w3.org/TeamSubmission/turtle/> for syntax specification.

This variant, longturtle as opposed to just turtle, makes some small format changes to turtle - the original turtle serializer. It:

- uses PREFIX instead of @prefix

- uses BASE instead of @base

- adds a new line at RDF.type, or ‘a’

- adds a newline and an indent for all triples with more than one object (object list)

- adds a new line and ‘;’ for the last triple in a set with ‘.’

    on the start of the next line

- uses default encoding (encode()) is used instead of “latin-1”

- Nicholas Car, 2023

class rdflib.plugins.serializers.longturtle.LongTurtleSerializer(store)

  Bases: `RecursiveSerializer`

  __annotations__ = {}
  __init__(store)
  __module__ = 'rdflib.plugins.serializers.longturtle'
  addNamespace(prefix, namespace)
  doList(l_)
  endDocument()
  getQName(uri, gen_prefix=True)
  indentString = ' '
  isValidList(l_)

    Checks if l is a valid RDF list, i.e. no nodes have other properties.

  label(node, position)
  objectList(objects)
  p_default(node, position, newline=False)
  p_squared(node, position)
  path(node, position, newline=False)
  predicateList(subject, newline=False)
  preprocessTriple(triple)
  reset()
  s_default(subject)
  s_squared(subject)
  serialize(stream, base=None, encoding=None, spacious=None, **args)

    Abstract method

  short_name = 'longturtle'
  startDocument()
  statement(subject)
  verb(node, newline=False)

## rdflib.plugins.serializers.n3 module

Notation 3 (N3) RDF graph serializer for RDFLib.

class rdflib.plugins.serializers.n3.N3Serializer(store, parent=None)

  Bases: `TurtleSerializer`

  Parameters:

    store (`Graph`)

  __annotations__ = {}
  __init__(store, parent=None)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.n3'
  endDocument()
  getQName(uri, gen_prefix=True)
  indent(modifier=0)

    Returns indent string multiplied by the depth

  p_clause(node, position)
  path(node, position, newline=False)
  preprocessTriple(triple)
  reset()
  s_clause(subject)
  short_name = 'n3'
  statement(subject)

## rdflib.plugins.serializers.nquads module

class rdflib.plugins.serializers.nquads.NQuadsSerializer(store)

  Bases: `Serializer`

  Parameters:

    store (`Graph`)

  __annotations__ = {}
  __init__(store)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.nquads'
  serialize(stream, base=None, encoding=None, **args)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

## rdflib.plugins.serializers.nt module

class rdflib.plugins.serializers.nt.NTSerializer(store)

  Bases: `Serializer`

  Serializes RDF graphs to NTriples format.

  Parameters:

    store (`Graph`)

  __annotations__ = {}
  __init__(store)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.nt'
  serialize(stream, base=None, encoding='utf-8', **args)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

    Return type:

      `None`

## rdflib.plugins.serializers.patch module

class rdflib.plugins.serializers.patch.PatchSerializer(store)

  Bases: `Serializer`

  Creates an RDF patch file to add and remove triples/quads. Can either: - Create an add or delete patch for a single Dataset. - Create a patch to represent the difference between two Datasets.

  Parameters:

    store (`Dataset`)

  __annotations__ = {}
  __init__(store)
    Parameters:

      store (`Dataset`)

  __module__ = 'rdflib.plugins.serializers.patch'
  serialize(stream, base=None, encoding=None, **kwargs)

    Serialize the store to the given stream. :type stream: `IO`[`bytes`] :param stream: The stream to serialize to. :type base: `Optional`[`str`] :param base: The base URI to use for the serialization. :type encoding: `Optional`[`str`] :param encoding: The encoding to use for the serialization. :type kwargs: :param kwargs: Additional keyword arguments. Supported keyword arguments: - operation: The operation to perform. Either ‘add’ or ‘remove’. - target: The target Dataset to compare against. NB: Only one of ‘operation’ or ‘target’ should be provided. - header_id: The header ID to use. - header_prev: The previous header ID to use.

## rdflib.plugins.serializers.rdfxml module

class rdflib.plugins.serializers.rdfxml.PrettyXMLSerializer(store, max_depth=3)

  Bases: `Serializer`

  Parameters:

    store (`Graph`)

  __annotations__ = {}
  __init__(store, max_depth=3)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.rdfxml'
  predicate(predicate, object, depth=1)
    Parameters:

      - predicate (`Identifier`)

      - object (`Identifier`)

      - depth (`int`)

    Return type:

      `None`

  serialize(stream, base=None, encoding=None, **args)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

    Return type:

      `None`

  subject(subject, depth=1)
    Parameters:

      - subject (`Identifier`)

      - depth (`int`)

class rdflib.plugins.serializers.rdfxml.XMLSerializer(store)

  Bases: `Serializer`

  Parameters:

    store (`Graph`)

  __annotations__ = {}
  __init__(store)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.rdfxml'
  predicate(predicate, object, depth=1)
    Parameters:

      - predicate (`Identifier`)

      - object (`Identifier`)

      - depth (`int`)

    Return type:

      `None`

  serialize(stream, base=None, encoding=None, **args)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

    Return type:

      `None`

  subject(subject, depth=1)
    Parameters:

      - subject (`Identifier`)

      - depth (`int`)

    Return type:

      `None`

rdflib.plugins.serializers.rdfxml.fix(val)

  strip off _: from nodeIDs… as they are not valid NCNames

  Parameters:

    val (`str`)

  Return type:

    `str`

## rdflib.plugins.serializers.trig module

Trig RDF graph serializer for RDFLib. See <http://www.w3.org/TR/trig/> for syntax specification.

class rdflib.plugins.serializers.trig.TrigSerializer(store)

  Bases: `TurtleSerializer`

  Parameters:

    store (`Union`[`Graph`, `ConjunctiveGraph`])

  __annotations__ = {}
  __init__(store)
    Parameters:

      store (`Union`[`Graph`, `ConjunctiveGraph`])

  __module__ = 'rdflib.plugins.serializers.trig'
  indentString = ' '
  preprocess()
    Return type:

      `None`

  reset()
    Return type:

      `None`

  serialize(stream, base=None, encoding=None, spacious=None, **args)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

      - spacious (`Optional`[`bool`])

  short_name = 'trig'

## rdflib.plugins.serializers.trix module

class rdflib.plugins.serializers.trix.TriXSerializer(store)

  Bases: `Serializer`

  Parameters:

    store (`Graph`)

  __annotations__ = {}
  __init__(store)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.trix'
  serialize(stream, base=None, encoding=None, **args)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

## rdflib.plugins.serializers.turtle module

Turtle RDF graph serializer for RDFLib. See <http://www.w3.org/TeamSubmission/turtle/> for syntax specification.

class rdflib.plugins.serializers.turtle.RecursiveSerializer(store)

  Bases: `Serializer`

  Parameters:

    store (`Graph`)

  __annotations__ = {'roundtrip_prefixes': 'Tuple[Any, ...]'}
  __init__(store)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.turtle'
  addNamespace(prefix, uri)
    Parameters:

      - prefix (`str`)

      - uri (`URIRef`)

    Return type:

      `None`

  buildPredicateHash(subject)

    Build a hash key by predicate to a list of objects for the given subject

    Parameters:

      subject (`Node`)

    Return type:

      `Mapping`[`Node`, `List`[`Node`]]

  checkSubject(subject)

    Check to see if the subject should be serialized yet

    Parameters:

      subject (`Node`)

    Return type:

      `bool`

  indent(modifier=0)

    Returns indent string multiplied by the depth

    Parameters:

      modifier (`int`)

    Return type:

      `str`

  indentString = ' '
  isDone(subject)

    Return true if subject is serialized

    Parameters:

      subject (`Node`)

    Return type:

      `bool`

  maxDepth = 10
  orderSubjects()
    Return type:

      `List`[`Node`]

  predicateOrder = [rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label')]
  preprocess()
    Return type:

      `None`

  preprocessTriple(spo)
    Parameters:

      spo (`Tuple`[`Node`, `Node`, `Node`])

    Return type:

      `None`

  reset()
    Return type:

      `None`

  roundtrip_prefixes: `Tuple`[`Any`, `...`] = ()
  sortProperties(properties)

    Take a hash from predicate uris to lists of values. Sort the lists of values. Return a sorted list of properties.

    Parameters:

      properties (`Mapping`[`Node`, `List`[`Node`]])

    Return type:

      `List`[`Node`]

  subjectDone(subject)

    Mark a subject as done.

    Parameters:

      subject (`Node`)

    Return type:

      `None`

  topClasses = [rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Class')]
  write(text)

    Write text in given encoding.

    Parameters:

      text (`str`)

    Return type:

      `None`

class rdflib.plugins.serializers.turtle.TurtleSerializer(store)

  Bases: `RecursiveSerializer`

  Parameters:

    store (`Graph`)

  __annotations__ = {}
  __init__(store)
    Parameters:

      store (`Graph`)

  __module__ = 'rdflib.plugins.serializers.turtle'
  addNamespace(prefix, namespace)
    Parameters:

      - prefix (`str`)

      - namespace (`URIRef`)

    Return type:

      `str`

  doList(l_)
    Parameters:

      l_ (`Node`)

    Return type:

      `None`

  endDocument()
    Return type:

      `None`

  getQName(uri, gen_prefix=True)
    Parameters:

      - uri (`Node`)

      - gen_prefix (`bool`)

    Return type:

      `Optional`[`str`]

  indentString = ' '
  isValidList(l_)

    Checks if l is a valid RDF list, i.e. no nodes have other properties.

    Parameters:

      l_ (`Node`)

    Return type:

      `bool`

  label(node, position)
    Parameters:

      - node (`Node`)

      - position (`int`)

    Return type:

      `str`

  objectList(objects)
    Parameters:

      objects (`Sequence`[`Node`])

    Return type:

      `None`

  p_default(node, position, newline=False)
    Parameters:

      - node (`Node`)

      - position (`int`)

      - newline (`bool`)

    Return type:

      `bool`

  p_squared(node, position, newline=False)
    Parameters:

      - node (`Node`)

      - position (`int`)

      - newline (`bool`)

    Return type:

      `bool`

  path(node, position, newline=False)
    Parameters:

      - node (`Node`)

      - position (`int`)

      - newline (`bool`)

    Return type:

      `None`

  predicateList(subject, newline=False)
    Parameters:

      - subject (`Node`)

      - newline (`bool`)

    Return type:

      `None`

  preprocessTriple(triple)
    Parameters:

      triple (`Tuple`[`Node`, `Node`, `Node`])

    Return type:

      `None`

  reset()
    Return type:

      `None`

  s_default(subject)
    Parameters:

      subject (`Node`)

    Return type:

      `bool`

  s_squared(subject)
    Parameters:

      subject (`Node`)

    Return type:

      `bool`

  serialize(stream, base=None, encoding=None, spacious=None, **args)

    Abstract method

    Parameters:

      - stream (`IO`[`bytes`])

      - base (`Optional`[`str`])

      - encoding (`Optional`[`str`])

      - spacious (`Optional`[`bool`])

      - args (`Any`)

    Return type:

      `None`

  short_name = 'turtle'
  startDocument()
    Return type:

      `None`

  statement(subject)
    Parameters:

      subject (`Node`)

    Return type:

      `bool`

  verb(node, newline=False)
    Parameters:

      - node (`Node`)

      - newline (`bool`)

    Return type:

      `None`

## rdflib.plugins.serializers.xmlwriter module

class rdflib.plugins.serializers.xmlwriter.XMLWriter(stream, namespace_manager, encoding=None, decl=1, extra_ns=None)

  Bases: `object`

  Parameters:

    - stream (`IO`[`bytes`])

    - namespace_manager (`NamespaceManager`)

    - encoding (`Optional`[`str`])

    - decl (`int`)

    - extra_ns (`Optional`[`Dict`[`str`, `Namespace`]])

  __dict__ = mappingproxy({'__module__': 'rdflib.plugins.serializers.xmlwriter', '__init__': <function XMLWriter.__init__>, '_XMLWriter__get_indent': <function XMLWriter.__get_indent>, 'indent': <property object>, '_XMLWriter__close_start_tag': <function XMLWriter.__close_start_tag>, 'push': <function XMLWriter.push>, 'pop': <function XMLWriter.pop>, 'element': <function XMLWriter.element>, 'namespaces': <function XMLWriter.namespaces>, 'attribute': <function XMLWriter.attribute>, 'text': <function XMLWriter.text>, 'qname': <function XMLWriter.qname>, '__dict__': <attribute '__dict__' of 'XMLWriter' objects>, '__weakref__': <attribute '__weakref__' of 'XMLWriter' objects>, '__doc__': None, '__annotations__': {'element_stack': 'List[str]'}})
  __init__(stream, namespace_manager, encoding=None, decl=1, extra_ns=None)
    Parameters:

      - stream (`IO`[`bytes`])

      - namespace_manager (`NamespaceManager`)

      - encoding (`Optional`[`str`])

      - decl (`int`)

      - extra_ns (`Optional`[`Dict`[`str`, `Namespace`]])

  __module__ = 'rdflib.plugins.serializers.xmlwriter'
  __weakref__

    list of weak references to the object

  attribute(uri, value)
    Parameters:

      - uri (`str`)

      - value (`str`)

    Return type:

      `None`

  element(uri, content, attributes={})

    Utility method for adding a complete simple element

    Parameters:

      - uri (`str`)

      - content (`str`)

      - attributes (`Dict`[`URIRef`, `str`])

    Return type:

      `None`

  property indent: str
  namespaces(namespaces=None)
    Parameters:

      namespaces (`Iterable`[`Tuple`[`str`, `str`]])

    Return type:

      `None`

  pop(uri=None)
    Parameters:

      uri (`Optional`[`str`])

    Return type:

      `None`

  push(uri)
    Parameters:

      uri (`str`)

    Return type:

      `None`

  qname(uri)

    Compute qname for a uri using our extra namespaces, or the given namespace manager

    Parameters:

      uri (`str`)

    Return type:

      `str`

  text(text)
    Parameters:

      text (`str`)

    Return type:

      `None`

## Module contents