RdfLiteral constructor

const RdfLiteral([
  1. IriTerm? datatype,
  2. bool registerGlobally = true,
  3. MapperDirection direction = MapperDirection.both
])

Creates an annotation for a class or enum to be mapped to a literal term.

This standard constructor creates a mapper that automatically handles the conversion between your type and an RDF literal term. By default, this mapper is registered within initRdfMapper when registerGlobally is true.

For Classes

The mapper works by:

  1. Looking for a property in your class marked with @RdfValue
  2. Using that property to create a literal value during serialization
  3. Using that property's type and the constructor to deserialize values

This is the simplest approach for value objects that wrap a single literal value.

For Enums

When applied to enums, generates an automatic literal mapper that:

  • Uses enum constant names as literal values by default
  • Respects @RdfEnumValue annotations for custom values
  • Handles bidirectional conversion between enum constants and literals

Parameters

datatype - Optional RDF datatype IRI to apply to generated literals. If not specified, the datatype is inferred from the Dart type.

registerGlobally - Whether to register the generated mapper in initRdfMapper. Set to false if the mapper should be registered manually or used at the property level instead.

Example with class:

@RdfLiteral()
class Rating {
  @RdfValue() // Marks this property as the source for the literal value
  final int stars;

  Rating(this.stars) {
    if (stars < 0 || stars > 5) {
      throw ArgumentError('Rating must be between 0 and 5 stars');
    }
  }
}

Example with enum:

@RdfLiteral() // Uses enum names as literal values
enum Priority {
  @RdfEnumValue('H') // Custom literal value
  high,              // → "H"
  medium,            // → "medium" (uses enum name)
  low,               // → "low" (uses enum name)
}

Implementation

const RdfLiteral(
    [this.datatype,
    bool registerGlobally = true,
    MapperDirection direction = MapperDirection.both])
    : toLiteralTermMethod = null,
      fromLiteralTermMethod = null,
      super(registerGlobally: registerGlobally, direction: direction);