convert method
Converts an RDF graph to a JSON-LD string representation.
This method analyzes the graph structure and automatically determines the most appropriate JSON-LD representation:
- For empty graphs, it returns an empty JSON object
{} - For graphs with a single subject, it creates a single JSON-LD object with all properties of that subject
- For graphs with multiple subjects, it creates a JSON-LD document with
a top-level
@grapharray containing all subject nodes
The method also:
- Generates consistent labels for blank nodes
- Creates a
@contextobject with meaningful prefixes based on the graph content - Groups triples by subject for better structure
- Handles typed literals appropriately
graph The RDF graph to convert to JSON-LD.
baseUri Optional base URI for relative IRIs. When provided and
includeBaseDeclaration is true, it will be included in the @context.
Returns a formatted JSON-LD string with 2-space indentation.
Implementation
@override
String convert(RdfDataset dataset, {String? baseUri}) {
_log.fine('Serializing dataset to JSON-LD');
// Expanded mode: delegate to JsonLdExpandedSerializer.
if (_options.outputMode == JsonLdOutputMode.expanded) {
return _convertExpanded(dataset);
}
// Flattened mode: expand, then flatten, optionally compact.
if (_options.outputMode == JsonLdOutputMode.flattened) {
return _convertFlattened(dataset, baseUri: baseUri);
}
// Compact mode: W3C expand-then-compact pipeline.
return _convertCompactSpec(dataset, baseUri: baseUri);
}