sparqljson-parse
Advanced tools
Comparing version 2.1.2 to 2.2.0
@@ -35,2 +35,8 @@ /// <reference types="node" /> | ||
/** | ||
* Convert a SPARQL JSON result value to an RDF term. | ||
* @param rawValue A SPARQL JSON result value | ||
* @return {RDF.Term} An RDF term. | ||
*/ | ||
parseJsonValue(rawValue: any): RDF.Term; | ||
/** | ||
* Convert a SPARQL JSON boolean response to a boolean. | ||
@@ -37,0 +43,0 @@ * This will throw an error if the given reponse was not a valid boolean response. |
@@ -7,3 +7,3 @@ "use strict"; | ||
// tslint:disable-next-line:no-var-requires | ||
const JsonParser = require('jsonparse'); | ||
const JsonParser = require('@bergos/jsonparse'); | ||
/** | ||
@@ -54,3 +54,8 @@ * Parser for the SPARQL 1.1 Query Results JSON format. | ||
else if (typeof jsonParser.key === 'number' && jsonParser.stack.length === 3 && jsonParser.stack[1].key === 'results' && jsonParser.stack[2].key === 'bindings') { | ||
resultStream.push(this.parseJsonBindings(value)); | ||
try { | ||
resultStream.push(this.parseJsonBindings(value)); | ||
} | ||
catch (error) { | ||
resultStream.emit("error", error); | ||
} | ||
} | ||
@@ -88,27 +93,3 @@ else if (jsonParser.key === "metadata" && jsonParser.stack.length === 1) { | ||
const rawValue = rawBindings[key]; | ||
let value = null; | ||
switch (rawValue.type) { | ||
case 'bnode': | ||
value = this.dataFactory.blankNode(rawValue.value); | ||
break; | ||
case 'literal': | ||
if (rawValue['xml:lang']) { | ||
value = this.dataFactory.literal(rawValue.value, rawValue['xml:lang']); | ||
} | ||
else if (rawValue.datatype) { | ||
value = this.dataFactory.literal(rawValue.value, this.dataFactory.namedNode(rawValue.datatype)); | ||
} | ||
else { | ||
value = this.dataFactory.literal(rawValue.value); | ||
} | ||
break; | ||
case 'typed-literal': | ||
// Virtuoso uses this non-spec-compliant way of defining typed literals | ||
value = this.dataFactory.literal(rawValue.value, this.dataFactory.namedNode(rawValue.datatype)); | ||
break; | ||
default: | ||
value = this.dataFactory.namedNode(rawValue.value); | ||
break; | ||
} | ||
bindings[this.prefixVariableQuestionMark ? ('?' + key) : key] = value; | ||
bindings[this.prefixVariableQuestionMark ? ('?' + key) : key] = this.parseJsonValue(rawValue); | ||
} | ||
@@ -118,2 +99,41 @@ return bindings; | ||
/** | ||
* Convert a SPARQL JSON result value to an RDF term. | ||
* @param rawValue A SPARQL JSON result value | ||
* @return {RDF.Term} An RDF term. | ||
*/ | ||
parseJsonValue(rawValue) { | ||
let value; | ||
switch (rawValue.type) { | ||
case 'bnode': | ||
value = this.dataFactory.blankNode(rawValue.value); | ||
break; | ||
case 'literal': | ||
if (rawValue['xml:lang']) { | ||
value = this.dataFactory.literal(rawValue.value, rawValue['xml:lang']); | ||
} | ||
else if (rawValue.datatype) { | ||
value = this.dataFactory.literal(rawValue.value, this.dataFactory.namedNode(rawValue.datatype)); | ||
} | ||
else { | ||
value = this.dataFactory.literal(rawValue.value); | ||
} | ||
break; | ||
case 'typed-literal': | ||
// Virtuoso uses this non-spec-compliant way of defining typed literals | ||
value = this.dataFactory.literal(rawValue.value, this.dataFactory.namedNode(rawValue.datatype)); | ||
break; | ||
case 'triple': | ||
const tripleValue = rawValue.value; | ||
if (!tripleValue || !tripleValue.subject || !tripleValue.predicate || !tripleValue.object) { | ||
throw new Error('Invalid quoted triple: ' + JSON.stringify(rawValue)); | ||
} | ||
value = this.dataFactory.quad(this.parseJsonValue(tripleValue.subject), this.parseJsonValue(tripleValue.predicate), this.parseJsonValue(tripleValue.object)); | ||
break; | ||
default: | ||
value = this.dataFactory.namedNode(rawValue.value); | ||
break; | ||
} | ||
return value; | ||
} | ||
/** | ||
* Convert a SPARQL JSON boolean response to a boolean. | ||
@@ -120,0 +140,0 @@ * This will throw an error if the given reponse was not a valid boolean response. |
{ | ||
"name": "sparqljson-parse", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Parses SPARQL JSON query results", | ||
@@ -35,7 +35,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"@types/jest": "^28.0.0", | ||
"@types/jest": "^29.0.0", | ||
"@types/minimist": "^1.2.0", | ||
"arrayify-stream": "^2.0.0", | ||
"coveralls": "^3.0.0", | ||
"jest": "^28.0.0", | ||
"jest": "^29.0.0", | ||
"jest-rdf": "^1.7.0", | ||
@@ -45,3 +45,3 @@ "manual-git-changelog": "^1.0.0", | ||
"streamify-string": "^1.0.1", | ||
"ts-jest": "^28.0.0", | ||
"ts-jest": "^29.0.0", | ||
"ts-loader": "^9.3.1", | ||
@@ -52,3 +52,3 @@ "tslint": "^6.0.0", | ||
"webpack": "^5.73.0", | ||
"webpack-cli": "^4.10.0" | ||
"webpack-cli": "^5.0.0" | ||
}, | ||
@@ -91,4 +91,3 @@ "jest": { | ||
"@types/readable-stream": "^2.3.13", | ||
"buffer": "^6.0.3", | ||
"jsonparse": "^1.3.1", | ||
"@bergos/jsonparse": "^1.4.1", | ||
"rdf-data-factory": "^1.1.0", | ||
@@ -95,0 +94,0 @@ "readable-stream": "^4.0.0" |
@@ -8,3 +8,3 @@ # SPARQL-Results+JSON Parse | ||
A utility package that allows you to parse [SPARQL JSON](https://www.w3.org/TR/sparql11-results-json/) results | ||
in a convenient [RDFJS](http://rdf.js.org/)-based datastructure. | ||
in a convenient [RDF/JS](https://rdf.js.org/)-based datastructure. | ||
@@ -27,3 +27,18 @@ For example, the following SPARQL JSON result can be converted as follows: | ||
{ "book": { "type": "uri", "value": "http://example.org/book/book4" } }, | ||
{ "book": { "type": "uri", "value": "http://example.org/book/book5" } } | ||
{ "book": { "type": "uri", "value": "http://example.org/book/book5" } }, | ||
{ | ||
"book": { | ||
"type": "triple", | ||
"value": { | ||
"subject": { | ||
"type": "uri", | ||
"value": "http://example.org/alice" | ||
}, | ||
"predicate": { | ||
"type": "uri", | ||
"value": "http://example.org/name" | ||
} | ||
} | ||
} | ||
} | ||
] | ||
@@ -42,8 +57,9 @@ } | ||
{ '?book': namedNode('http://example.org/book/book5') }, | ||
{ '?book': quad(namedNode('http://example.org/bob'), namedNode('http://example.org/name'), literal('Bob', namedNode('http://example.org/Type'))) }, | ||
] | ||
``` | ||
Where `namedNode` is an RDFJS named node. | ||
Where `namedNode` is an RDF/JS named node, `quad` is an RDF/JS quad/triple, and `literal` is an RDF/JS literal. | ||
This library automatically converts all SPARQL JSON result values to their respective RDFJS type. | ||
This library automatically converts all SPARQL JSON result values to their respective RDF/JS type. | ||
@@ -50,0 +66,0 @@ ## Usage |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33065
5
165
9
263
+ Added@bergos/jsonparse@^1.4.1
+ Added@bergos/jsonparse@1.4.2(transitive)
- Removedbuffer@^6.0.3
- Removedjsonparse@^1.3.1
- Removedjsonparse@1.3.1(transitive)