# @rdfjs/formats

> Bundle of RDF/JS parsers and serializers

Latest version **4.0.1** (published 2025-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rdfjs/formats
pnpm add @rdfjs/formats
yarn add @rdfjs/formats
bun add @rdfjs/formats
```

## Health

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

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

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2025-01-10 |
| First published | 2023-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/rdfjs__formats) |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 19 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Thomas Bergwinkl |
| Maintainers | bergos |
| Keywords | rdf, rdfjs, format, parser, serializer |

## Links

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

## Dependencies (8)

- [@rdfjs/sink-map](https://npm.io/package/@rdfjs/sink-map.md) ^2.0.0
- [@rdfjs/parser-n3](https://npm.io/package/@rdfjs/parser-n3.md) ^2.0.1
- [@rdfjs/parser-jsonld](https://npm.io/package/@rdfjs/parser-jsonld.md) ^2.1.0
- [rdfxml-streaming-parser](https://npm.io/package/rdfxml-streaming-parser.md) ^3.0.1
- [@rdfjs/serializer-jsonld](https://npm.io/package/@rdfjs/serializer-jsonld.md) ^2.0.0
- [@rdfjs/serializer-turtle](https://npm.io/package/@rdfjs/serializer-turtle.md) ^1.1.1
- [@rdfjs/serializer-ntriples](https://npm.io/package/@rdfjs/serializer-ntriples.md) ^2.0.0
- [@rdfjs/serializer-jsonld-ext](https://npm.io/package/@rdfjs/serializer-jsonld-ext.md) ^4.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 4.0.1 (latest) — 2025-01-10
- 4.0.0 — 2023-11-18

## README

# @rdfjs/formats

[![build status](https://img.shields.io/github/actions/workflow/status/rdfjs-base/formats/test.yaml?branch=master)](https://github.com/rdfjs-base/formats/actions/workflows/test.yaml)
[![npm version](https://img.shields.io/npm/v/@rdfjs/formats.svg)](https://www.npmjs.com/package/@rdfjs/formats)

This module bundles parsers and serializers for the most common RDF formats.
Instances of [SinkMap](https://github.com/rdfjs-base/sink-map) are used to handle different media types.

## Usage

The formats object has a `parsers` and `serializers` property.
Each is an instance of `SinkMap` with the most common RDF media types as key.
The formats object is exported as default and can be imported like this:

```javascript
import formats from '@rdfjs/formats'
```

### Pretty-Print Serializers

The default bundle of serializers is optimized for streaming.
The pretty-print formats bundle can be used if a more human-readable output is required.
It can be imported like this:

### Factory

A factory that takes care of parsers and serializers.
An additional `.formats` object will be attached to the environment.
That object is compatible with the data structure of [@rdfjs/formats-common](https://github.com/rdfjs-base/formats-common).
The `.formats` object has an additional `.import()` function to import other format bundles.
The following code shows how to import the [@rdfjs/formats-common](https://github.com/rdfjs-base/formats-common) bundle:


```javascript
import formats from '@rdfjs/formats/pretty.js'
```

## Example

The following example uses the parser for the media type `text/turtle` to parse the given string.
The quads emitted by the `data` event are written to the console:

```javascript
import formats from '@rdfjs/formats'
import { Readable } from 'readable-stream'

const input = Readable.from([`
  PREFIX s: <http://schema.org/>

  [] a s:Person;
    s:jobTitle "Professor";
    s:name "Jane Doe";
    s:telephone "(425) 123-4567";
    s:url <http://www.janedoe.com>.
`])

const output = formats.parsers.import('text/turtle', input)

output.on('data', quad => {
  console.log(`quad: ${quad.subject.value} - ${quad.predicate.value} - ${quad.object.value}`)
})

output.on('prefix', (prefix, ns) => {
  console.log(`prefix: ${prefix} ${ns.value}`)
})
```

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