RdfLiteral.custom constructor
const
RdfLiteral.custom({})
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 aLiteralContentfromLiteralTermMethod: A static method that creates your object from aLiteralContentdatatype: 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();