Skip to content

RDFJS JavaScript code serializer that implements the RDFJS Sink interface

License

Notifications You must be signed in to change notification settings

rdfjs-base/serializer-rdfjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@rdfjs/serializer-rdfjs

build status

npm version

RDF/JS JavaScript code serializer that implements the RDF/JS Sink interface. It serializes the given quads to a JavaScript module that exports a single function, like shown below. The created function will return an array of the re-created quads.

export default ({ factory }) => {
  return [
    factory.quad(
      factory.blankNode('foo'),
      factory.namedNode('http://example.com/bar'),
      factory.literal('baz')
    )
  ]
}

Usage

The package exports the serializer as a class, so an instance must be created before it can be used. The .import method, as defined in the RDF/JS specification, must be called to do the actual serialization. It expects a quad stream as argument. The method will return a stream that emits the JavaScript code as a string.

Example

This example shows how to create a serializer instance and how to feed it with a stream of quads. The code emitted by the serializer will be written to stdout.

import { Readable } from 'stream'
import rdf from '@rdfjs/data-model'
import Serializer from '@rdfjs/serializer-rdfjs'


const serializer = new Serializer()
const input = new Readable({
  objectMode: true,
  read: () => {
    input.push(rdf.quad(
      rdf.namedNode('http://example.org/sheldon-cooper'),
      rdf.namedNode('http://schema.org/givenName'),
      rdf.literal('Sheldon')))
    input.push(rdf.quad(
      rdf.namedNode('http://example.org/sheldon-cooper'),
      rdf.namedNode('http://schema.org/familyName'),
      rdf.literal('Cooper')))
    input.push(rdf.quad(
      rdf.namedNode('http://example.org/sheldon-cooper'),
      rdf.namedNode('http://schema.org/knows'),
      rdf.namedNode('http://example.org/amy-farrah-fowler')))
    input.push(null)
  }
})

const output = serializer.import(input)
output.pipe(process.stdout)

Target module type

By default, the serializer will produce an ECMAScript module. The optional argument module can be used to serialize it to CommonJS or TypeScript.

Example

import Serializer from '@rdfjs/serializer-rdfjs'
import quadStream from './quads'

// serialize to CommonJS by default
const serializer = new Serializer({
  module: 'commonjs'
})

// call to .import also accepts same options parameter
const typescriptStream = serializer.import(quadStream, {
  module: 'ts'
})

transform(quads)

The actual serializer code runs sync, and the RDF/JS Sink interface is just a wrapper. If your use case is very specific, with a low chance to use other formats, it can be used directly. The .transform method accepts quads provided as an object that implements the Symbol.iterator method. It returns the generated JavaScript code as a string.

Example

This example shows how to create a serializer instance and how to feed it with quads. The returned code will be written to the console.

import rdf from '@rdfjs/data-model'
import Serializer from '@rdfjs/serializer-rdfjs'

const serializer = new Serializer()
const code = serializer.transform([
  rdf.quad(
    rdf.namedNode('http://example.org/sheldon-cooper'),
    rdf.namedNode('http://schema.org/givenName'),
    rdf.literal('Sheldon')),
  rdf.quad(
    rdf.namedNode('http://example.org/sheldon-cooper'),
    rdf.namedNode('http://schema.org/familyName'),
    rdf.literal('Cooper')),
  rdf.quad(
    rdf.namedNode('http://example.org/sheldon-cooper'),
    rdf.namedNode('http://schema.org/knows'),
    rdf.namedNode('http://example.org/amy-farrah-fowler'))
])

console.log(code)

About

RDFJS JavaScript code serializer that implements the RDFJS Sink interface

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •