UNPKG

lincd-jsonld

Version:

Utilities to parse and write JSON-LD for the LINCD library

352 lines 15.4 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; /* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { BlankNode, Graph, Literal, NamedNode, Quad } from 'lincd/models'; import { QuadSet } from 'lincd/collections/QuadSet'; import { NodeSet } from 'lincd/collections/NodeSet'; import { ShapeSet } from 'lincd/collections/ShapeSet'; import { Shape } from 'lincd/shapes/Shape'; import { CoreMap } from 'lincd/collections/CoreMap'; import { CoreSet } from 'lincd/collections/CoreSet'; import { JSONLDWriter } from './JSONLDWriter.js'; import { linkedUtil } from '../package.js'; import { QuadArray } from 'lincd/collections/QuadArray'; let URIMappings = class URIMappings { constructor(map = []) { this.map = map; } addMapping(mapping) { this.map.push(mapping); } }; URIMappings = __decorate([ linkedUtil, __metadata("design:paramtypes", [Array]) ], URIMappings); export { URIMappings }; let JSONWriter = class JSONWriter { static toJsObject(object, includeData = true, includeGraphs = true, includeLocalResources = true, uriMappings = new URIMappings(), includeIncomingProperties = false) { var data; if (includeData === true) { data = new QuadSet(); } else if (includeData instanceof QuadSet) { data = includeData; } return this.toJsObjectInternal(object, data, includeIncomingProperties, includeGraphs).then((jsObject) => { if (data && data.size) { if (!includeLocalResources) { data = data.filter((quad) => !quad.subject.isTemporaryNode && !(quad.object instanceof NamedNode && quad.object.isTemporaryNode)); } return JSONLDWriter.fromQuads(data, true, includeGraphs).then((jsonldData) => { let res = { __n: jsObject, __data: jsonldData, }; if (uriMappings) { res['__map'] = JSON.stringify(uriMappings.map); } return res; }); } return jsObject; }); } /** * Convert any object into a json string. The object can be or include framework native objects like NamedNodes, Instances, Maps and Sets * The resulting string is meant to be consumed by the JSONParser class * WARNING: when object is a quadset that contain removed quads will not convert well. Use convertQuadSetNatively for this * @param object * @param includeData - data of properties of resources and instances in the object will be included in the resulting string * @param includeLocalResources - if false, temporary local resources not yet stored in the graph will not be included * @param mappings * @param includeIncomingProperties - if true, data of incoming properties of resources and instances will also be included */ static stringify(object, includeData = false, includeGraphs = true, includeLocalResources = true, mappings = null, includeIncomingProperties = false) { return this.toJsObject(object, includeData, includeGraphs, includeLocalResources, mappings, includeIncomingProperties) .then((jsObject) => { return JSON.stringify(jsObject); }) .catch((e) => { throw new Error('Could not convert object to JSON:' + e.stack); }); } /** * A fast performing native implementation to convert a QuadSet to a plain js object, * which MAINTAINS extra information about the quad, like whether its removed or not * (which is not the case if it would be converted to jsonld) * The result is ready to be stringified by JSON * Use this for example if you want to send quad CHANGES across environments, which will include both removed and non-removed quads * @param quadset * @private */ static convertQuadSetToData(quadset) { var entries = quadset.map((quad) => this.convertQuadToData(quad)); return { __type: 'qd', //quad data entries, }; } static toJsObjectInternal(object, dataQuads, includeIncomingProperties = false, includeGraphs = true) { if (typeof object === 'string' || typeof object === 'number' || typeof object === 'boolean') { return Promise.resolve(object); } else if (object instanceof Graph) { return this.convertGraph(object, dataQuads, includeIncomingProperties); } else if (object instanceof BlankNode) { return this.convertBlankNode(object, dataQuads); } else if (object instanceof NamedNode) { return this.convertNamedNode(object, dataQuads, includeIncomingProperties); } else if (object instanceof Literal) { return this.convertLiteral(object, dataQuads); } else if (object instanceof QuadSet) { return this.convertQuads(object, includeGraphs); } else if (object instanceof QuadArray) { return this.convertQuads(object, includeGraphs, 'qa'); } else if (object instanceof Shape) { return this.convertShape(object, dataQuads, includeIncomingProperties); } else if (object instanceof Quad) { return this.convertQuad(object, dataQuads, includeGraphs); } else if (object instanceof NodeSet) { return this.convertNodeSet(object, dataQuads, includeIncomingProperties, includeGraphs); } else if (object instanceof ShapeSet) { return this.convertShapeSet(object, dataQuads, includeIncomingProperties, includeGraphs); } else if (object instanceof CoreSet) { return this.convertCoreSet(object, dataQuads, 'cs', includeIncomingProperties, includeGraphs); } else if (object instanceof CoreMap) { return this.convertCoreMap(object, dataQuads, 'cm', includeIncomingProperties, includeGraphs); } else if ((object === null || object === void 0 ? void 0 : object.prototype) instanceof Shape || object === Shape) { return this.convertShapeClass(object); } else if (object instanceof URIMappings) { return this.convertURIMappings(object); } else if (object instanceof CoreMap) { throw new Error('JSON conversion to be implemented'); } else if ((object instanceof Object && object !== null) || object instanceof Array) { var valuePromises = []; //clone the object so that we don't overwrite the original object = Array.isArray(object) ? [...object] : { ...object }; var setValue = (k, v) => { object[k] = v; }; for (var key in object) { valuePromises.push(this.toJsObjectInternal(object[key], dataQuads, includeIncomingProperties, includeGraphs).then(setValue.bind(this, key))); } return Promise.all(valuePromises).then((valueObjects) => { return this.convertNativeObject(object); }); } else if (typeof object === 'undefined' || object === null) { return Promise.resolve(object); } else { throw new Error('Unknown type. Trying to convert this to a JS object ' + typeof object + ' - ' + object.toString()); } } static convertNativeObject(object) { return Promise.resolve(object); } static convertNamedNode(uriResource, dataQuads, includeIncomingProperties = false) { var res = { __u: uriResource.uri, }; if (dataQuads) { return this.collectData(uriResource.getAllQuads(includeIncomingProperties), dataQuads).then(() => res); } return Promise.resolve(res); } static convertBlankNode(uriResource, dataQuads, includeIncomingProperties = true) { var res = { __b: uriResource.uri, }; if (dataQuads) { return this.collectData(uriResource.getAllQuads(includeIncomingProperties), dataQuads).then(() => res); } return Promise.resolve(res); } static convertLiteral(literal, dataQuads) { return Promise.resolve({ __l: literal.toString(), }); } static async convertGraph(graph, dataQuads, includeIncomingProperties = false) { var res = { __g: graph.node.uri, }; if (dataQuads) { return Promise.all([ this.collectData(graph.node.getAllQuads(includeIncomingProperties), dataQuads), JSONLDWriter.fromQuads(graph.getContents()), ]).then(([, jsonldData]) => { res['content'] = jsonldData; return res; }); // return this.collectData(graph.getContents(),dataQuads).then(() => res); // this.addQuads(graph.getContents(), quads); } return Promise.resolve(res); } static convertCoreSet(coreset, dataQuads, type = 'cs', includeIncomingProperties = false, includeGraphs = true) { var entryPromises = []; coreset.forEach((item) => { entryPromises.push(this.toJsObjectInternal(item, dataQuads, includeIncomingProperties, includeGraphs)); }); return Promise.all(entryPromises) .then((results) => { return { __type: type, entries: results, }; }) .catch((err) => { console.warn('Error during converting CoreSet ' + coreset.toString()); console.warn(err.stack); // this.convertCoreSet(coreset,dataQuads,type,includeIncomingProperties); return null; }); } static convertShapeSet(coreset, dataQuads, includeIncomingProperties = false, includeGraphs = true) { // throw new Error('Converting ShapeSets to JSON is not yet supported'); // return Promise.reject(); return this.convertCoreSet(coreset, dataQuads, 'ss', includeIncomingProperties, includeGraphs); } static convertURIMappings(mappings) { return Promise.resolve({ __urimap: mappings.map, }); } static convertCoreMap(coremap, dataQuads, type = 'cm', includeIncomingProperties = false, includeGraphs = true) { var entryPromises = []; coremap.forEach((item, key) => { entryPromises.push(this.toJsObjectInternal([key, item], dataQuads, includeIncomingProperties, includeGraphs)); }); return Promise.all(entryPromises) .then((results) => { return { __type: type, entries: results, }; }) .catch((err) => { console.warn('Error during converting CoreMap'); console.warn(err.stack); return null; }); } static convertNodeSet(set, dataQuads, includeIncomingProperties = false, includeGraphs = false) { return this.convertCoreSet(set, dataQuads, 'ns', includeIncomingProperties, includeGraphs); } static convertQuads(quads, includeGraphs = true, type = 'qs') { // if (quads.some((q) => q.isRemoved)) { // console.warn( // 'WARNING: Converting quads to JSON that contain removed quads. You should likely filter these out first. If you want to send information about removed quads, use JSONWriter.convertQuadSetToData instead', // ); // } //The JSON writer here depends on the JSONLD writer to convert quads into jsonld return JSONLDWriter.fromQuads(quads, true, includeGraphs).then((jsonld) => { let result = {}; result['__' + type] = jsonld; return result; }); } static convertQuadToData(quad, includeGraphs = true) { return [ quad.subject.uri, quad.predicate.uri, quad.object.toString(), includeGraphs ? quad.graph.node.toString() : null, quad.isRemoved, ]; } static async convertQuad(quad, dataQuads, includeGraphs = true) { var res = { __t: this.convertQuadToData(quad, includeGraphs), }; if (dataQuads) { return this.collectData([quad], dataQuads).then(() => res); } return Promise.resolve(res); } static async convertShape(shape, dataQuads, includeIncomingProperties = false) { if (!shape.nodeShape) { console.warn('This shape is not linked and hence cannot be converted to JSON: ' + shape.toString()); } var res = { __s: shape.nodeShape.namedNode.uri, u: shape.namedNode.uri, }; if (dataQuads) { if (dataQuads) { return this.collectData(shape.getDataQuads(includeIncomingProperties), dataQuads).then(() => res); } } return Promise.resolve(res); } static async convertShapeClass(shapeClass) { if (!shapeClass.shape) { console.warn('This shape class is not linked and hence cannot be converted to JSON: ' + shapeClass.name); } var res = { __sc: shapeClass.shape.namedNode.uri, }; return Promise.resolve(res); } static collectData(quadsToAdd, dataQuads) { var promises = []; quadsToAdd === null || quadsToAdd === void 0 ? void 0 : quadsToAdd.forEach((quad) => { //we allow implementers of getDataQuads send null in between other quads, so we need to check for that if (!quad) return; if (!dataQuads.has(quad)) { dataQuads.add(quad); if (quad.object instanceof BlankNode) { this.collectData(quad.object.getAllQuads(), dataQuads); } else if (quad.object instanceof NamedNode && quad.object.isTemporaryNode) { this.collectData(quad.object.getAllQuads(), dataQuads); } } }); return Promise.all(promises); } }; JSONWriter = __decorate([ linkedUtil ], JSONWriter); export { JSONWriter }; //# sourceMappingURL=JSONWriter.js.map