# rdf-serialize

> Serializes to any RDF serialization

Latest version **5.1.0** (published 2026-01-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install rdf-serialize
pnpm add rdf-serialize
yarn add rdf-serialize
bun add rdf-serialize
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 5.1.0 |
| Published | 2026-01-16 |
| First published | 2020-09-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 14 |
| Unpacked size | 38 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Ruben Taelman |
| Maintainers | rubensworks |
| Keywords | rdf, serialize, stream, rdfjs, linked data |

## Links

- npm: https://www.npmjs.com/package/rdf-serialize
- Repository: https://github.com/rubensworks/rdf-serialize.js
- Homepage: https://github.com/rubensworks/rdf-serialize.js#readme
- Issues: https://github.com/rubensworks/rdf-serialize.js/issues
- Funding: https://github.com/sponsors/rubensworks/
- npm.io page: https://npm.io/package/rdf-serialize

## Dependencies (14)

- [@rdfjs/types](https://npm.io/package/@rdfjs/types.md) *
- [@comunica/core](https://npm.io/package/@comunica/core.md) ^5.1.0
- [readable-stream](https://npm.io/package/readable-stream.md) ^4.5.2
- [stream-to-string](https://npm.io/package/stream-to-string.md) ^1.2.1
- [@comunica/bus-init](https://npm.io/package/@comunica/bus-init.md) ^5.1.0
- [@comunica/mediator-race](https://npm.io/package/@comunica/mediator-race.md) ^5.1.0
- [@comunica/context-entries](https://npm.io/package/@comunica/context-entries.md) ^5.1.0
- [@comunica/bus-rdf-serialize](https://npm.io/package/@comunica/bus-rdf-serialize.md) ^5.1.0
- [@comunica/config-query-sparql](https://npm.io/package/@comunica/config-query-sparql.md) ^5.0.0
- [@comunica/actor-rdf-serialize-n3](https://npm.io/package/@comunica/actor-rdf-serialize-n3.md) ^5.1.0
- [@comunica/mediator-combine-union](https://npm.io/package/@comunica/mediator-combine-union.md) ^5.1.0
- [@comunica/mediator-combine-pipeline](https://npm.io/package/@comunica/mediator-combine-pipeline.md) ^5.1.0
- [@comunica/actor-rdf-serialize-jsonld](https://npm.io/package/@comunica/actor-rdf-serialize-jsonld.md) ^5.1.0
- [@comunica/actor-rdf-serialize-shaclc](https://npm.io/package/@comunica/actor-rdf-serialize-shaclc.md) ^5.1.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 5.1.0 (latest) — 2026-01-16
- 5.0.0 — 2026-01-09
- 4.0.1 — 2024-10-15
- 4.0.0 — 2024-10-15
- 3.0.0 — 2024-07-04
- 2.2.3 — 2024-02-06
- 2.2.2 — 2023-02-27
- 2.2.1 — 2023-02-08
- 2.2.0 — 2023-02-01
- 2.1.0 — 2023-01-31
- 2.0.1 — 2022-11-09
- 2.0.0 — 2022-03-02
- 1.2.0 — 2021-08-30
- 1.1.0 — 2021-01-15
- 1.0.1 — 2020-11-12
- … 1 more at https://npm.io/package/rdf-serialize/versions

## README

# RDF Serialize

[![Build status](https://github.com/rubensworks/rdf-serialize.js/workflows/CI/badge.svg)](https://github.com/rubensworks/rdf-serialize.js/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/github/rubensworks/rdf-serialize.js/badge.svg?branch=master)](https://coveralls.io/github/rubensworks/rdf-serialize.js?branch=master)
[![npm version](https://badge.fury.io/js/rdf-serialize.svg)](https://www.npmjs.com/package/rdf-serialize)

This library serializes [RDF/JS](http://rdf.js.org/) quad streams to _RDF streams_ based on _content type_.

This is useful in situations where have a stream of RDF/JS quads,
and you want to _serialize_ them to a certain RDF serialization.

The following RDF serializations are supported:

| **Name**                                                                                                                                                                                 | **Content type** | **Extensions** |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| ---------------- | ------------- |
| [TriG 1.2](https://www.w3.org/TR/rdf12-trig/)                                                                                                                                            | `application/trig` | `.trig` |
| [N-Quads 1.2](https://www.w3.org/TR/rdf12-n-quads/)                                                                                                                                            | `application/n-quads` | `.nq`, `.nquads` |
| [Turtle 1.2](https://www.w3.org/TR/rdf12-turtle/)                                                                                                                                              | `text/turtle` | `.ttl`, `.turtle` |
| [N-Triples 1.2](https://www.w3.org/TR/rdf12-n-triples/)                                                                                                                                        | `application/n-triples` | `.nt`, `.ntriples` |
| [Notation3](https://www.w3.org/TeamSubmission/n3/)                                                                                                                                       | `text/n3` | `.n3` |
| [JSON-LD 1.1](https://json-ld.org/)                                                                                                                                                      | `application/ld+json`, `application/json` | `.json`, `.jsonld` |
| [SHACL Compact Syntax](https://w3c.github.io/shacl/shacl-compact-syntax/)                                                                                                                | `text/shaclc` | `.shaclc`, `.shc` |
| [Extended SHACL Compact Syntax](https://github.com/jeswr/shaclcjs#extended-shacl-compact-syntax)                                                                                         | `text/shaclc-ext` | `.shaclce`, `.shce` |

Internally, this library makes use of RDF serializers from the [Comunica framework](https://github.com/comunica/comunica),
which enable streaming processing of RDF.

Internally, the following fully spec-compliant serializers are used:

* [N3.js](https://github.com/rdfjs/n3.js)
* [jsonld-streaming-serializer.js](https://github.com/rubensworks/jsonld-streaming-serializer.js)
* [shaclc-writer](https://github.com/jeswr/shaclc-writer/)

## Installation

```bash
$ npm install rdf-serialize
```

or

```bash
$ yarn add rdf-serialize
```

This package also works out-of-the-box in browsers via tools such as [webpack](https://webpack.js.org/) and [browserify](http://browserify.org/).

## Require

```typescript
import  { rdfSerializer } from "rdf-serialize";
```

_or_

```javascript
const { rdfSerializer } = require("rdf-serialize");
```

## Usage

### Serializing by content type

The `rdfSerializer.serialize` method takes in an [RDFJS stream](http://rdf.js.org/stream-spec/#stream-interface) emitting RDF quads,
and an options object, and outputs  text stream containing RDF in a certain serialization.

```javascript
const { streamifyArray } = require('streamify-array');
const stringifyStream = require('stream-to-string');
const quad = require('rdf-quad');

const quadStream = streamifyArray([
  quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o1'),
  quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o2'),
]);

const textStream = rdfSerializer.serialize(quadStream, { contentType: 'text/turtle' });

// Handle the serialization in the streaming manner
textStream.pipe(process.stdout)
    .on('error', (error) => console.error(error))
    .on('end', () => console.log('All done!'));

// Or merge it in a single string.
console.log(await stringifyStream(textStream));
```

You may optionally pass prefixes to the serializer:

```javascript
rdfSerializer.serialize(quadStream, { contentType: 'text/turtle', prefixes: { ex: 'http://ex.org/' } });
```

### Serializing for file name

Sometimes, you know the desired path/URL of the serialized RDF document, but not the content type.
For those cases, this library allows you to provide the path/URL of the RDF document,
using which the content type will be determined.

For example, Turtle documents can be detected using the `.ttl` extension.

```javascript
const quadStream = streamifyArray([
  quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o1'),
  quad('http://ex.org/s', 'http://ex.org/p', 'http://ex.org/o2'),
]);

const textStream = rdfSerializer.serialize(quadStream, { path: 'http://example.org/myfile.ttl' });
```

### Getting all known content types

With `rdfSerializer.getContentTypes()`, you can retrieve a list of all content types for which a serializer is available.
Note that this method returns a promise that can be `await`-ed.

`rdfSerializer.getContentTypesPrioritized()` returns an object instead,
with content types as keys, and numerical priorities as values.

```javascript
// An array of content types
console.log(await rdfSerializer.getContentTypes());

// An object of prioritized content types
console.log(await rdfSerializer.getContentTypesPrioritized());
```

## License
This software is written by [Ruben Taelman](http://rubensworks.net/).

This code is released under the [MIT license](http://opensource.org/licenses/MIT).

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