Skip to content

Commit

Permalink
jsonld BlankNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
bourgeoa committed Dec 2, 2022
1 parent c8cf530 commit 17052b6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/jsonldparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function jsonldObjectToTerm (kb, obj) {
}

if (Object.prototype.hasOwnProperty.call(obj, '@id')) {
return kb.rdfFactory.namedNode(obj['@id'])
return nodeType(kb, obj)
}

if (Object.prototype.hasOwnProperty.call(obj, '@language')) {
Expand All @@ -44,7 +44,7 @@ export function jsonldObjectToTerm (kb, obj) {
* Adds the statements in a json-ld list object to {kb}.
*/
function listToStatements (kb, obj) {
const listId = obj['@id'] ? kb.rdfFactory.namedNode(obj['@id']) : kb.rdfFactory.blankNode()
const listId = obj['@id'] ? nodeType(kb, obj) : kb.rdfFactory.blankNode()

const items = obj['@list'].map((listItem => jsonldObjectToTerm(kb, listItem)))
const statements = arrayToStatements(kb.rdfFactory, listId, items)
Expand Down Expand Up @@ -82,10 +82,19 @@ export default function jsonldParser (str, kb, base, callback) {
.catch(callback)
}

function nodeType (kb, obj) {
if (obj['@id'].startsWith('_:')) {
// This object is a Blank Node.. pass the id without the preceding _:
return kb.rdfFactory.blankNode(obj['@id'].substring(2));
} else {
// This object is a Named Node
return kb.rdfFactory.namedNode(obj['@id']);
}
}

function processResource(kb, base, flatResource) {
const id = flatResource['@id']
? kb.rdfFactory.namedNode(flatResource['@id'])
? nodeType(kb, flatResource)
: kb.rdfFactory.blankNode()

for (const property of Object.keys(flatResource)) {
Expand Down
67 changes: 66 additions & 1 deletion tests/unit/parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import parse from '../../src/parse'
import CanonicalDataFactory from '../../src/factories/canonical-data-factory'
import defaultXSD from '../../src/xsd'
import DataFactory from '../../src/factories/rdflib-data-factory'
import serialize from '../../src/serialize'

describe('Parse', () => {
describe('ttl', () => {
Expand Down Expand Up @@ -231,7 +232,71 @@ describe('Parse', () => {
})
})

describe('with a @type node', () => {
describe('jsonld with blanknodes', () => {
let store
before(done => {
const base = 'https://www.example.org/abc/def'
const mimeType = 'application/ld+json'
const content = `
{
"@context": {
"ex": "http://example.com#"
},
"@id": "ex:myid",
"ex:prop1": {
"ex:prop2": {
"ex:prop3": "value"
}
}
}`
store = DataFactory.graph()
parse(content, store, base, mimeType, done)
})

it('store contains 3 statements', () => {
expect(store.statements).to.have.length(3)
// console.log(serialize(null, store, null, 'text/turtle'))
expect(serialize(null, store, null, 'text/turtle')).to.eql(`@prefix ex: <http://example.com#>.
ex:myid ex:prop1 [ ex:prop2 [ ex:prop3 "value" ] ].
`)
const nt = store.toNT()
expect(nt).to.include('<http://example.com#myid> <http://example.com#prop1> _:b0 .')
expect(nt).to.include('_:b0 <http://example.com#prop2> _:b1 .')
expect(nt).to.include('_:b1 <http://example.com#prop3> "value" .')
});
})

describe('ttl with blanknodes', () => {
let store
before(done => {
const base = 'https://www.example.org/abc/def'
const mimeType = 'text/turtle'
const content = `
@prefix : <#>.
@prefix ex: <http://example.com#>.
ex:myid ex:prop1 _:b0.
_:b0 ex:prop2 _:b1.
_:b1 ex:prop3 "value".
`
store = DataFactory.graph()
parse(content, store, base, mimeType, done)
})

it('store contains 3 statements', () => {
expect(store.statements).to.have.length(3)
// console.log(serialize(null, store, null, 'application/json+ld'))
expect(serialize(null, store, null, 'text/turtle')).to.eql(`@prefix ex: <http://example.com#>.
ex:myid ex:prop1 [ ex:prop2 [ ex:prop3 "value" ] ].
`)
});
})

describe('with a @type node', () => {
const content = `
{
"@id": "jane",
Expand Down

0 comments on commit 17052b6

Please sign in to comment.