[
https://issues.apache.org/jira/browse/JENA-1031?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14954993#comment-14954993
]
Gert van Valkenhoef commented on JENA-1031:
-------------------------------------------
Sorry for the double post, but I just realized that a much simpler example
would demonstrate the same issue without confounding it by a JavaScript
processing step.
{code:java|title=test/JenaTest2.java}
package test;
import java.io.FileReader;
import java.io.IOException;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFLanguages;
import org.junit.Test;
public class JenaTest2 {
public void testUsingFile(String fileName) throws IOException {
Model model = ModelFactory.createDefaultModel();
model.read(new FileReader(fileName), "http://example.com",
RDFLanguages.strLangJSONLD);
System.out.println(model.getProperty(
model.getResource("http://example.com/a"),
model.getProperty("http://example.com/ontology#generated_at")));
}
@Test
public void test() throws IOException {
Model model = ModelFactory.createDefaultModel();
model.read(new FileReader("test-date.ttl"),
"http://example.com", RDFLanguages.strLangTurtle);
RDFDataMgr.write(System.out, model, RDFLanguages.JSONLD);
System.out.println("\n=====================================");
System.out.println("Example JSON failing to use aliases");
testUsingFile("test3.json");
System.out.println("\n=====================================");
System.out.println("Example JSON using aliases");
testUsingFile("test4.json");
}
}
{code}
{code:none|title=test-date.ttl}
<a> <http://example.com/ontology#generated_at>
"2015-10-13"^^<http://www.w3.org/2001/XMLSchema#date> .
{code}
{code:none|title=test3.json}
{
"@id" : "http://example.com/a",
"http://example.com/ontology#generated_at": "2015-10-13",
"@context" : {
"generated_at" : {
"@id" : "http://example.com/ontology#generated_at",
"@type" : "http://www.w3.org/2001/XMLSchema#date"
}
}
}
{code}
{code:none|title=test4.json}
{
"@id" : "http://example.com/a",
"generated_at": "2015-10-13",
"@context" : {
"generated_at" : {
"@id" : "http://example.com/ontology#generated_at",
"@type" : "http://www.w3.org/2001/XMLSchema#date"
}
}
}
{code}
The turtle will result in the JSON LD shown in test3.json. The file test4.json
is identical to test3.json except that the alias for generated_at is actually
used. This is the test output:
{code:none}
{
"@id" : "http://example.com/a",
"generated_at" : "2015-10-13",
"@context" : {
"generated_at" : {
"@id" : "http://example.com/ontology#generated_at",
"@type" : "http://www.w3.org/2001/XMLSchema#date"
}
}
}
=====================================
Example JSON failing to use aliases
[http://example.com/a, http://example.com/ontology#generated_at, "2015-10-13"]
=====================================
Example JSON using aliases
[http://example.com/a, http://example.com/ontology#generated_at,
"2015-10-13"^^http://www.w3.org/2001/XMLSchema#date]
{code}
As you can see, the JSON LD generated by Jena will result in the loss of the
data type information when parsed again by Jena (although Jena will accept both
parses as isomorphic because the values are identical irrespective of the data
type - I couldn't say if this is correct or not).
> Aliases for data properties not used
> ------------------------------------
>
> Key: JENA-1031
> URL: https://issues.apache.org/jira/browse/JENA-1031
> Project: Apache Jena
> Issue Type: Bug
> Components: Jena
> Affects Versions: Jena 3.0.0
> Environment: Ubuntu 14.04 64bit, IntelliJ IDEA, JDK 8
> Reporter: Gert van Valkenhoef
> Assignee: Andy Seaborne
> Priority: Minor
>
> When JSON-LD is generated for a graph, the @context will contain property
> aliases for all properties used in the graph. However, for properties that
> have a data value (rather than a resource), these aliases are not used, and
> the properties in the @graph are still their full URIs. This is harmful when
> the @context also specifies a data type. For example, doubles can be
> converted to integer by accident. The example turtle below results in JSON-LD
> that when parsed by Jena is not isomorphic with the graph parsed from turtle
> (because the doubleValuedProperty will get an integer value):
> {code:title=test.ttl}
> <a> <http://www.w3.org/2000/01/rdf-schema#label> "b".
> <a> <http://example.com/ontology#doubleValuedProperty> 3.0e1 .
> {code}
> The JSON-LD as output by Jena:
> {code:javascript}
> {
> "@id" : "http://example.com/a",
> "http://example.com/ontology#doubleValuedProperty" : 30.0,
> "http://www.w3.org/2000/01/rdf-schema#label" : "b",
> "@context" : {
> "doubleValuedProperty" : {
> "@id" : "http://example.com/ontology#doubleValuedProperty",
> "@type" : "http://www.w3.org/2001/XMLSchema#double"
> },
> "label" : {
> "@id" : "http://www.w3.org/2000/01/rdf-schema#label",
> "@type" : "http://www.w3.org/2001/XMLSchema#string"
> }
> }
> }
> {code}
> A failing unit test for round-trip through JSON-LD:
> {code:java}
> @Test
> public void jenaBugTest() throws IOException {
> Model model = ModelFactory.createDefaultModel();
> model.read(new FileReader("test.ttl"), "http://example.com",
> RDFLanguages.strLangTurtle);
> StringWriter writer = new StringWriter();
> RDFDataMgr.write(writer, model, RDFLanguages.JSONLD);
> writer.close();
> System.out.println(writer.toString());
> Model model2 = ModelFactory.createDefaultModel();
> model2.read(new StringReader(writer.toString()), "http://example.com",
> RDFLanguages.strLangJSONLD);
> assertTrue(model.isIsomorphicWith(model2));
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)