Hi fps,
Jena uses https://github.com/jsonld-java/jsonld-java/, the core module,
for JSON-LD.
test1 and test2 seems to be about the process that is done by that code.
Do they have issues raised? Or is it a Jena issue? (if so, any idea
what's wrong with the setup passed to jsonld-java?)
Jena currently use 0.5.1 which is the latest release. Maybe the
development does better?
For test3, "" is overloaded between use as base and the empty string
prefix. JSON-LD mixes the two up.
We can at least not pass on the "" prefix - will that get legal output?
For now, I have put in code to skip "".
Andy
On 05/05/15 11:02, François-Paul Servant wrote:
Hi,
I find this very cool: when outputing to JSON-LD, setNSPrefix can be used to
benefit from JSON-LD's unorthodox but very nice use of “namespaces”.
I have some minor related remarks, summarized in the following tests. test1()
is the most annoying one, in my point of view.
(Tested with latest Jena)
Best,
fps
package sometests;
import org.junit.Test;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
public class JsonLDWriterTest {
@Test
/**
* setNSPrefix works great wrt JSON-LD very nice interpretation of "namespaces"
* That's cool! (No problem here) */
public final void test0() {
Model m = ModelFactory.createDefaultModel();
String url = "http://www.a.com/foo?";
Resource s = m.createResource(url + "param=s");
Property p = m.createProperty("http://www.a.com/p");
Resource o = m.createResource(url + "param=o");
m.add(s,p,o);
m.setNsPrefix("ex",url);
m.write(System.out, "JSON-LD");
System.out.println();
m.write(System.out, "TTL");
}
@Test
/**
* if the URI of a resource exactly matches one prefix,
* its @id is set to its long URI, not to the short form "prefix:"
*/
public final void test1() {
Model m = ModelFactory.createDefaultModel();
String url = "http://www.a.com/foo";
Resource s = m.createResource(url);
Property p = m.createProperty(url + "/p");
Resource o = m.createResource(url + "/o");
m.add(s,p,o);
m.setNsPrefix("ex",url);
m.write(System.out, "JSON-LD");
// we get:
/*
{
"@id" : "http://www.a.com/foo",
"p" : "ex:/o",
"@context" : {
"p" : {
"@id" : "http://www.a.com/foo/p",
"@type" : "@id"
},
"ex" : "http://www.a.com/foo"
}
}
*/
// why not:
/*
{
"@id" : "ex:",
"p" : "ex:/o",
"@context" : {
"p" : {
"@id" : "http://www.a.com/foo/p",
"@type" : "@id"
},
"ex" : "http://www.a.com/foo"
}
}
*/
System.out.println("no such problem in turtle:");
m.write(System.out, "TTL");
}
@Test
/** short URIs are not used inside the @context node */
public final void test2() {
Model m = ModelFactory.createDefaultModel();
// String url = "http://www.a.com/foo/s?param1=val1¶m2=val2";
String url = "http://www.a.com/foo/";
Resource s = m.createResource(url + "s");
Property p = m.createProperty(url + "p");
Resource o = m.createResource(url + "o");
m.add(s,p,o);
m.setNsPrefix("ex",url);
m.write(System.out, "JSON-LD");
System.out.println();
// we get
/*
{
"@id" : "ex:s",
"p" : "ex:o",
"@context" : {
"p" : {
"@id" : "http://www.a.com/foo/p",
"@type" : "@id"
},
"ex" : "http://www.a.com/foo/"
}
}
*/
// why not:
/*
{
"@id" : "ex:s",
"p" : "ex:o",
"@context" : {
"p" : {
"@id" : "ex:p",
"@type" : "@id"
},
"ex" : "http://www.a.com/foo/"
}
}
*/
}
@Test
/**
* if a prefix is set to "", we get an output that is not correct JSON-LD
* (according to the JSON-LD playground: "Invalid JSON-LD syntax; a term cannot be
an empty string."
* ld.org/playground/jsonld.js:324:25\nhttp://json-ld.org/playground/jsonld.js:303:11", "name": "jsonld.SyntaxError", "message": "Invalid JSON-LD syntax; a term cannot be an empty string.", "details": { "code": "invalid
term definition", "context": { "p": { "@id": "http://www.a.com/foo/p", "@type": "@id" }, "": "http://www.a.com/foo" } }, "line": 5207, "column": 60, "sourceURL":
"http://json-ld.org/playground/jsonld.js" } } } }, "line": 0, "column": 0 }
*/
public final void test3() {
Model m = ModelFactory.createDefaultModel();
String url = "http://www.a.com/foo/";
Resource s = m.createResource(url + "s");
Property p = m.createProperty(url + "p");
Resource o = m.createResource(url + "o");
m.add(s,p,o);
m.setNsPrefix("",url);
m.write(System.out, "JSON-LD");
System.out.println("Things are OK in Turtle: ");
m.write(System.out, "TTL");
}
}