Search in sources :

Example 1 with JSONLDWriter

use of org.eclipse.rdf4j.rio.jsonld.JSONLDWriter in project rdf4j by eclipse.

the class JSONLDParserHandlerTest method writeJSONLD.

/**
 * Helper method to write the given model to JSON-LD and return an InputStream containing the results.
 *
 * @param statements
 * @return An {@link InputStream} containing the results.
 * @throws RDFHandlerException
 */
private InputStream writeJSONLD(Model statements) throws RDFHandlerException {
    final StringWriter writer = new StringWriter();
    final RDFWriter jsonldWriter = new JSONLDWriter(writer);
    jsonldWriter.startRDF();
    for (final Namespace prefix : statements.getNamespaces()) {
        jsonldWriter.handleNamespace(prefix.getPrefix(), prefix.getName());
    }
    for (final Statement nextStatement : statements) {
        jsonldWriter.handleStatement(nextStatement);
    }
    jsonldWriter.endRDF();
    return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
Also used : JSONLDWriter(org.eclipse.rdf4j.rio.jsonld.JSONLDWriter) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Statement(org.eclipse.rdf4j.model.Statement) RDFWriter(org.eclipse.rdf4j.rio.RDFWriter) Namespace(org.eclipse.rdf4j.model.Namespace)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringWriter (java.io.StringWriter)1 Namespace (org.eclipse.rdf4j.model.Namespace)1 Statement (org.eclipse.rdf4j.model.Statement)1 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)1 JSONLDWriter (org.eclipse.rdf4j.rio.jsonld.JSONLDWriter)1