LiteralMapping.mapper constructor

const LiteralMapping.mapper(
  1. Type mapperType
)

Creates a reference to a mapper that will be instantiated from the given type.

The generator will create an instance of mapperType to handle literal term mapping for this class. The type must implement LiteralTermMapper<T> where T is the annotated class and it must have a no-argument default constructor.

It will only be used for the Resource Mapper whose property is annotated with this mapping, not automatically be registered globally.

This approach is useful when the mapper has a default constructor and doesn't require additional configuration parameters.

Example:

@RdfGlobalResource(...)
class WeatherStation {
  // Using a custom mapper for a Temperature object
  @RdfProperty(
    WeatherSchema.temperature,
    literal: LiteralMapping.mapper(TemperatureMapper)
  )
  final Temperature temperature;
}

// The mapper implementation must be accessible to the generator:
class TemperatureMapper implements LiteralTermMapper<Temperature> {
  @override
  LiteralTerm toRdfTerm(Temperature temp, SerializationContext context) {
    return LiteralTerm('${temp.celsius}°C');
  }

  @override
  Temperature fromRdfTerm(LiteralTerm term, DeserializationContext context) {
    return Temperature(double.parse(term.value.replaceAll('°C', '')));
  }
}

Implementation

const LiteralMapping.mapper(Type mapperType)
    : language = null,
      datatype = null,
      super.mapper(mapperType);