Skip to content

Commit

Permalink
JENA-1948: JSON-LD 1.1 - reader and writer
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Aug 10, 2021
1 parent 0cd8cc3 commit 469251e
Show file tree
Hide file tree
Showing 17 changed files with 706 additions and 186 deletions.
2 changes: 2 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class Lang
/** <a href="http://www.w3.org/TR/json-ld/">JSON-LD</a>. */
public static Lang JSONLD ;

//public static Lang JSONLD10 ;

public static Lang JSONLD11 ;

/**
Expand Down
11 changes: 11 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/RDFFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ public class RDFFormat {
// JSONLD related
//

// ---- JSONLD 1.1 / Titanium

/** JSON LD 1.1 - multi-line JSON */
public static RDFFormat JSONLD11_PLAIN = new RDFFormat(Lang.JSONLD11, RDFFormat.PLAIN);
/** JSON LD 1.1 - single-line JSON */
public static RDFFormat JSONLD11_FLAT = new RDFFormat(Lang.JSONLD11, RDFFormat.FLAT);
/** JSON LD 1.1 default form - multi-line JSON */
public static RDFFormat JSONLD11 = JSONLD11_PLAIN;

// ---- JSONLD 1.0 / jsonld-java

public static class JSONLDVariant extends RDFFormatVariant {
private static enum JSONLD_FORMAT {
COMPACT,
Expand Down
25 changes: 18 additions & 7 deletions jena-arq/src/main/java/org/apache/jena/riot/RDFLanguages.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.jena.atlas.logging.Log;
import org.apache.jena.atlas.web.ContentType;
import org.apache.jena.atlas.web.MediaType;
import org.apache.jena.riot.lang.LangJSONLD11;
import org.apache.jena.util.FileUtils;

/**
Expand Down Expand Up @@ -90,22 +89,35 @@ public class RDFLanguages
/** Alternative constant for {@link #NTRIPLES} */
public static final Lang NT = NTRIPLES;

/** <a href="http://www.w3.org/TR/json-ld/">JSON-LD</a>. */
/** The standard JSON-LD registration.
* <a href="http://www.w3.org/TR/json-ld/">JSON-LD</a>. */
public static final Lang JSONLD = LangBuilder.create(strLangJSONLD, "application/ld+json")
.addAltNames("JSONLD")
.addFileExtensions("jsonld")
.build();

/*
* Short term - have JSON-LD as an additional language for reading only
* and requiring an explicit language name {@code RDFParser.forceLang(Lang.JSONLD11)...}
* Override for JSON-LD 1.1 - requires an explicit language name
* {@code RDFParser.forceLang(Lang.JSONLD11)...}
* or use of the file extensions {@code .jsonld11}
*/
public static final String strLangJSONLD11 = "JSON-LD-11";
public static final Lang JSONLD11 = LangBuilder.create(strLangJSONLD11, "x/ld-json-11")
.addAltNames("JSONLD11")
.addFileExtensions("jsonld11")
.build();

/*
* Override for JSON-LD 1.0 - requires an explicit language name
* {@code RDFParser.forceLang(Lang.JSONLD10)...}
* or use of the file extensions {@code .jsonld10}
*/
public static final String strLangJSONLD10 = "JSON-LD-10";
public static final Lang JSONLD10 = LangBuilder.create(strLangJSONLD10, "x/ld-json-10")
.addAltNames("JSONLD10")
.addFileExtensions("jsonld10")
.build();

/** <a href="http://www.w3.org/TR/rdf-json/">RDF/JSON</a>. This is not <a href="http://www.w3.org/TR/json-ld/">JSON-LD</a>. */
public static final Lang RDFJSON = LangBuilder.create(strLangRDFJSON, contentTypeRDFJSON)
.addAltNames("RDFJSON")
Expand Down Expand Up @@ -188,6 +200,7 @@ public static void init() {}
Lang.TURTLE = RDFLanguages.TURTLE;
Lang.TTL = RDFLanguages.TTL;
Lang.JSONLD = RDFLanguages.JSONLD;
//Lang.JSONLD10 = RDFLanguages.JSONLD10;
Lang.JSONLD11 = RDFLanguages.JSONLD11;
Lang.RDFJSON = RDFLanguages.RDFJSON;
Lang.NQUADS = RDFLanguages.NQUADS;
Expand All @@ -207,9 +220,6 @@ public static void init() {}
.addAltNames("tsv")
.addFileExtensions("tsv")
.build();

// JSON-LD 11
LangJSONLD11.Sys.init();
}
// ----------------------

Expand All @@ -220,6 +230,7 @@ private static void initStandard() {
register(N3);
register(NTRIPLES);
register(JSONLD);
register(JSONLD10);
register(JSONLD11);
register(RDFJSON);
register(TRIG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ private static void initStandard() {
registerLangTriples(N3, parserFactory);
registerLangTriples(TURTLE, parserFactory);
registerLangTriples(RDFJSON, parserFactory);
registerLangTriples(RDFXML, ReaderRIOTRDFXML.factory);
registerLangTriples(RDFXML, parserFactoryRDFXML);
registerLangTriples(JSONLD, parserFactoryJsonLD);
registerLangTriples(RDFTHRIFT, ReaderRDFThrift.factory);
registerLangTriples(TRIX, ReaderTriX.factory);
registerLangTriples(RDFNULL, ReaderRDFNULL.factory);
registerLangTriples(RDFTHRIFT, parserFactoryTriX);
registerLangTriples(TRIX, parserFactoryTriX);
registerLangTriples(RDFNULL, parserFactoryRDFNULL);

registerLangQuads(JSONLD, parserFactoryJsonLD);
registerLangQuads(NQUADS, parserFactory);
Expand All @@ -98,6 +98,11 @@ private static void initStandard() {

// Javacc based Turtle parser, different language name.
TurtleJCC.register();

// Currently done in currently done in SysJSONLD11.init.
//registerLangTriples(Lang.JSONLD11, jsonld11ReaderFactory);
//registerLangQuads(Lang.JSONLD11, jsonld11ReaderFactory);

}

/**
Expand Down
63 changes: 34 additions & 29 deletions jena-arq/src/main/java/org/apache/jena/riot/RDFWriterRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,21 @@ public WriterGraphRIOT create(RDFFormat serialization)
}} ;

// Writing a dataset
static WriterDatasetRIOTFactory wdsfactory = new WriterDatasetRIOTFactory() {
@Override
public WriterDatasetRIOT create(RDFFormat serialization)
{
if ( Objects.equals(RDFFormat.TRIG_PRETTY, serialization) )
return new TriGWriter() ;
if ( Objects.equals(RDFFormat.TRIG_BLOCKS, serialization) )
return new TriGWriterBlocks() ;
if ( Objects.equals(RDFFormat.TRIG_FLAT, serialization) )
return new TriGWriterFlat() ;
if ( Objects.equals(RDFFormat.NQUADS_UTF8, serialization) )
return new NQuadsWriter() ;
if ( Objects.equals(RDFFormat.NQUADS_ASCII, serialization) )
return new NQuadsWriter(CharSpace.ASCII) ;
if ( Objects.equals(RDFFormat.RDFNULL, serialization) )
return NullWriter.factory.create(RDFFormat.RDFNULL) ;
return null ;
}} ;
static WriterDatasetRIOTFactory wdsfactory = (RDFFormat serialization) -> {
if ( Objects.equals(RDFFormat.TRIG_PRETTY, serialization) )
return new TriGWriter() ;
if ( Objects.equals(RDFFormat.TRIG_BLOCKS, serialization) )
return new TriGWriterBlocks() ;
if ( Objects.equals(RDFFormat.TRIG_FLAT, serialization) )
return new TriGWriterFlat() ;
if ( Objects.equals(RDFFormat.NQUADS_UTF8, serialization) )
return new NQuadsWriter() ;
if ( Objects.equals(RDFFormat.NQUADS_ASCII, serialization) )
return new NQuadsWriter(CharSpace.ASCII) ;
if ( Objects.equals(RDFFormat.RDFNULL, serialization) )
return NullWriter.factory.create(RDFFormat.RDFNULL) ;
return null ;
} ;

private static WriterDatasetRIOTFactory wdsJsonldfactory = syntaxForm -> new JsonLDWriter(syntaxForm);
private static WriterGraphRIOTFactory wgJsonldfactory = syntaxForm -> RiotLib.adapter(new JsonLDWriter(syntaxForm));
Expand All @@ -108,17 +105,19 @@ public WriterDatasetRIOT create(RDFFormat serialization)
private static WriterGraphRIOTFactory wgTriXFactory = syntaxForm -> new WriterTriX();
private static WriterDatasetRIOTFactory wdsTriXFactory = syntaxForm -> new WriterTriX() ;

public static void init() {}
static { init$() ; }
private static void init$()
{
// Language to format.
register(Lang.TURTLE, RDFFormat.TURTLE) ;
register(Lang.N3, RDFFormat.TURTLE) ;
register(Lang.NTRIPLES, RDFFormat.NTRIPLES) ;
public static void init() {}
static { init$() ; }
private static void init$()
{
// Language to format.
register(Lang.TURTLE, RDFFormat.TURTLE) ;
register(Lang.N3, RDFFormat.TURTLE) ;
register(Lang.NTRIPLES, RDFFormat.NTRIPLES) ;
register(Lang.RDFXML, RDFFormat.RDFXML) ;

register(Lang.JSONLD, RDFFormat.JSONLD) ;
//register(Lang.JSONLD10, RDFFormat.JSONLD) ;
register(Lang.JSONLD11, RDFFormat.JSONLD11) ;
register(Lang.RDFJSON, RDFFormat.RDFJSON) ;

register(Lang.TRIG, RDFFormat.TRIG) ;
Expand All @@ -137,6 +136,14 @@ public static void init() {}
register(RDFFormat.NTRIPLES, wgfactory) ;
register(RDFFormat.NTRIPLES_ASCII, wgfactory) ;

// Writer.

// JSONLD 1.1 -- currently done in SysJSONLD11.init.
//register(RDFFormat.JSONLD11_PLAIN, jsonld11WriterGraphFactory);
//register(RDFFormat.JSONLD11_PLAIN, jsonld11WriterDatasetFactory);
//register(RDFFormat.JSONLD11_FLAT, jsonld11WriterGraphFactory);
//register(RDFFormat.JSONLD11_FLAT, jsonld11WriterDatasetFactory);

register(RDFFormat.JSONLD, wgJsonldfactory) ;
register(RDFFormat.JSONLD_FLAT, wgJsonldfactory) ;
register(RDFFormat.JSONLD_PRETTY, wgJsonldfactory) ;
Expand Down Expand Up @@ -201,8 +208,7 @@ public static void init() {}
* @param serialization RDFFormat for the output format.
* @param graphWriterFactory Source of writer engines
*/
public static void register(RDFFormat serialization, WriterGraphRIOTFactory graphWriterFactory)
{
public static void register(RDFFormat serialization, WriterGraphRIOTFactory graphWriterFactory) {
registryGraph.put(serialization, graphWriterFactory) ;
}

Expand Down Expand Up @@ -270,7 +276,6 @@ public static Collection<RDFFormat> registered() {
return Collections.unmodifiableSet(x) ;
}


/** Get the graph writer factory associated with the language */
public static WriterGraphRIOTFactory getWriterGraphFactory(Lang lang) {
RDFFormat serialization = defaultSerialization(lang) ;
Expand Down
4 changes: 4 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/RIOT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.jena.query.ARQ ;
import org.apache.jena.riot.resultset.ResultSetLang;
import org.apache.jena.riot.system.StreamRDF;
import org.apache.jena.riot.system.SysJSONLD11;
import org.apache.jena.sparql.SystemARQ ;
import org.apache.jena.sparql.mgt.SystemInfo ;
import org.apache.jena.sparql.util.Context ;
Expand Down Expand Up @@ -79,6 +80,9 @@ public static void init() {

IO_Jena.wireIntoJena() ;

// JSON-LD 11 while still an optional feature.
SysJSONLD11.init();

// Don't register JMX info with ARQ as it may not be initialized
// itself and we can get into a circularity.
// This is done in ARQ.init at the proper moment.
Expand Down
Loading

0 comments on commit 469251e

Please sign in to comment.