rdflib 2.2.22-1e22b5ce → 2.2.22-2cea0982

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -65,7 +65,7 @@ export default function jsonldParser(str, kb, base, callback) {
65
65
  }
66
66
  function nodeType(kb, obj) {
67
67
  if (obj['@id'].startsWith('_:')) {
68
- // This object is a Blank Node.. pass the id without the preceding _:
68
+ // This object is a Blank Node. Pass the id without the `_:` prefix
69
69
  return kb.rdfFactory.blankNode(obj['@id'].substring(2));
70
70
  } else {
71
71
  // This object is a Named Node
package/esm/serialize.js CHANGED
@@ -1,7 +1,5 @@
1
1
  import Serializer from './serializer';
2
2
  import { JSONLDContentType, N3ContentType, N3LegacyContentType, NQuadsAltContentType, NQuadsContentType, NTriplesContentType, RDFXMLContentType, TurtleContentType, TurtleLegacyContentType } from './types';
3
- import * as jsonld from 'jsonld';
4
-
5
3
  /**
6
4
  * Serialize to the appropriate format
7
5
  */
@@ -21,7 +19,6 @@ contentType, callback, options) {
21
19
  var sz = Serializer(kb);
22
20
  if (opts.flags) sz.setFlags(opts.flags);
23
21
  var newSts = kb.statementsMatching(undefined, undefined, undefined, target);
24
- var n3String;
25
22
 
26
23
  // If an IndexedFormula, use the namespaces from the given graph as suggestions
27
24
  if ('namespaces' in kb) {
@@ -51,10 +48,9 @@ contentType, callback, options) {
51
48
  documentString = sz.statementsToNTriples(newSts);
52
49
  return executeCallback(null, documentString);
53
50
  case JSONLDContentType:
54
- sz.setFlags('deinprstux'); // Use adapters to connect to incmpatible parser
55
- n3String = sz.statementsToNTriples(newSts);
56
- // n3String = sz.statementsToN3(newSts)
57
- return toJsonld(n3String);
51
+ sz.setFlags('si'); // use turtle parameters
52
+ documentString = sz.statementsToJsonld(newSts); // convert via turtle
53
+ return executeCallback(null, documentString);
58
54
  case NQuadsContentType:
59
55
  case NQuadsAltContentType:
60
56
  // @@@ just outpout the quads? Does not work for collections
@@ -80,16 +76,4 @@ contentType, callback, options) {
80
76
  return result;
81
77
  }
82
78
  }
83
- function toJsonld(item) {
84
- try {
85
- return jsonld.fromRDF(item, {
86
- format: 'application/n-quads'
87
- }).then(docJsonld => {
88
- return JSON.stringify(docJsonld);
89
- });
90
- // return JSON.stringify(await jsonld.fromRDF(item, {format: 'application/n-quads'}))
91
- } catch (e) {
92
- throw e;
93
- }
94
- }
95
79
  }
package/esm/serializer.js CHANGED
@@ -13,6 +13,8 @@ import * as Util from './utils-js';
13
13
  import CanonicalDataFactory from './factories/canonical-data-factory';
14
14
  import { createXSD } from './xsd';
15
15
  import solidNs from 'solid-namespace';
16
+ // import * as jsonld from 'jsonld'
17
+ import * as ttl2jsonld from '@frogcat/ttl2jsonld';
16
18
  export default function createSerializer(store) {
17
19
  return new Serializer(store);
18
20
  }
@@ -962,6 +964,27 @@ export class Serializer {
962
964
  var tree2 = [str, tree, '</rdf:RDF>']; // @@ namespace declrations
963
965
  return XMLtreeToString(tree2, -1);
964
966
  } // End @@ body
967
+
968
+ statementsToJsonld(sts) {
969
+ // ttl2jsonld creates context keys for all ttl prefix
970
+ // context keys must be full IRI
971
+ function findId(itemObj) {
972
+ if (itemObj['@id']) {
973
+ const item = itemObj['@id'].split(':');
974
+ if (keys[item[0]]) itemObj['@id'] = jsonldObj['@context'][item[0]] + item[1];
975
+ }
976
+ const itemValues = Object.values(itemObj);
977
+ for (const i in itemValues) {
978
+ if (typeof itemValues[i] !== 'string') {
979
+ // @list contains array
980
+ findId(itemValues[i]);
981
+ }
982
+ }
983
+ }
984
+ const turtleDoc = this.statementsToN3(sts);
985
+ const jsonldObj = ttl2jsonld.parse(turtleDoc);
986
+ return JSON.stringify(jsonldObj, null, 2);
987
+ }
965
988
  }
966
989
 
967
990
  // String escaping utilities
package/lib/formula.d.ts CHANGED
@@ -270,7 +270,7 @@ export default class Formula extends Node {
270
270
  * @param provenance - The provenance URI
271
271
  * @param options - options to pass to the serializer, as defined in serialize method
272
272
  */
273
- serialize(base: any, contentType: any, provenance: any, options?: any): string | undefined;
273
+ serialize(base: any, contentType: any, provenance: any, options?: any): string | Promise<string> | undefined;
274
274
  /**
275
275
  * Creates a new formula with the substituting bindings applied
276
276
  * @param bindings - The bindings to substitute
@@ -72,7 +72,7 @@ function jsonldParser(str, kb, base, callback) {
72
72
  }
73
73
  function nodeType(kb, obj) {
74
74
  if (obj['@id'].startsWith('_:')) {
75
- // This object is a Blank Node.. pass the id without the preceding _:
75
+ // This object is a Blank Node. Pass the id without the `_:` prefix
76
76
  return kb.rdfFactory.blankNode(obj['@id'].substring(2));
77
77
  } else {
78
78
  // This object is a Named Node
@@ -23,4 +23,4 @@ contentType?: string | ContentType, callback?: (err: Error | undefined | null, r
23
23
  * A set of [prefix, uri] pairs that define namespace prefixes
24
24
  */
25
25
  namespaces?: Record<string, string>;
26
- }): string | undefined;
26
+ }): string | undefined | Promise<string>;
package/lib/serialize.js CHANGED
@@ -7,9 +7,6 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.default = serialize;
8
8
  var _serializer = _interopRequireDefault(require("./serializer"));
9
9
  var _types = require("./types");
10
- var jsonld = _interopRequireWildcard(require("jsonld"));
11
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
10
  /**
14
11
  * Serialize to the appropriate format
15
12
  */
@@ -29,7 +26,6 @@ contentType, callback, options) {
29
26
  var sz = (0, _serializer.default)(kb);
30
27
  if (opts.flags) sz.setFlags(opts.flags);
31
28
  var newSts = kb.statementsMatching(undefined, undefined, undefined, target);
32
- var n3String;
33
29
 
34
30
  // If an IndexedFormula, use the namespaces from the given graph as suggestions
35
31
  if ('namespaces' in kb) {
@@ -59,10 +55,9 @@ contentType, callback, options) {
59
55
  documentString = sz.statementsToNTriples(newSts);
60
56
  return executeCallback(null, documentString);
61
57
  case _types.JSONLDContentType:
62
- sz.setFlags('deinprstux'); // Use adapters to connect to incmpatible parser
63
- n3String = sz.statementsToNTriples(newSts);
64
- // n3String = sz.statementsToN3(newSts)
65
- return toJsonld(n3String);
58
+ sz.setFlags('si'); // use turtle parameters
59
+ documentString = sz.statementsToJsonld(newSts); // convert via turtle
60
+ return executeCallback(null, documentString);
66
61
  case _types.NQuadsContentType:
67
62
  case _types.NQuadsAltContentType:
68
63
  // @@@ just outpout the quads? Does not work for collections
@@ -88,16 +83,4 @@ contentType, callback, options) {
88
83
  return result;
89
84
  }
90
85
  }
91
- function toJsonld(item) {
92
- try {
93
- return jsonld.fromRDF(item, {
94
- format: 'application/n-quads'
95
- }).then(docJsonld => {
96
- return JSON.stringify(docJsonld);
97
- });
98
- // return JSON.stringify(await jsonld.fromRDF(item, {format: 'application/n-quads'}))
99
- } catch (e) {
100
- throw e;
101
- }
102
- }
103
86
  }
@@ -63,5 +63,6 @@ export class Serializer {
63
63
  symbolToN3(x: any): any;
64
64
  writeStore(write: any): void;
65
65
  statementsToXML(sts: any): string;
66
+ statementsToJsonld(sts: any): string;
66
67
  }
67
68
  import NamedNode from "./named-node";
package/lib/serializer.js CHANGED
@@ -14,6 +14,7 @@ var Util = _interopRequireWildcard(require("./utils-js"));
14
14
  var _canonicalDataFactory = _interopRequireDefault(require("./factories/canonical-data-factory"));
15
15
  var _xsd = require("./xsd");
16
16
  var _solidNamespace = _interopRequireDefault(require("solid-namespace"));
17
+ var ttl2jsonld = _interopRequireWildcard(require("@frogcat/ttl2jsonld"));
17
18
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
18
19
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
20
  /* Serialization of RDF Graphs
@@ -24,6 +25,8 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
24
25
  ** Licence: MIT
25
26
  */
26
27
 
28
+ // import * as jsonld from 'jsonld'
29
+
27
30
  function createSerializer(store) {
28
31
  return new Serializer(store);
29
32
  }
@@ -973,6 +976,27 @@ class Serializer {
973
976
  var tree2 = [str, tree, '</rdf:RDF>']; // @@ namespace declrations
974
977
  return XMLtreeToString(tree2, -1);
975
978
  } // End @@ body
979
+
980
+ statementsToJsonld(sts) {
981
+ // ttl2jsonld creates context keys for all ttl prefix
982
+ // context keys must be full IRI
983
+ function findId(itemObj) {
984
+ if (itemObj['@id']) {
985
+ const item = itemObj['@id'].split(':');
986
+ if (keys[item[0]]) itemObj['@id'] = jsonldObj['@context'][item[0]] + item[1];
987
+ }
988
+ const itemValues = Object.values(itemObj);
989
+ for (const i in itemValues) {
990
+ if (typeof itemValues[i] !== 'string') {
991
+ // @list contains array
992
+ findId(itemValues[i]);
993
+ }
994
+ }
995
+ }
996
+ const turtleDoc = this.statementsToN3(sts);
997
+ const jsonldObj = ttl2jsonld.parse(turtleDoc);
998
+ return JSON.stringify(jsonldObj, null, 2);
999
+ }
976
1000
  }
977
1001
 
978
1002
  // String escaping utilities
package/lib/store.d.ts CHANGED
@@ -299,5 +299,5 @@ export default class IndexedFormula extends Formula {
299
299
  * @param term
300
300
  */
301
301
  uris(term: Quad_Subject): string[];
302
- serialize(base: any, contentType: any, provenance: any, options?: any): string | undefined;
302
+ serialize(base: any, contentType: any, provenance: any, options?: any): string | Promise<string> | undefined;
303
303
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rdflib",
3
3
  "description": "an RDF library for node.js. Suitable for client and server side.",
4
- "version": "2.2.22-1e22b5ce",
4
+ "version": "2.2.22-2cea0982",
5
5
  "private": false,
6
6
  "browserslist": [
7
7
  "> 0.5%"
@@ -45,6 +45,7 @@
45
45
  "bugs": "http://github.com/linkeddata/rdflib.js/issues",
46
46
  "dependencies": {
47
47
  "@babel/runtime": "^7.20.1",
48
+ "@frogcat/ttl2jsonld": "^0.0.9",
48
49
  "@xmldom/xmldom": "^0.8.6",
49
50
  "cross-fetch": "^3.1.5",
50
51
  "jsonld": "^8.1.0",
@@ -84,7 +85,7 @@
84
85
  "typescript": "^4.9.3",
85
86
  "webpack": "^5.75.0",
86
87
  "webpack-cli": "^5.0.0",
87
- "webpack-dev-server": "4.11"
88
+ "webpack-dev-server": "^4.11.1"
88
89
  },
89
90
  "scripts": {
90
91
  "build": "babel src --extensions \".ts,.js\" -d lib",
@@ -99,7 +100,7 @@
99
100
  "test": "npm run test:unit && npm run test:serialize && npm run test:types",
100
101
  "test:clean": "rimraf tests/serialize/,*",
101
102
  "test:serialize": "npm run build && npm run test:serialize:all && npm run test:clean",
102
- "test:serialize:all": "npm run test:serialize:8 && npm run test:serialize:1 && npm run test:serialize:2 && npm run test:serialize:3 && npm run test:serialize:4 && npm run test:serialize:5 && npm run test:serialize:6 && npm run test:serialize:7 && npm run test:serialize:8 && npm run test:serialize:9 && npm run test:serialize:10 && npm run test:serialize:11 && npm run test:serialize:12 && npm run test:serialize:13 && npm run test:serialize:14 && npm run test:serialize:15 && npm run test:serialize:16",
103
+ "test:serialize:all": "npm run test:serialize:1 && npm run test:serialize:2 && npm run test:serialize:3 && npm run test:serialize:4 && npm run test:serialize:5 && npm run test:serialize:6 && npm run test:serialize:7 && npm run test:serialize:8 && npm run test:serialize:10 && npm run test:serialize:11 && npm run test:serialize:12 && npm run test:serialize:13 && npm run test:serialize:14 && npm run test:serialize:15",
103
104
  "test:serialize:1": "cd ./tests/serialize && node ./data.js -in=t1.ttl -format=application/rdf+xml -out=,t1.xml && fs-grep http://www.w3.org/2001/XMLSchema#integer ,t1.xml",
104
105
  "test:serialize:2": "cd ./tests/serialize && node ./data.js -in=t2.ttl -format=application/rdf+xml -out=,t2.xml && node diff ,t2.xml t2-ref.xml",
105
106
  "test:serialize:3": "cd ./tests/serialize && node ./data.js -in=t3.ttl -format=application/rdf+xml -out=,t3.xml && node diff ,t3.xml t3-ref.xml",
@@ -108,7 +109,6 @@
108
109
  "test:serialize:6": "cd ./tests/serialize && node ./data.js -in=t5.n3 -format=text/n3 -out=,t6.n3 && node diff ,t6.n3 t6-ref.n3",
109
110
  "test:serialize:7": "cd ./tests/serialize && node ./data.js -in=t7.n3 -format=application/n-triples -out=,t7.nt && node diff ,t7.nt t7-ref.nt",
110
111
  "test:serialize:8": "cd ./tests/serialize && node ./data.js -in=t5.n3 -format=application/n-quads -out=,t8.nq && node diff ,t8.nq t8-ref.nq",
111
- "test:serialize:9": "cd ./tests/serialize && node ./data.js -in=t7.n3 -format=application/ld+json -out=,t9.jsonld && node diff ,t9.jsonld t9-ref.jsonld",
112
112
  "test:serialize:10": "cd ./tests/serialize && node ./data.js -in=details.ttl -format=text/turtle -out=,t10.ttl && node diff ,t10.ttl t10-ref.ttl",
113
113
  "test:serialize:11": "cd ./tests/serialize && node ./data.js -in=structures.n3 -format=application/rdf+xml -out=,structures.xml && node diff ,structures.xml t11-ref.xml",
114
114
  "test:serialize:12": "cd ./tests/serialize && node ./data.js -in=structures.n3 -format=text/turtle -out=,structures.ttl && node diff ,structures.ttl t12-ref.ttl",
@@ -84,7 +84,7 @@ export default function jsonldParser (str, kb, base, callback) {
84
84
 
85
85
  function nodeType (kb, obj) {
86
86
  if (obj['@id'].startsWith('_:')) {
87
- // This object is a Blank Node.. pass the id without the preceding _:
87
+ // This object is a Blank Node. Pass the id without the `_:` prefix
88
88
  return kb.rdfFactory.blankNode(obj['@id'].substring(2));
89
89
  } else {
90
90
  // This object is a Named Node
package/src/serialize.ts CHANGED
@@ -14,7 +14,6 @@ import {
14
14
  } from './types'
15
15
  import IndexedFormula from './store'
16
16
  import { BlankNode, NamedNode } from './tf-types'
17
- import * as jsonld from 'jsonld'
18
17
 
19
18
  /**
20
19
  * Serialize to the appropriate format
@@ -42,7 +41,7 @@ export default function serialize (
42
41
  */
43
42
  namespaces?: Record<string, string>
44
43
  }
45
- ): string | undefined {
44
+ ): string | undefined | Promise<string> {
46
45
  base = base || target?.value
47
46
  const opts = options || {}
48
47
  contentType = contentType || TurtleContentType // text/n3 if complex?
@@ -51,7 +50,6 @@ export default function serialize (
51
50
  var sz = Serializer(kb)
52
51
  if ((opts as any).flags) sz.setFlags((opts as any).flags)
53
52
  var newSts = kb!.statementsMatching(undefined, undefined, undefined, target as NamedNode)
54
- var n3String: string
55
53
 
56
54
  // If an IndexedFormula, use the namespaces from the given graph as suggestions
57
55
  if ('namespaces' in kb) {
@@ -82,10 +80,9 @@ export default function serialize (
82
80
  documentString = sz.statementsToNTriples(newSts)
83
81
  return executeCallback(null, documentString)
84
82
  case JSONLDContentType:
85
- sz.setFlags('deinprstux') // Use adapters to connect to incmpatible parser
86
- n3String = sz.statementsToNTriples(newSts)
87
- // n3String = sz.statementsToN3(newSts)
88
- return toJsonld(n3String) as any
83
+ sz.setFlags('si') // use turtle parameters
84
+ documentString = sz.statementsToJsonld(newSts) // convert via turtle
85
+ return executeCallback(null, documentString)
89
86
  case NQuadsContentType:
90
87
  case NQuadsAltContentType: // @@@ just outpout the quads? Does not work for collections
91
88
  sz.setFlags('deinprstux q') // Suppress nice parts of N3 to make ntriples
@@ -110,11 +107,4 @@ export default function serialize (
110
107
  return result as string
111
108
  }
112
109
  }
113
-
114
- function toJsonld (item ) {
115
- try {
116
- return jsonld.fromRDF(item, {format: 'application/n-quads'}).then( docJsonld => { return JSON.stringify(docJsonld) })
117
- // return JSON.stringify(await jsonld.fromRDF(item, {format: 'application/n-quads'}))
118
- } catch (e) { throw e }
119
- }
120
110
  }
package/src/serializer.js CHANGED
@@ -12,6 +12,8 @@ import * as Util from './utils-js'
12
12
  import CanonicalDataFactory from './factories/canonical-data-factory'
13
13
  import { createXSD } from './xsd'
14
14
  import solidNs from 'solid-namespace'
15
+ // import * as jsonld from 'jsonld'
16
+ import * as ttl2jsonld from '@frogcat/ttl2jsonld'
15
17
 
16
18
 
17
19
  export default function createSerializer(store) {
@@ -1011,6 +1013,28 @@ export class Serializer {
1011
1013
  var tree2 = [str, tree, '</rdf:RDF>'] // @@ namespace declrations
1012
1014
  return XMLtreeToString(tree2, -1)
1013
1015
  } // End @@ body
1016
+
1017
+ statementsToJsonld (sts) {
1018
+ // ttl2jsonld creates context keys for all ttl prefix
1019
+ // context keys must be full IRI
1020
+ function findId (itemObj) {
1021
+ if (itemObj['@id']) {
1022
+ const item = itemObj['@id'].split(':')
1023
+ if (keys[item[0]]) itemObj['@id'] = jsonldObj['@context'][item[0]] + item[1]
1024
+ }
1025
+ const itemValues = Object.values(itemObj)
1026
+ for (const i in itemValues) {
1027
+ if (typeof itemValues[i] !== 'string') { // @list contains array
1028
+ findId(itemValues[i])
1029
+ }
1030
+ }
1031
+ }
1032
+ const turtleDoc = this.statementsToN3(sts)
1033
+ const jsonldObj = ttl2jsonld.parse(turtleDoc)
1034
+
1035
+ return JSON.stringify(jsonldObj, null, 2)
1036
+ }
1037
+
1014
1038
  }
1015
1039
 
1016
1040
  // String escaping utilities