xsd prefix is created when this class is extended
more prefixes can be added by calling this.addPrefix(prefix: string, iri: string)
Blank node is created when a developer extends this class. Identifies the subject in triple
subject predicate object
This can be overridden in the constructor after calling super(); by calling this.makeBlankNode(id?: string) with optional id of the blank node
Used to create a triple with a resource identifier i.e ?subject a ?object
Add a prefix to the model, must be called in the constructor after calling super();
Throws an exception if the IRI does not end in '#' or '/' symbol
e.g. 'foaf'
Create a triple by providing subject predicate object
must be of type Triple_Subject
must be of type Triple_Predicate
must be of type Triple_Object
Create a literal with provided xsdDataType
a string or XSDDataType can be used
Convenience method to make predicate
in the form of IRI or prefixed IRI (e.g. xsd:type)
Convenience method to create RDFResourceIRI
This method must create a triple for each key:value pair in the provided json object
Example:
Provided the following was called in constructor this.addPrefix('address', 'http://example.com/Address/');
const triples: RDFTriple[] = [];
Object.keys(value).forEach(key => {
const predicate: RDFResourceIRI = this.makePredicate(`address:${key}`);
let obj: RDFLiteral;
// if value is a string
if (typeof (value[key]) === 'string') {
obj = this.makeLiteralWithDataType(value[key], 'xsd:string');
}
// If value is boolean
if (typeof (value[key]) === 'boolean') {
obj = this.makeLiteralWithDataType(value[key], 'xsd:boolean');
}
// if value is number
if (typeof (value[key]) === 'number') {
obj = this.makeLiteralWithDataType(value[key], 'xsd:integer');
}
triples.push(this.createTriple(this.subject, predicate, obj));
});
return triples;
json object passed i.e.
{
streetName: 'Queen St',
streetNumber: 223,
isRegistered: true
}
An array of triples where each triple in the array corresponds to key:value pairs of the provided json
Generated using TypeDoc
This class can be extended whenever you may want to serialize a value that is a json object
The value will be passed to the class that will extend AbstractBNodeSerializer
export interface UAddress { streetName: string; streetNumber: number; isRegistered: boolean; } export class AddressSerializer extends AbstractBNodeSerializer { constructor() { super(); this.addPrefix('address', 'http://example.com/Address/'); } serialize(value: Object): RDFTriple[] { const triples: RDFTriple[] = []; Object.keys(value).forEach(key => { const predicate: RDFResourceIRI = this.makePredicate(`address:${key}`); let obj: RDFLiteral; // if value is a string if (typeof (value[key]) === 'string') { obj = this.makeLiteralWithDataType(value[key], 'xsd:string'); } // If value is boolean if (typeof (value[key]) === 'boolean') { obj = this.makeLiteralWithDataType(value[key], 'xsd:boolean'); } // if value is number if (typeof (value[key]) === 'number') { obj = this.makeLiteralWithDataType(value[key], 'xsd:integer'); } triples.push(this.createTriple(this.subject, predicate, obj)); }); return triples; } } @RdfPrefixes({ foaf: 'http://xmlns.com/foaf/0.1/', user: 'http://example.com/User/' }) @RdfBean('foaf:User') export class User { @RdfSubject('user') public name: string; @RdfProperty({predicate: 'user:address', serializer: AddressSerializer}) public address: UAddress; } const u: User = new User(); u.name = 'Anton'; u.address = {streetName: 'Queen St', streetNumber: 223, isRegistered: true}; const r = RdfMapper.serialize(u);
Resulting turtle:
@prefix foaf: <http://xmlns.com/foaf/0.1/>. @prefix user: <http://example.com/User/>. @prefix xsd: <http://www.w3.org/2001/XMLSchema#>. @prefix address: <http://example.com/Address/>. user:Anton a foaf:User; user:address _:n3-2. _:n3-2 address:streetName "Queen St"^^xsd:string; address:streetNumber "223"^^xsd:integer; address:isRegistered "true"^^xsd:boolean.