RdfLiteral.custom constructor

const RdfLiteral.custom({
  1. required String toLiteralTermMethod,
  2. required String fromLiteralTermMethod,
  3. IriTerm? datatype,
})

Creates an annotation for a class using custom methods for literal conversion.

This approach allows you to define how your class is converted to/from RDF literals by specifying methods in your class:

  • toLiteralTermMethod: An instance method that converts your object to a LiteralContent
  • fromLiteralTermMethod: A static method that creates your object from a LiteralContent
  • datatype: Optional: The RDF datatype IRI to apply to generated literals.

This is ideal for classes that need special formatting or validation during serialization, such as formatted values with specific string representations (temperatures, currencies, structured values, etc.).

Example:

@RdfLiteral.custom(
  toLiteralTermMethod: 'formatCelsius',
  fromLiteralTermMethod: 'parse',
  datatype: Xsd.string
)
class Temperature {
  final double celsius;

  Temperature(this.celsius);

  // Instance method for serialization
  LiteralContent formatCelsius() => LiteralContent('$celsius°C');

  // Static method for deserialization
  static Temperature parse(LiteralContent term) =>
    Temperature(double.parse(term.value.replaceAll('°C', '')));
}

Implementation

const RdfLiteral.custom({
  required String toLiteralTermMethod,
  required String fromLiteralTermMethod,
  IriTerm? datatype,
})  : toLiteralTermMethod = toLiteralTermMethod,
      fromLiteralTermMethod = fromLiteralTermMethod,
      datatype = datatype,
      super();