You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@jena.apache.org by "imsdu (via GitHub)" <gi...@apache.org> on 2026/01/15 11:19:13 UTC

[PR] GH-3698: Allow JSON-LD conversion outside of read/write operations [jena]

imsdu opened a new pull request, #3702:
URL: https://github.com/apache/jena/pull/3702

   GitHub issue resolved #3698
   
   Pull request Description: Proposal to allow conversion between Jena datasets and JSON-LD and vice versa outside of read/write operations.
   
   This PR is mostly about extracting the existing code in dedicated classes to allow it.
   
   In `JsonLD11Writer`, I also introduced the JSON-LD options from the baseUri/Jena context so it is consistent with the reading part.
   
   ----
   
    - [ ] Tests are included.
    - [ ] Documentation change and updates are provided for the [Apache Jena website](https://github.com/apache/jena-site/)
    - [ ] Commits have been squashed to remove intermediate development commit messages.
    - [X] Key commit messages start with the issue number (GH-xxxx)
   
   By submitting this pull request, I acknowledge that I am making a contribution to the Apache Software Foundation under the terms and conditions of the [Contributor's Agreement](https://www.apache.org/licenses/contributor-agreements.html).
   
   ----
   
   See the [Apache Jena "Contributing" guide](https://github.com/apache/jena/blob/main/CONTRIBUTING.md).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org
For additional commands, e-mail: pr-help@jena.apache.org


Re: [PR] GH-3698: Allow JSON-LD conversion outside of read/write operations [jena]

Posted by "afs (via GitHub)" <gi...@apache.org>.
afs commented on code in PR #3702:
URL: https://github.com/apache/jena/pull/3702#discussion_r2705581171


##########
jena-arq/src/main/java/org/apache/jena/riot/system/jsonld/JenaToTitanium.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ *   SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.apache.jena.riot.system.jsonld;
+
+import com.apicatalog.jsonld.JsonLd;
+import com.apicatalog.jsonld.JsonLdError;
+import com.apicatalog.jsonld.JsonLdOptions;
+import com.apicatalog.jsonld.JsonLdVersion;
+import com.apicatalog.jsonld.serialization.QuadsToJsonld;
+import com.apicatalog.rdf.api.RdfConsumerException;
+import jakarta.json.JsonArray;
+import org.apache.jena.graph.Node;
+import org.apache.jena.shared.JenaException;
+import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.core.Quad;
+
+public class JenaToTitanium {
+
+    static class JenaTitaniumException extends JenaException {
+        JenaTitaniumException(String msg) { super(msg); }
+    }
+
+    /** Translates a Jena {@link DatasetGraph} to a JSON-LD document **/
+    public static JsonArray convert(DatasetGraph dataset, JsonLdOptions opts) throws JsonLdError {
+        QuadsToJsonld consumer = JsonLd.fromRdf()
+                .options(opts)
+                .mode(JsonLdVersion.V1_1);
+        dataset.stream().forEach( quad->{
+            String s = resource(quad.getSubject());
+            String p = resource(quad.getPredicate());
+            String g = resourceGraphName(quad.getGraph());
+            Node obj = quad.getObject();
+
+            if ( obj.isURI() || obj.isBlank() ) {
+                String o = resource(obj);
+                try {
+                    consumer.quad(s, p, o, null, null, null, g);
+                } catch (RdfConsumerException e) {
+                    e.printStackTrace();
+                }
+            } else if ( obj.isLiteral() ) {
+                String lex = obj.getLiteralLexicalForm();
+                String datatype = obj.getLiteralDatatypeURI();
+                String lang = obj.getLiteralLanguage();
+                if ( lang.isEmpty() )
+                    lang = null;
+                String dir = null;
+                if ( obj.getLiteralBaseDirection() != null )
+                    dir = obj.getLiteralBaseDirection().toString();
+                try {
+                    consumer.quad(s, p, lex, datatype, lang, dir, g);
+                } catch (RdfConsumerException ex) {
+                    throw new JenaException("Exception while translating to JSON-LD", ex);

Review Comment:
   
   ```suggestion
                       throw new JenaTitaniumException("Exception while translating to JSON-LD", ex);
   ```



##########
jena-arq/src/main/java/org/apache/jena/riot/system/jsonld/JenaToTitanium.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ *   SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.apache.jena.riot.system.jsonld;
+
+import com.apicatalog.jsonld.JsonLd;
+import com.apicatalog.jsonld.JsonLdError;
+import com.apicatalog.jsonld.JsonLdOptions;
+import com.apicatalog.jsonld.JsonLdVersion;
+import com.apicatalog.jsonld.serialization.QuadsToJsonld;
+import com.apicatalog.rdf.api.RdfConsumerException;
+import jakarta.json.JsonArray;
+import org.apache.jena.graph.Node;
+import org.apache.jena.shared.JenaException;
+import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.core.Quad;
+
+public class JenaToTitanium {
+
+    static class JenaTitaniumException extends JenaException {
+        JenaTitaniumException(String msg) { super(msg); }
+    }
+
+    /** Translates a Jena {@link DatasetGraph} to a JSON-LD document **/
+    public static JsonArray convert(DatasetGraph dataset, JsonLdOptions opts) throws JsonLdError {
+        QuadsToJsonld consumer = JsonLd.fromRdf()
+                .options(opts)
+                .mode(JsonLdVersion.V1_1);
+        dataset.stream().forEach( quad->{
+            String s = resource(quad.getSubject());
+            String p = resource(quad.getPredicate());
+            String g = resourceGraphName(quad.getGraph());
+            Node obj = quad.getObject();
+
+            if ( obj.isURI() || obj.isBlank() ) {
+                String o = resource(obj);
+                try {
+                    consumer.quad(s, p, o, null, null, null, g);
+                } catch (RdfConsumerException e) {

Review Comment:
   (yes, I know it was in the original code :-( )
   
   Please convert to a JenaTitaniumException.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org
For additional commands, e-mail: pr-help@jena.apache.org


Re: [PR] GH-3698: Allow JSON-LD conversion outside of read/write operations [jena]

Posted by "afs (via GitHub)" <gi...@apache.org>.
afs commented on PR #3702:
URL: https://github.com/apache/jena/pull/3702#issuecomment-3769423248

   This needs some tests to show that the connection between Jena and Titanium is working.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org
For additional commands, e-mail: pr-help@jena.apache.org


Re: [PR] GH-3698: Allow JSON-LD conversion outside of read/write operations [jena]

Posted by "afs (via GitHub)" <gi...@apache.org>.
afs merged PR #3702:
URL: https://github.com/apache/jena/pull/3702


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org
For additional commands, e-mail: pr-help@jena.apache.org


Re: [PR] GH-3698: Allow JSON-LD conversion outside of read/write operations [jena]

Posted by "imsdu (via GitHub)" <gi...@apache.org>.
imsdu commented on PR #3702:
URL: https://github.com/apache/jena/pull/3702#issuecomment-3777204096

   I addressed the different comments and added a test to convert from Jena to Titanium and back, which also checks for some parts of the JSON (mostly like what was done here: https://github.com/apache/jena/blob/jena-5.2.0/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java#L77)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org
For additional commands, e-mail: pr-help@jena.apache.org