# @rdfjs/serializer-rdfjs

> RDF/JS JavaScript code serializer that implements the RDF/JS Sink interface

Latest version **0.1.3** (published 2024-06-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rdfjs/serializer-rdfjs
pnpm add @rdfjs/serializer-rdfjs
yarn add @rdfjs/serializer-rdfjs
bun add @rdfjs/serializer-rdfjs
```

## Health

**Score 38/100 (D)** — status: abandoned.

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2024-06-06 |
| First published | 2020-06-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/rdfjs__serializer-rdfjs) |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 26.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Thomas Bergwinkl |
| Maintainers | bergos |
| Keywords | rdf, rdfjs, serializer |

## Links

- npm: https://www.npmjs.com/package/@rdfjs/serializer-rdfjs
- Repository: https://github.com/rdfjs-base/serializer-rdfjs
- Issues: https://github.com/rdfjs-base/serializer-rdfjs/issues
- npm.io page: https://npm.io/package/@rdfjs/serializer-rdfjs

## Dependencies (4)

- [@rdfjs/sink](https://npm.io/package/@rdfjs/sink.md) ^2.0.0
- [stream-chunks](https://npm.io/package/stream-chunks.md) ^1.0.0
- [readable-stream](https://npm.io/package/readable-stream.md) ^4.3.0
- [js-string-escape](https://npm.io/package/js-string-escape.md) ^1.0.1

## Recent versions

- 0.1.3 (latest) — 2024-06-06
- 0.1.2 — 2024-03-04
- 0.1.1 — 2023-03-10
- 0.1.0 — 2021-12-18
- 0.0.4 — 2020-10-21
- 0.0.3 — 2020-09-08
- 0.0.2 — 2020-07-20
- 0.0.1 — 2020-06-14

## README

# @rdfjs/serializer-rdfjs

[![build status](https://img.shields.io/github/actions/workflow/status/rdfjs-base/serializer-rdfjs/test.yaml?branch=master)](https://github.com/rdfjs-base/serializer-rdfjs/actions/workflows/test.yaml)

[![npm version](https://img.shields.io/npm/v/@rdfjs/serializer-rdfjs.svg)](https://www.npmjs.com/package/@rdfjs/serializer-rdfjs)

RDF/JS JavaScript code serializer that implements the [RDF/JS Sink interface](http://rdf.js.org/).
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. 

```javascript
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](http://rdf.js.org/#sink-interface), 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.

```javascript
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

```js
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.

```javascript
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)
```

---
_Source: https://npm.io/package/@rdfjs/serializer-rdfjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
