CollectionMapping.mapper constructor
- Type mapperType
Creates a reference to a collection mapper that will be instantiated from the given type.
Use this constructor when you want to provide your own custom collection mapper implementation that handles the entire collection serialization internally without needing access to item-level serializers/deserializers.
The generator will create an instance of mapperType to handle collection mapping.
The type must implement Mapper<C> where C is the collection type (e.g., List<T>)
and must have a no-argument default constructor.
Use this constructor when: Your collection mapper handles the entire collection
serialization internally (e.g., serializing a List<String> as a single JSON array
literal, or using a custom RDF structure that doesn't require item delegation).
Use CollectionMapping.withItemMappers() instead when: Your collection mapper
needs to delegate individual item serialization to the generated item mappers.
Example:
class Book {
// Using a collection mapper that handles entire list as single literal
@RdfProperty(
SchemaBook.keywords,
collection: CollectionMapping.mapper(StringListMapper)
)
final List<String> keywords; // Serialized as single JSON array literal
}
// The mapper implementation must be accessible to the generator:
class StringListMapper implements LiteralTermMapper<List<String>> {
StringListMapper(); // No-argument constructor required
@override
List<String> fromRdfTerm(LiteralTerm term, DeserializationContext context) {
// Parse JSON array from literal value
return (jsonDecode(term.value) as List).cast<String>();
}
@override
LiteralTerm toRdfTerm(List<String> value, SerializationContext context) {
// Serialize entire list as JSON array literal
return LiteralTerm(jsonEncode(value));
}
}
Implementation
const CollectionMapping.mapper(Type mapperType)
: isAuto = false,
factory = null,
super.mapper(mapperType);