TurtleEncoder class

Encoder for serializing RDF graphs to Turtle syntax.

The Turtle format (Terse RDF Triple Language) is a textual syntax for RDF that allows writing down RDF graphs in a compact and natural text form. This encoder implements the W3C Turtle recommendation, with additional optimizations for readability and compactness.

Features:

  • Automatic namespace prefix generation
  • Compact representation for blank nodes and collections
  • Proper indentation and formatting for readability
  • Support for base URI relative references
  • Special handling for common datatypes (integers, decimals, booleans)

Example usage:

import 'package:rdf_core/rdf_core.dart';

final graph = RdfGraph();
graph.add(Triple(
  IriTerm('http://example.org/subject'),
  IriTerm('http://example.org/predicate'),
  LiteralTerm('object')
));

final encoder = TurtleEncoder();
final turtle = encoder.convert(graph);
// Outputs: @prefix ex: <http://example.org/> .
//
// ex:subject ex:predicate "object" .

See: Turtle - Terse RDF Triple Language

NOTE: Always use canonical RDF vocabularies (e.g., http://xmlns.com/foaf/0.1/) with http://, not https:// This encoder will warn if it detects use of https:// for a namespace that is canonical as http://.

Inheritance

Constructors

TurtleEncoder({RdfNamespaceMappings? namespaceMappings, TurtleEncoderOptions options = const TurtleEncoderOptions()})
Creates a new Turtle encoder with the specified options.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

bind(Stream<RdfGraph> stream) Stream<String>
Transforms the provided stream.
inherited
cast<RS, RT>() Converter<RS, RT>
Provides a Converter<RS, RT> view of this stream transformer.
inherited
convert(RdfGraph graph, {String? baseUri}) String
Converts an RDF graph to a Turtle string representation.
override
fuse<TT>(Converter<String, TT> other) Converter<RdfGraph, TT>
Fuses this with other.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
startChunkedConversion(Sink<String> sink) Sink<RdfGraph>
Starts a chunked conversion.
inherited
toString() String
A string representation of this object.
inherited
withOptions(RdfGraphEncoderOptions options) RdfGraphEncoder
Creates a new encoder with the specified options, preserving the current namespace mappings.
override
writeTerm(RdfTerm term, {Map<String, String> prefixesByIri = const {}, Map<BlankNodeTerm, String> blankNodeLabels = const {}, String? baseUri, bool isPredicate = false}) String
Convert RDF terms to Turtle syntax string representation

Operators

operator ==(Object other) bool
The equality operator.
inherited