Search in sources :

Example 1 with WriterDatasetRIOT

use of org.apache.jena.riot.WriterDatasetRIOT in project jena by apache.

the class ExJsonLD method write.

/**
     * Write RDF data as JSON-LD.
     * 
     * To get more control about the output,
     * we have to use a mechanism provided by jena to pass information
     * to the writing process (cf. org.apache.jena.riot.WriterDatasetRIOT and the "Context" mechanism).
     * For that, we have to create a WriterDatasetRIOT (a one-time use object)
     * and we pass a "Context" object (not to be confused with the "@context" in JSON-LD) as argument to its write method
     * 
     * @param out
     * @param f RDFFormat of the output, eg. RDFFormat.JSONLD_COMPACT_PRETTY
     * @param g the data that we want to output as JSON-LD
     * @param ctx the object that allows to control the writing process (a set of parameters)
     */
void write(OutputStream out, DatasetGraph g, RDFFormat f, Context ctx) {
    // create a WriterDatasetRIOT with the RDFFormat
    WriterDatasetRIOT w = RDFDataMgr.createDatasetWriter(f);
    PrefixMap pm = RiotLib.prefixMap(g);
    String base = null;
    w.write(out, g, pm, base, ctx);
}
Also used : WriterDatasetRIOT(org.apache.jena.riot.WriterDatasetRIOT) PrefixMap(org.apache.jena.riot.system.PrefixMap)

Example 2 with WriterDatasetRIOT

use of org.apache.jena.riot.WriterDatasetRIOT in project jena by apache.

the class TestJsonLDWriter method toString.

//
// some utilities
//
private String toString(Model m, RDFFormat f, Context jenaContext) {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        WriterDatasetRIOT w = RDFDataMgr.createDatasetWriter(f);
        DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
        PrefixMap pm = RiotLib.prefixMap(g);
        String base = null;
        w.write(out, g, pm, base, jenaContext);
        out.flush();
        return out.toString("UTF-8");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : WriterDatasetRIOT(org.apache.jena.riot.WriterDatasetRIOT) PrefixMap(org.apache.jena.riot.system.PrefixMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonString(org.apache.jena.atlas.json.JsonString) IOException(java.io.IOException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Aggregations

WriterDatasetRIOT (org.apache.jena.riot.WriterDatasetRIOT)2 PrefixMap (org.apache.jena.riot.system.PrefixMap)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 JsonString (org.apache.jena.atlas.json.JsonString)1 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)1