Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@virtual-assembly/semantizer

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@virtual-assembly/semantizer

The semantizer library lets you enhance your object model with semantic data.

Source
npmnpm
Version
1.0.0-alpha
Version published
Weekly downloads
36
140%
Maintainers
1
Weekly downloads
 
Created
Source

Semantizer lets you enhance your object model with semantic data without breaking existing code. Just inherit from the SemanticObject class to get the ability to mark some properties of your object as "semantic". You can add to your object as any semantic properties as you want. These semantic properties can then be serialized to any output format, like JSON-LD.

This library was writen for the Data Food Consortium project (DFC) which aims to provide interoperability between food supply chain platforms. We use the semantizer library inside our connector library to help developers to exchange JSON-LD data expressed with the DFC ontology.

Get started

Lets take an example, with a simple Person class:

class Person {

    private name: string;

    constructor(name: string) {
        this.name = name;
    }

    public getName(): string {
        return this.name;
    }
}

Inherit from the SemanticObject class and add this two following lines at the end of the constructor method:

this.setSemanticType("http://xmlns.com/foaf/0.1/Person");
this.registerSemanticValue("http://xmlns.com/foaf/0.1/name", () => this.getName());

The Person class should now looks like:

class Person extends SemanticObject {

    private name: string;

    constructor(name: string) {
        super(); // call SemanticObject constructor
        this.name = name;
        this.setSemanticType("http://xmlns.com/foaf/0.1/Person");
        this.registerSemanticProperty("http://xmlns.com/foaf/0.1/name", () => this.getName());
    }

    public getName(): string {
        return this.name;
    }
}

Then you can serialize this object to a simple regular JavaScript object:

const person = new Person("John");
person.setSemanticId("http://platform.example/John");
const output = person.serialize(new ObjectSerializer));

console.log(output) will output:

{
  '@id': 'http://platform.example/John',
  '@type': 'http://xmlns.com/foaf/0.1/Person',
  'http://xmlns.com/foaf/0.1/name': 'John'
}

You can then use a library like jsonld to add a semantic context like:

import jsonld from 'jsonld';

let context = {
    "foaf": "http://xmlns.com/foaf/0.1/"
};

const compacted = await jsonld.compact(output, context);
console.log(JSON.stringify(compacted, null, 2));

This will output a contextualized JSON-LD text:

{
  "@context": {
    "foaf": "http://xmlns.com/foaf/0.1/"
  },
  "@id": "http://platform.example/John",
  "@type": "foaf:Person",
  "foaf:name": "John"
}

FAQs

Package last updated on 25 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts