JsonLdEncoder class final
Encoder for converting RDF graphs to JSON-LD format.
JSON-LD is a lightweight Linked Data format that is easy for humans to read and write and easy for machines to parse and generate. This serializer transforms RDF data into compact, structured JSON documents by:
- Grouping triples by subject
- Creating a @context section for namespace prefixes
- Nesting objects for more readable representation
- Handling different RDF term types appropriately
Graph Structure Handling
The encoder automatically detects the structure of the input graph:
-
Single Subject: When the graph contains triples with only one subject, the output is a single JSON-LD object with properties representing predicates.
-
Multiple Subjects: When the graph contains triples with multiple subjects, the encoder generates a JSON-LD document with a top-level
@graph
array containing all subject nodes. This produces more readable output by structuring the data naturally.
@graph and Named Graphs
Note that the current implementation does not support true RDF Datasets with
named graphs. When outputting a graph with multiple subjects as @graph
,
this does not represent different named graphs but rather is a structural
device for organizing multiple nodes in the default graph.
In JSON-LD, a top-level @graph
array can be used for two different purposes:
- As a way to organize multiple unrelated nodes (current implementation)
- As a way to represent named graphs in an RDF dataset (future enhancement)
Datatype Handling
The encoder handles various RDF literal types and automatically converts them to appropriate JSON representations:
- String literals are represented as JSON strings
- Integer literals are converted to JSON numbers when possible
- Boolean literals are converted to JSON booleans when possible
- Other datatypes use the
@value
and@type
syntax
Configuration Options
The serializer produces compacted JSON-LD by default, using prefixes to make property names more readable. Customizations are possible through namespace mappings and encoder options.
- Inheritance
-
- Object
- Converter<
RdfGraph, String> - RdfGraphEncoder
- JsonLdEncoder
Constructors
- JsonLdEncoder({RdfNamespaceMappings? namespaceMappings, JsonLdEncoderOptions options = const JsonLdEncoderOptions()})
-
Creates a new JSON-LD serializer.
const
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 JSON-LD string representation.
override
-
fuse<
TT> (Converter< String, TT> other) → Converter<RdfGraph, TT> -
Fuses
this
withother
.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 instance with the specified options.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited