Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rdf-serializer-jsonld

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rdf-serializer-jsonld - npm Package Compare versions

Comparing version 0.3.0 to 1.0.0

lib/SerializerStream.js

100

index.js

@@ -1,96 +0,10 @@

var rdf = require('rdf-ext')
var util = require('util')
var AbstractSerializer = require('rdf-serializer-abstract')
const SerializerStream = require('./lib/SerializerStream')
const Sink = require('rdf-sink')
function JsonLdSerializer (options) {
this.options = options || {}
AbstractSerializer.call(this, rdf)
class Serializer extends Sink {
constructor (options) {
super(SerializerStream, options)
}
}
util.inherits(JsonLdSerializer, AbstractSerializer)
JsonLdSerializer.prototype.serialize = function (graph, done) {
var self = this
return new Promise(function (resolve) {
done = done || function () {}
var jsonGraph = []
var subjects = {}
var subjectIndex = function (subject) {
var value = subject.nominalValue
if (typeof subjects[value] === 'undefined') {
if (subject.interfaceName === 'BlankNode') {
jsonGraph.push({ '@id': '_:' + value })
} else {
jsonGraph.push({ '@id': value })
}
subjects[value] = jsonGraph.length - 1
}
return subjects[value]
}
var objectValue = function (object) {
var value = object.nominalValue
if (object.interfaceName === 'NamedNode') {
return { '@id': value }
} else if (object.interfaceName === 'BlankNode') {
return { '@id': '_:' + value }
} else {
if (object.language) {
return { '@language': object.language, '@value': value }
} else if (object.datatype && !object.datatype.equals('http://www.w3.org/2001/XMLSchema#string')) {
return { '@type': object.datatype.nominalValue, '@value': value }
} else {
return value
}
}
}
graph.forEach(function (triple) {
var index = subjectIndex(triple.subject)
var predicateValue = triple.predicate.nominalValue
if (predicateValue === 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
if (typeof jsonGraph[index]['@type'] === 'undefined') {
jsonGraph[index]['@type'] = []
}
jsonGraph[index]['@type'].push(triple.object.nominalValue)
} else {
if (typeof jsonGraph[index][predicateValue] === 'undefined') {
jsonGraph[index][predicateValue] = objectValue(triple.object)
} else {
if (!Array.isArray(jsonGraph[index][predicateValue])) {
jsonGraph[index][predicateValue] = [jsonGraph[index][predicateValue]]
}
jsonGraph[index][predicateValue].push(objectValue(triple.object))
}
}
})
if (self.options.outputString) {
jsonGraph = JSON.stringify(jsonGraph)
}
done(null, jsonGraph)
resolve(jsonGraph)
})
}
// add singleton methods to class
var instance = new JsonLdSerializer()
for (var property in instance) {
JsonLdSerializer[property] = instance[property]
}
module.exports = JsonLdSerializer
module.exports = Serializer
{
"name": "rdf-serializer-jsonld",
"version": "0.3.0",
"description": "JSON-LD serializer that follows the RDF Interface specification",
"version": "1.0.0",
"description": "JSON-LD serializer that implements the RDFJS Sink Interfaces",
"main": "index.js",
"scripts": {
"lint": "standard",
"test": "node_modules/.bin/mocha"
"test": "standard && mocha",
"test-browser": "build-browser-test"
},

@@ -15,6 +15,8 @@ "repository": {

"keywords": [
"rdf-serializer",
"rdf",
"linked",
"data"
"rdfjs",
"rdf-ext",
"serializer",
"json-ld",
"stream"
],

@@ -28,11 +30,10 @@ "author": "Thomas Bergwinkl <bergi@axolotlfarm.org> (https://www.bergnet.org/people/bergi/card#me)",

"dependencies": {
"rdf-ext": "^0.3.0",
"rdf-serializer-abstract": "^0.3.0"
"rdf-sink": "^1.0.0",
"readable-stream": "^2.2.6"
},
"devDependencies": {
"jsonld": "^0.4.2",
"mocha": "^2.3.3",
"rdf-test-data": "^0.3.0",
"rdf-test-utils": "^0.3.0"
"mocha": "^3.2.0",
"rdf-ext": "^1.0.0",
"standard": "^9.0.2"
}
}
# rdf-serializer-jsonld
[![Build Status](https://travis-ci.org/rdf-ext/rdf-serializer-jsonld.svg?branch=master)](https://travis-ci.org/rdf-ext/rdf-serializer-jsonld)
[![NPM Version](https://img.shields.io/npm/v/rdf-serializer-jsonld.svg?style=flat)](https://npm.im/rdf-serializer-jsonld)
[![npm version](https://badge.fury.io/js/rdf-serializer-jsonld.svg)](https://badge.fury.io/js/rdf-serializer-jsonld)
JSON-LD serializer that follows the RDF Interface specification.
JSON-LD serializer that implements the [RDFJS Sink interface](https://github.com/rdfjs/representation-task-force/).
/* global describe, it */
var assert = require('assert')
var jsonld = require('jsonld')
var rdf = require('rdf-ext')
var testData = require('rdf-test-data')
var testUtils = require('rdf-test-utils')
var JsonLdSerializer = require('../')
var simpleGraph = rdf.createGraph()
const assert = require('assert')
const rdf = require('rdf-ext')
const sinkTest = require('rdf-sink/test')
const Readable = require('readable-stream')
const JsonLdSerializer = require('..')
simpleGraph.add(rdf.createTriple(
rdf.createNamedNode('http://example.org/subject'),
rdf.createNamedNode('http://example.org/predicate'),
rdf.createLiteral('object')
))
describe('rdf-serializer-jsonld', () => {
sinkTest(JsonLdSerializer, {readable: true})
describe('JSON-LD serializer', function () {
describe('instance API', function () {
describe('callback API', function () {
it('should be supported', function (done) {
var serializer = new JsonLdSerializer()
it('should serialize incoming quads', () => {
const quad1 = rdf.quad(
rdf.namedNode('http://example.org/subject'),
rdf.namedNode('http://example.org/predicate'),
rdf.literal('object1'))
Promise.resolve(new Promise(function (resolve, reject) {
serializer.serialize(simpleGraph, function (error) {
if (error) {
reject(error)
} else {
resolve()
}
})
})).then(function () {
done()
}).catch(function (error) {
done(error)
})
})
})
const quad2 = rdf.quad(
rdf.namedNode('http://example.org/subject'),
rdf.namedNode('http://example.org/predicate'),
rdf.literal('object2'),
rdf.namedNode('http://example.org/graph'))
describe('Promise API', function () {
it('should be supported', function (done) {
var serializer = new JsonLdSerializer()
const jsonld = [{
'@id': '@default',
'@graph': {
'@id': 'http://example.org/subject',
'http://example.org/predicate': 'object1'
}
}, {
'@id': 'http://example.org/graph',
'@graph': {
'@id': 'http://example.org/subject',
'http://example.org/predicate': 'object2'
}
}]
serializer.serialize(simpleGraph).then(function () {
done()
}).catch(function (error) {
done(error)
})
})
const input = new Readable()
input._readableState.objectMode = true
input._read = () => {
input.push(quad1)
input.push(quad2)
input.push(null)
}
const serializer = new JsonLdSerializer()
const stream = serializer.import(input)
return Promise.resolve().then(() => {
assert.deepEqual(stream.read(), jsonld)
return rdf.waitFor(stream)
})
})
describe('Stream API', function () {
it('should be supported', function (done) {
var serializer = new JsonLdSerializer()
var jsonGraph
it('should serialize rdf:type', () => {
const quad = rdf.quad(
rdf.namedNode('http://example.org/subject'),
rdf.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
rdf.namedNode('http://example.org/type'))
serializer.stream(simpleGraph).on('data', function (data) {
jsonGraph = data
}).on('end', function () {
if (!jsonGraph) {
done('no data streamed')
} else {
done()
}
}).on('error', function (error) {
done(error)
})
})
const jsonld = [{
'@id': '@default',
'@graph': {
'@id': 'http://example.org/subject',
'@type': 'http://example.org/type'
}
}]
const input = new Readable()
input._readableState.objectMode = true
input._read = () => {
input.push(quad)
input.push(null)
}
const serializer = new JsonLdSerializer()
const stream = serializer.import(input)
return Promise.resolve().then(() => {
assert.deepEqual(stream.read(), jsonld)
return rdf.waitFor(stream)
})
})
describe('static API', function () {
describe('callback API', function () {
it('should be supported', function (done) {
Promise.resolve(new Promise(function (resolve, reject) {
JsonLdSerializer.serialize(simpleGraph, function (error) {
if (error) {
reject(error)
} else {
resolve()
}
})
})).then(function () {
done()
}).catch(function (error) {
done(error)
})
})
it('should serialize blank node subjects', () => {
const quad = rdf.quad(
rdf.blankNode(),
rdf.namedNode('http://example.org/predicate'),
rdf.literal('object'))
const jsonld = [{
'@id': '@default',
'@graph': {
'@id': '_:b1',
'http://example.org/predicate': 'object'
}
}]
const input = new Readable()
input._readableState.objectMode = true
input._read = () => {
input.push(quad)
input.push(null)
}
const serializer = new JsonLdSerializer()
const stream = serializer.import(input)
return Promise.resolve().then(() => {
assert.deepEqual(stream.read(), jsonld)
return rdf.waitFor(stream)
})
})
describe('Promise API', function () {
it('should be supported', function (done) {
JsonLdSerializer.serialize(simpleGraph).then(function () {
done()
}).catch(function (error) {
done(error)
})
})
it('should serialize named node objects', () => {
const quad = rdf.quad(
rdf.namedNode('http://example.org/subject'),
rdf.namedNode('http://example.org/predicate'),
rdf.namedNode('http://example.org/object'))
const jsonld = [{
'@id': '@default',
'@graph': {
'@id': 'http://example.org/subject',
'http://example.org/predicate': {
'@id': 'http://example.org/object'
}
}
}]
const input = new Readable()
input._readableState.objectMode = true
input._read = () => {
input.push(quad)
input.push(null)
}
const serializer = new JsonLdSerializer()
const stream = serializer.import(input)
return Promise.resolve().then(() => {
assert.deepEqual(stream.read(), jsonld)
return rdf.waitFor(stream)
})
})
describe('Stream API', function () {
it('should be supported', function (done) {
var jsonGraph
it('should serialize blank node objects', () => {
const quad = rdf.quad(
rdf.namedNode('http://example.org/subject'),
rdf.namedNode('http://example.org/predicate'),
rdf.blankNode('b1'))
JsonLdSerializer.stream(simpleGraph).on('data', function (data) {
jsonGraph = data
}).on('end', function () {
if (!jsonGraph) {
done('no data streamed')
} else {
done()
}
}).on('error', function (error) {
done(error)
})
})
const jsonld = [{
'@id': '@default',
'@graph': {
'@id': 'http://example.org/subject',
'http://example.org/predicate': {
'@id': '_:b1'
}
}
}]
const input = new Readable()
input._readableState.objectMode = true
input._read = () => {
input.push(quad)
input.push(null)
}
const serializer = new JsonLdSerializer()
const stream = serializer.import(input)
return Promise.resolve().then(() => {
assert.deepEqual(stream.read(), jsonld)
return rdf.waitFor(stream)
})
})
describe('options', function () {
it('outputString should convert output to String', function (done) {
Promise.all([
(new JsonLdSerializer({outputString: false})).serialize(simpleGraph).then(function (serialized) {
assert.equal(typeof serialized, 'object')
}),
it('should serialize a language literal', () => {
const quad = rdf.quad(
rdf.namedNode('http://example.org/subject'),
rdf.namedNode('http://example.org/predicate'),
rdf.literal('object', 'en'))
(new JsonLdSerializer({outputString: true})).serialize(simpleGraph).then(function (serialized) {
assert.equal(typeof serialized, 'string')
})
]).then(function () {
done()
}).catch(function (error) {
done(error)
})
const jsonld = [{
'@id': '@default',
'@graph': {
'@id': 'http://example.org/subject',
'http://example.org/predicate': {
'@language': 'en',
'@value': 'object'
}
}
}]
const input = new Readable()
input._readableState.objectMode = true
input._read = () => {
input.push(quad)
input.push(null)
}
const serializer = new JsonLdSerializer()
const stream = serializer.import(input)
return Promise.resolve().then(() => {
assert.deepEqual(stream.read(), jsonld)
return rdf.waitFor(stream)
})
})
describe('example data', function () {
it('card should be serialized', function (done) {
var serializer = new JsonLdSerializer()
it('should serialize a datatype literal', () => {
const quad = rdf.quad(
rdf.namedNode('http://example.org/subject'),
rdf.namedNode('http://example.org/predicate'),
rdf.literal('object', rdf.namedNode('http://example.org/datatype')))
Promise.all([
serializer.serialize(testData.cardGraph).then(function (cardJson) {
return jsonld.promises().normalize(cardJson)
}),
const jsonld = [{
'@id': '@default',
'@graph': {
'@id': 'http://example.org/subject',
'http://example.org/predicate': {
'@type': 'http://example.org/datatype',
'@value': 'object'
}
}
}]
testUtils.readFile('support/card.json', __dirname).then(function (cardString) {
return jsonld.promises().normalize(JSON.parse(cardString))
})
]).then(function (results) {
assert.deepEqual(results[0], results[1])
}).then(function () {
done()
}).catch(function (error) {
done(error)
})
const input = new Readable()
input._readableState.objectMode = true
input._read = () => {
input.push(quad)
input.push(null)
}
const serializer = new JsonLdSerializer()
const stream = serializer.import(input)
return Promise.resolve().then(() => {
assert.deepEqual(stream.read(), jsonld)
return rdf.waitFor(stream)
})
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc