linkeddata / rdflib.js

Linked Data API for JavaScript
http://linkeddata.github.io/rdflib.js/doc/
Other
566 stars 146 forks source link

Unable to serialize to JSON-LD #364

Closed thewilkybarkid closed 4 years ago

thewilkybarkid commented 5 years ago

Using 1.0.4 (and N3 1.2.1) I'm getting TypeError: n3Writer.addTriple is not a function when trying to serialize to JSON-LD. (The other formats work correctly.)

Seems that addQuad needs to be used. Can't see any tests about serializing to JSON-LD either.

dmitrizagidulin commented 5 years ago

+1, I also just hit this bug when upgrading to rdflib v1.0.4

WhyINeedToFillUsername commented 5 years ago

I don't not know if this is the same problem as @thewilkybarkid has, but I also have problems parsing JSON-LD. I'm using a simple setup with index.html and app.js, using rdflib.js 1.0.6 from https://cdn.jsdelivr.net/npm/rdflib@1.0.6/dist/rdflib.min.js.

When I do

    var uri = "some uri";
    var body = '{\n' +
        '  "@context": "http://schema.org/",\n' +
        '  "@type": "Person",\n' +
        '  "name": "Jane Doe",\n' +
        '  "jobTitle": "Professor",\n' +
        '  "telephone": "(425) 123-4567",\n' +
        '  "url": "http://www.janedoe.com"\n' +
        '}';
    var mimeType = 'application/ld+json';
    var store = $rdf.graph();
    $rdf.parse(body, store, uri, mimeType);

it ends with an console error Uncaught TypeError: i is not a function When debugging, I found this problem: index.htmlAccess to XMLHttpRequest at 'http://schema.org/' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

If I do the same for RDF triples, it works ok. If I do it for different JSON without schema.org, it also doesn't work, however I was unable to debug the library to find the cause.

jaxoncreed commented 5 years ago

This is causing a failure fetching JSON-LD documents from Node Solid Server. (https://github.com/solid/node-solid-server/issues/1332)

jaxoncreed commented 5 years ago

@joepio I heard you were working with JSON-LD, is this perhaps one of the things that will be fixed with that?

joepio commented 5 years ago

My colleage @fletcher91 worked on JSON-LD parsing, his PR was merged 16 days ago into 1.0.6. This problem is probably not caused or solved by that PR.

I'm working on migration to typescript, but I'll try to replicate the issue above!

linonetwo commented 4 years ago

Still a problem in 1.0.6 and 1.0.7-2

Playground https://runkit.com/linonetwo/5e018eb5b3a641001a406dbe I'm trying to read solid container.

linonetwo commented 4 years ago

Hi @joepio seems addTriple is deprecated in n3 npm package, please use addQuad instead.

But, I tried replace it with addQuad, resulted in following error:

/node_modules/async/dist/async.js:322
            if (fn === null) throw new Error("Callback was already called.");
                             ^

Error: Callback was already called.
    at N3Parser._callback (/node_modules/async/dist/async.js:322:36)
    at N3Parser._emit (/node_modules/n3/lib/N3Parser.js:840:10)
    at N3Parser._readPunctuation (/node_modules/n3/lib/N3Parser.js:605:35)
    at /node_modules/n3/lib/N3Parser.js:995:127
    at N3Lexer._tokenizeToEnd (/node_modules/n3/lib/N3Lexer.js:382:7)
    at Immediate.<anonymous> (/node_modules/n3/lib/N3Lexer.js:490:14)
    at processImmediate (internal/timers.js:445:21)
linonetwo commented 4 years ago

@joepio So this might be fixed after refactor to TS completed?

joepio commented 4 years ago

@linonetwo the TS migration is mostly finished - most of the important classes have been fully migrated. Sorry I didn't fix the issue. I won't be working on it in the near future, so don't expect too much TS migrations from me.

However, I think this issue doesn't have anything to do with TS migration (or the aforementioned JSON-LD parsing).

linonetwo commented 4 years ago

I can try to fix this after TS refactor, let's wait.

ericprud commented 4 years ago

The prob is that convertToJson's first waterfall function is expected to get called repeatedly. Apparently async's waterfall expects a single invocation. The solution appears to be to move the second callback into the first:

    n3Parser.parse(n3String, function (error, quad, prefix) {
      if (error) {
        throw error;
      } else if (quad !== null) {
        n3Writer.addQuad(quad);
      } else {
        n3Writer.end(callback);
      }
    });

though I don't know what should really happen with that if (error) condition.

Will have to do the same with convertToNQuads (below) as well.

WhyINeedToFillUsername commented 4 years ago

Btw, when I use https: "@context": "https://schema.org/", I get no cors error. Might be helpful to somebody.

csarven commented 4 years ago

@WhyINeedToFillUsername Is it no Mixed Content error?

WhyINeedToFillUsername commented 4 years ago

Hope I am not mixing things too much. I am adding to my comment above (https://github.com/linkeddata/rdflib.js/issues/364#issuecomment-546705383).