You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by Phillip Rhodes <mo...@gmail.com> on 2013/11/14 01:46:42 UTC

Consuming JSON-LD?

Jena gang:

I am trying to store triples retrieved from Stanbol in JSON-LD format,
but having
no luck so far.  I'm using Jena 2.10.1 and using the Jena JSONLD project from
https://github.com/afs/jena-jsonld

My (Groovy) code looks like this:


static main(args)
{
    JenaJSONLD.init();
    def content = """Quoddy can now recognize famous people like
Richard Marx""";

    // call Stanbol REST API to get enrichment data
    RESTClient restClient = new RESTClient( "http://localhost:8080" )

    // println "content submitted: ${content}";
    def restResponse = restClient.post( path:'enhancer',
                                                         body: content,
                                                      // headers:
['Accept':'application/rdf+xml'],

requestContentType : TEXT );

    String restResponseText = restResponse.getData();
    println "restResponseText:\n ${restResponseText}\n\n";

    // Make a TDB-backed dataset
    String directory = "MyDatabases/Dataset2" ;
    Dataset dataset = TDBFactory.createDataset(directory) ;
    dataset.begin(ReadWrite.READ) ;
    // Get model inside the transaction
    Model model = dataset.getDefaultModel() ;
    dataset.end() ;

    dataset.begin(ReadWrite.WRITE) ;
    model = dataset.getDefaultModel() ;
    StringReader reader = new StringReader( restResponseText );
    model.read( reader, "http://www.example.com", "JSON-LD" );
    // RDFDataMgr.read(model, reader, "http://example.com/", JenaJSONLD.JSONLD);
    dataset.commit();
    dataset.end();

    println "done";
}

and results in a NullPointerException like this:

Caught: java.lang.NullPointerException
java.lang.NullPointerException
at org.apache.jena.riot.RDFDataMgr.processTriples(RDFDataMgr.java:796)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:289)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:273)
at org.apache.jena.riot.adapters.RDFReaderRIOT.read(RDFReaderRIOT.java:61)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:267)
at com.hp.hpl.jena.rdf.model.Model$read.call(Unknown Source)
at org.example.stanbol.StanbolJenaMain2.main(StanbolJenaMain2.groovy:58)


If I do the same thing with RDF/XML (and appropriate code changes) I
can save the triples fine.  But I really want to use JSON-LD, as I do
other stuff with the JSON-LD
that comes back, using Javascript on the client-side, and it makes
life a lot easier
if I just take the JSON-LD and use it everywhere.


Any thoughts? Am I doing something wrong, or is there just a bug in
the JSONLD module?  I can also post the blog of json that I get back
if it would help.


Thanks,


Phil

This message optimized for indexing by NSA PRISM

Re: Consuming JSON-LD?

Posted by Phillip Rhodes <mo...@gmail.com>.
Ah, OK.   I'll give that a try then.  Thanks for the heads-up.


Phil
This message optimized for indexing by NSA PRISM


On Fri, Nov 15, 2013 at 4:01 AM, Andy Seaborne <an...@apache.org> wrote:
> On 15/11/13 05:59, Phillip Rhodes wrote:
>>
>> FWIW... I just grabbed the Jena source, imported it into my workspace
>> and set my project to reference that, so I could debug into this as
>> it's running. What I see is the code getting to line 864 in RDFDataMgr
>> and executing this code:
>>
>> parser = RiotReader.createParser(tokenizer, lang, base, output) ;
>>
>> which returns null.  The next line tries to use the null reference,
>> and hence the NPE.
>>
>> Looking at RiotReader.createParser(), I don't see anything in there
>> that mentions JSON-LD at all.    But the docs for java-jsonld say
>> something like:
>>
>> "JenaJSONLD must be initialized so that the readers and writers are
>> registered with Jena."  and I do call the init() method.  But I don't
>> see how that registration process plays with this createParser() code.
>>
>> I'm guessing things have just gotten out of sync between Jena and the
>> JSON-LD stuff?
>
>
> The problem is reading from a reader (I should have noticed this earlier in
> the thread).
>
> If you use an InputStream it will work (= it does for me).
>
> Readers are troublesome because they fix the charset before Jena gets a
> chance set the encodign according to syntax.  A FileReader is particularly
> troublesome because it fixes the charset as the system default so if that is
> not UTF-8 (and it isn't on Windows) it will not be UTF-8.
>
> StringReader is only usecase but it currently follows a hardwired path
> inside RDFDataMgr.  The general ReaderRIOT interface does not have Readers.
>
> Regrettably, I think I'll have to go and add Readers to ReaderRIOT (it only
> results in parallel code - InputStream and Reader have no commonality) just
> because of StringReader.
>
> You should be able to pass the input stream from HTTP request to the
> RDFDataMgr or model.read directly.
>
>         Andy
>
>> Phil
>
>

Re: Consuming JSON-LD?

Posted by Andy Seaborne <an...@apache.org>.
> I've created JENA-589 to add Reader-ness.

Done.

	Andy


Re: Consuming JSON-LD?

Posted by Andy Seaborne <an...@apache.org>.
On 15/11/13 10:02, Stian Soiland-Reyes wrote:
> Could there be not non-String-Readers coming from more
> character-set-correct environments beyond files and network streams?
> E.g. databases or other libraries?  Readers allow the string provider
> to also do streaming from the source, like we do internally within
> Riot of the statements.

Anything is possible ... although only providing Readers would be a bit 
strange.

I've created JENA-589 to add Reader-ness.

But the projects experience from (RDF/)XML is that this is in itself is 
trouble because Windows users pass in FileReaders that have the wrong 
charset set.  At that point, there is nothing that can be done to fix 
the problem.  Result - potentially corrupt data.  XML is particularly 
difficult because of in-content processing instructions.

The right use of Readers is "short-range" - they are used inside RIOT. 
But they are under the control of the parser system that knows the 
charset for each syntax (UTF-8).

	Andy

>
> On 15 November 2013 09:01, Andy Seaborne <an...@apache.org> wrote:
>> On 15/11/13 05:59, Phillip Rhodes wrote:
>>>
>>> FWIW... I just grabbed the Jena source, imported it into my workspace
>>> and set my project to reference that, so I could debug into this as
>>> it's running. What I see is the code getting to line 864 in RDFDataMgr
>>> and executing this code:
>>>
>>> parser = RiotReader.createParser(tokenizer, lang, base, output) ;
>>>
>>> which returns null.  The next line tries to use the null reference,
>>> and hence the NPE.
>>>
>>> Looking at RiotReader.createParser(), I don't see anything in there
>>> that mentions JSON-LD at all.    But the docs for java-jsonld say
>>> something like:
>>>
>>> "JenaJSONLD must be initialized so that the readers and writers are
>>> registered with Jena."  and I do call the init() method.  But I don't
>>> see how that registration process plays with this createParser() code.
>>>
>>> I'm guessing things have just gotten out of sync between Jena and the
>>> JSON-LD stuff?
>>
>>
>> The problem is reading from a reader (I should have noticed this earlier in
>> the thread).
>>
>> If you use an InputStream it will work (= it does for me).
>>
>> Readers are troublesome because they fix the charset before Jena gets a
>> chance set the encodign according to syntax.  A FileReader is particularly
>> troublesome because it fixes the charset as the system default so if that is
>> not UTF-8 (and it isn't on Windows) it will not be UTF-8.
>>
>> StringReader is only usecase but it currently follows a hardwired path
>> inside RDFDataMgr.  The general ReaderRIOT interface does not have Readers.
>>
>> Regrettably, I think I'll have to go and add Readers to ReaderRIOT (it only
>> results in parallel code - InputStream and Reader have no commonality) just
>> because of StringReader.
>>
>> You should be able to pass the input stream from HTTP request to the
>> RDFDataMgr or model.read directly.
>>
>>          Andy
>>
>>> Phil
>>
>>
>
>
>


Re: Consuming JSON-LD?

Posted by Stian Soiland-Reyes <so...@cs.manchester.ac.uk>.
Could there be not non-String-Readers coming from more
character-set-correct environments beyond files and network streams?
E.g. databases or other libraries?  Readers allow the string provider
to also do streaming from the source, like we do internally within
Riot of the statements.

On 15 November 2013 09:01, Andy Seaborne <an...@apache.org> wrote:
> On 15/11/13 05:59, Phillip Rhodes wrote:
>>
>> FWIW... I just grabbed the Jena source, imported it into my workspace
>> and set my project to reference that, so I could debug into this as
>> it's running. What I see is the code getting to line 864 in RDFDataMgr
>> and executing this code:
>>
>> parser = RiotReader.createParser(tokenizer, lang, base, output) ;
>>
>> which returns null.  The next line tries to use the null reference,
>> and hence the NPE.
>>
>> Looking at RiotReader.createParser(), I don't see anything in there
>> that mentions JSON-LD at all.    But the docs for java-jsonld say
>> something like:
>>
>> "JenaJSONLD must be initialized so that the readers and writers are
>> registered with Jena."  and I do call the init() method.  But I don't
>> see how that registration process plays with this createParser() code.
>>
>> I'm guessing things have just gotten out of sync between Jena and the
>> JSON-LD stuff?
>
>
> The problem is reading from a reader (I should have noticed this earlier in
> the thread).
>
> If you use an InputStream it will work (= it does for me).
>
> Readers are troublesome because they fix the charset before Jena gets a
> chance set the encodign according to syntax.  A FileReader is particularly
> troublesome because it fixes the charset as the system default so if that is
> not UTF-8 (and it isn't on Windows) it will not be UTF-8.
>
> StringReader is only usecase but it currently follows a hardwired path
> inside RDFDataMgr.  The general ReaderRIOT interface does not have Readers.
>
> Regrettably, I think I'll have to go and add Readers to ReaderRIOT (it only
> results in parallel code - InputStream and Reader have no commonality) just
> because of StringReader.
>
> You should be able to pass the input stream from HTTP request to the
> RDFDataMgr or model.read directly.
>
>         Andy
>
>> Phil
>
>



-- 
Stian Soiland-Reyes, myGrid team
School of Computer Science
The University of Manchester
http://soiland-reyes.com/stian/work/ http://orcid.org/0000-0001-9842-9718

Re: Consuming JSON-LD?

Posted by Andy Seaborne <an...@apache.org>.
On 15/11/13 05:59, Phillip Rhodes wrote:
> FWIW... I just grabbed the Jena source, imported it into my workspace
> and set my project to reference that, so I could debug into this as
> it's running. What I see is the code getting to line 864 in RDFDataMgr
> and executing this code:
>
> parser = RiotReader.createParser(tokenizer, lang, base, output) ;
>
> which returns null.  The next line tries to use the null reference,
> and hence the NPE.
>
> Looking at RiotReader.createParser(), I don't see anything in there
> that mentions JSON-LD at all.    But the docs for java-jsonld say
> something like:
>
> "JenaJSONLD must be initialized so that the readers and writers are
> registered with Jena."  and I do call the init() method.  But I don't
> see how that registration process plays with this createParser() code.
>
> I'm guessing things have just gotten out of sync between Jena and the
> JSON-LD stuff?

The problem is reading from a reader (I should have noticed this earlier 
in the thread).

If you use an InputStream it will work (= it does for me).

Readers are troublesome because they fix the charset before Jena gets a 
chance set the encodign according to syntax.  A FileReader is 
particularly troublesome because it fixes the charset as the system 
default so if that is not UTF-8 (and it isn't on Windows) it will not be 
UTF-8.

StringReader is only usecase but it currently follows a hardwired path 
inside RDFDataMgr.  The general ReaderRIOT interface does not have Readers.

Regrettably, I think I'll have to go and add Readers to ReaderRIOT (it 
only results in parallel code - InputStream and Reader have no 
commonality) just because of StringReader.

You should be able to pass the input stream from HTTP request to the 
RDFDataMgr or model.read directly.

	Andy

> Phil


Re: Consuming JSON-LD?

Posted by Phillip Rhodes <mo...@gmail.com>.
FWIW... I just grabbed the Jena source, imported it into my workspace
and set my project to reference that, so I could debug into this as
it's running. What I see is the code getting to line 864 in RDFDataMgr
and executing this code:

parser = RiotReader.createParser(tokenizer, lang, base, output) ;

which returns null.  The next line tries to use the null reference,
and hence the NPE.

Looking at RiotReader.createParser(), I don't see anything in there
that mentions JSON-LD at all.    But the docs for java-jsonld say
something like:

"JenaJSONLD must be initialized so that the readers and writers are
registered with Jena."  and I do call the init() method.  But I don't
see how that registration process plays with this createParser() code.

I'm guessing things have just gotten out of sync between Jena and the
JSON-LD stuff?


Phil
This message optimized for indexing by NSA PRISM


On Fri, Nov 15, 2013 at 12:01 AM, Phillip Rhodes
<mo...@gmail.com> wrote:
>> FWIW, I just tried again with Jena 2.11.0 and jsonld-java built from
>> the latest Github sources as of yesterday, and I still get what
>> appears to be the same exception.
>
>
> Urgh, mis-spoke there... I meant to say "used jena-jsonld built from
> the latest..."
>
> That said, I just switched it out to not use jena-jsonld and to use
> java-jsonld directly (built from the latest sources), per the earlier
> email, and it's still failing in the same place.  This was also using
> jena-2.11.0.
>
>
> Phil

Re: Consuming JSON-LD?

Posted by Phillip Rhodes <mo...@gmail.com>.
> FWIW, I just tried again with Jena 2.11.0 and jsonld-java built from
> the latest Github sources as of yesterday, and I still get what
> appears to be the same exception.


Urgh, mis-spoke there... I meant to say "used jena-jsonld built from
the latest..."

That said, I just switched it out to not use jena-jsonld and to use
java-jsonld directly (built from the latest sources), per the earlier
email, and it's still failing in the same place.  This was also using
jena-2.11.0.


Phil

Re: Consuming JSON-LD?

Posted by Phillip Rhodes <mo...@gmail.com>.
On Thu, Nov 14, 2013 at 5:05 AM, Andy Seaborne <an...@apache.org> wrote:
> It may be a version issue.  The code on githib uses 2.11.0 (my code is > for jsonld-java v 0.2).


FWIW, I just tried again with Jena 2.11.0 and jsonld-java built from
the latest Github sources as of yesterday, and I still get what
appears to be the same exception.

Caught: java.lang.NullPointerException
java.lang.NullPointerException
at org.apache.jena.riot.RDFDataMgr.processTriples(RDFDataMgr.java:852)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:302)

I'm going to switch off to trying the approach from the link in
Stian's earlier email and see how that goes.


Phil



This message optimized for indexing by NSA PRISM

Re: Consuming JSON-LD?

Posted by Andy Seaborne <an...@apache.org>.
Phillip Rhodes wrote:
 >> I'm using Jena 2.10.1

It may be a version issue.  The code on githib uses 2.11.0 (my code is 
for jsonld-java v 0.2).

With both jsonld-java and Jena's RDFDataMgr changing internally, version 
alignment has been essential to get something working.  Hopefully, 
things will settle down now.  JSON-LD has itself only become stable very 
recently so if 0.3 catches up everythign will be simpler (tm).

	Andy



Re: Consuming JSON-LD?

Posted by Phillip Rhodes <mo...@gmail.com>.
Ah, OK.  I saw that project, but didn't realize that was the more
appropriate one.  I'll give that a go tomorrow.   Thanks for the heads-up!


Phil
This message optimized for indexing by NSA PRISM


On Thu, Nov 14, 2013 at 2:48 AM, Stian Soiland-Reyes
<so...@cs.manchester.ac.uk> wrote:
> Try instead:
>
> https://github.com/jsonld-java/jsonld-java/tree/master/integration/jena
>
> Instructions and examples above.
>
> You will currently have to build jsonld-java from github , until version
> 0.3.0 is released.
> On 14 Nov 2013 00:47, "Phillip Rhodes" <mo...@gmail.com> wrote:
>
>> Jena gang:
>>
>> I am trying to store triples retrieved from Stanbol in JSON-LD format,
>> but having
>> no luck so far.  I'm using Jena 2.10.1 and using the Jena JSONLD project
>> from
>> https://github.com/afs/jena-jsonld
>>
>> My (Groovy) code looks like this:
>>
>>
>> static main(args)
>> {
>>     JenaJSONLD.init();
>>     def content = """Quoddy can now recognize famous people like
>> Richard Marx""";
>>
>>     // call Stanbol REST API to get enrichment data
>>     RESTClient restClient = new RESTClient( "http://localhost:8080" )
>>
>>     // println "content submitted: ${content}";
>>     def restResponse = restClient.post( path:'enhancer',
>>                                                          body: content,
>>                                                       // headers:
>> ['Accept':'application/rdf+xml'],
>>
>> requestContentType : TEXT );
>>
>>     String restResponseText = restResponse.getData();
>>     println "restResponseText:\n ${restResponseText}\n\n";
>>
>>     // Make a TDB-backed dataset
>>     String directory = "MyDatabases/Dataset2" ;
>>     Dataset dataset = TDBFactory.createDataset(directory) ;
>>     dataset.begin(ReadWrite.READ) ;
>>     // Get model inside the transaction
>>     Model model = dataset.getDefaultModel() ;
>>     dataset.end() ;
>>
>>     dataset.begin(ReadWrite.WRITE) ;
>>     model = dataset.getDefaultModel() ;
>>     StringReader reader = new StringReader( restResponseText );
>>     model.read( reader, "http://www.example.com", "JSON-LD" );
>>     // RDFDataMgr.read(model, reader, "http://example.com/",
>> JenaJSONLD.JSONLD);
>>     dataset.commit();
>>     dataset.end();
>>
>>     println "done";
>> }
>>
>> and results in a NullPointerException like this:
>>
>> Caught: java.lang.NullPointerException
>> java.lang.NullPointerException
>> at org.apache.jena.riot.RDFDataMgr.processTriples(RDFDataMgr.java:796)
>> at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:289)
>> at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:273)
>> at org.apache.jena.riot.adapters.RDFReaderRIOT.read(RDFReaderRIOT.java:61)
>> at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:267)
>> at com.hp.hpl.jena.rdf.model.Model$read.call(Unknown Source)
>> at org.example.stanbol.StanbolJenaMain2.main(StanbolJenaMain2.groovy:58)
>>
>>
>> If I do the same thing with RDF/XML (and appropriate code changes) I
>> can save the triples fine.  But I really want to use JSON-LD, as I do
>> other stuff with the JSON-LD
>> that comes back, using Javascript on the client-side, and it makes
>> life a lot easier
>> if I just take the JSON-LD and use it everywhere.
>>
>>
>> Any thoughts? Am I doing something wrong, or is there just a bug in
>> the JSONLD module?  I can also post the blog of json that I get back
>> if it would help.
>>
>>
>> Thanks,
>>
>>
>> Phil
>>
>> This message optimized for indexing by NSA PRISM
>>

Re: Consuming JSON-LD?

Posted by Stian Soiland-Reyes <so...@cs.manchester.ac.uk>.
Try instead:

https://github.com/jsonld-java/jsonld-java/tree/master/integration/jena

Instructions and examples above.

You will currently have to build jsonld-java from github , until version
0.3.0 is released.
On 14 Nov 2013 00:47, "Phillip Rhodes" <mo...@gmail.com> wrote:

> Jena gang:
>
> I am trying to store triples retrieved from Stanbol in JSON-LD format,
> but having
> no luck so far.  I'm using Jena 2.10.1 and using the Jena JSONLD project
> from
> https://github.com/afs/jena-jsonld
>
> My (Groovy) code looks like this:
>
>
> static main(args)
> {
>     JenaJSONLD.init();
>     def content = """Quoddy can now recognize famous people like
> Richard Marx""";
>
>     // call Stanbol REST API to get enrichment data
>     RESTClient restClient = new RESTClient( "http://localhost:8080" )
>
>     // println "content submitted: ${content}";
>     def restResponse = restClient.post( path:'enhancer',
>                                                          body: content,
>                                                       // headers:
> ['Accept':'application/rdf+xml'],
>
> requestContentType : TEXT );
>
>     String restResponseText = restResponse.getData();
>     println "restResponseText:\n ${restResponseText}\n\n";
>
>     // Make a TDB-backed dataset
>     String directory = "MyDatabases/Dataset2" ;
>     Dataset dataset = TDBFactory.createDataset(directory) ;
>     dataset.begin(ReadWrite.READ) ;
>     // Get model inside the transaction
>     Model model = dataset.getDefaultModel() ;
>     dataset.end() ;
>
>     dataset.begin(ReadWrite.WRITE) ;
>     model = dataset.getDefaultModel() ;
>     StringReader reader = new StringReader( restResponseText );
>     model.read( reader, "http://www.example.com", "JSON-LD" );
>     // RDFDataMgr.read(model, reader, "http://example.com/",
> JenaJSONLD.JSONLD);
>     dataset.commit();
>     dataset.end();
>
>     println "done";
> }
>
> and results in a NullPointerException like this:
>
> Caught: java.lang.NullPointerException
> java.lang.NullPointerException
> at org.apache.jena.riot.RDFDataMgr.processTriples(RDFDataMgr.java:796)
> at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:289)
> at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:273)
> at org.apache.jena.riot.adapters.RDFReaderRIOT.read(RDFReaderRIOT.java:61)
> at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:267)
> at com.hp.hpl.jena.rdf.model.Model$read.call(Unknown Source)
> at org.example.stanbol.StanbolJenaMain2.main(StanbolJenaMain2.groovy:58)
>
>
> If I do the same thing with RDF/XML (and appropriate code changes) I
> can save the triples fine.  But I really want to use JSON-LD, as I do
> other stuff with the JSON-LD
> that comes back, using Javascript on the client-side, and it makes
> life a lot easier
> if I just take the JSON-LD and use it everywhere.
>
>
> Any thoughts? Am I doing something wrong, or is there just a bug in
> the JSONLD module?  I can also post the blog of json that I get back
> if it would help.
>
>
> Thanks,
>
>
> Phil
>
> This message optimized for indexing by NSA PRISM
>