Package org.eclipse.rdf4j.rio.rdfjson
Class RDFJSONWriter
- java.lang.Object
-
- org.eclipse.rdf4j.rio.helpers.AbstractRDFWriter
-
- org.eclipse.rdf4j.rio.rdfjson.RDFJSONWriter
-
- All Implemented Interfaces:
CharSink
,Sink
,RDFHandler
,RDFWriter
public class RDFJSONWriter extends AbstractRDFWriter implements CharSink
RDFWriter
implementation for the RDF/JSON format
-
-
Field Summary
Fields Modifier and Type Field Description private RDFFormat
actualFormat
private Model
graph
private boolean
isEmptyStream
private boolean
isStreaming
private com.fasterxml.jackson.core.JsonGenerator
jg
private IRI
lastWrittenPredicate
private Resource
lastWrittenSubject
private java.io.Writer
writer
-
Fields inherited from class org.eclipse.rdf4j.rio.helpers.AbstractRDFWriter
namespaceTable, statementConsumer
-
-
Constructor Summary
Constructors Constructor Description RDFJSONWriter(java.io.OutputStream out, RDFFormat actualFormat)
RDFJSONWriter(java.io.Writer writer, RDFFormat actualFormat)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private com.fasterxml.jackson.core.JsonFactory
configureNewJsonFactory()
Get an instance of JsonFactory.void
consumeStatement(Statement statement)
Consume a statement.private void
consumeStreamingStatement(Statement statement)
Consumes statement when RDF/JSON writer is streaming output.void
endRDF()
Signals the end of the RDF data.RDFFormat
getRDFFormat()
Gets the RDF format that this RDFWriter uses.java.util.Collection<RioSetting<?>>
getSupportedSettings()
java.io.Writer
getWriter()
get theWriter
used by thisCharSink
.void
handleComment(java.lang.String comment)
Handles a comment.void
handleNamespace(java.lang.String prefix, java.lang.String uri)
Handles a namespace declaration/definition.static void
modelToRdfJsonInternal(Model graph, WriterConfig writerConfig, com.fasterxml.jackson.core.JsonGenerator jg)
static java.lang.String
resourceToString(Resource uriOrBnode)
Returns the correct syntax for a Resource, depending on whether it is a URI or a Blank Node (ie, BNode)void
startRDF()
Signals the start of the RDF data.static void
writeObject(Value object, java.util.Set<Resource> contexts, com.fasterxml.jackson.core.JsonGenerator jg)
Helper method to reduce complexity of the JSON serialisation algorithm Any null contexts will only be serialised to JSON if there are also non-null contexts in the contexts array-
Methods inherited from class org.eclipse.rdf4j.rio.helpers.AbstractRDFWriter
checkWritingStarted, getFileFormat, getWriterConfig, handleStatement, isWritingStarted, set, setWriterConfig
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.eclipse.rdf4j.common.io.Sink
acceptsFileFormat, getFileFormat
-
-
-
-
Field Detail
-
writer
private final java.io.Writer writer
-
actualFormat
private final RDFFormat actualFormat
-
graph
private Model graph
-
lastWrittenSubject
private Resource lastWrittenSubject
-
lastWrittenPredicate
private IRI lastWrittenPredicate
-
jg
private com.fasterxml.jackson.core.JsonGenerator jg
-
isEmptyStream
private boolean isEmptyStream
-
isStreaming
private boolean isStreaming
-
-
Method Detail
-
getWriter
public java.io.Writer getWriter()
Description copied from interface:CharSink
get theWriter
used by thisCharSink
.
-
startRDF
public void startRDF() throws RDFHandlerException
Description copied from interface:RDFHandler
Signals the start of the RDF data. This method is called before any data is reported.- Specified by:
startRDF
in interfaceRDFHandler
- Overrides:
startRDF
in classAbstractRDFWriter
- Throws:
RDFHandlerException
- If the RDF handler has encountered an unrecoverable error.
-
endRDF
public void endRDF() throws RDFHandlerException
Description copied from interface:RDFHandler
Signals the end of the RDF data. This method is called when all data has been reported.- Specified by:
endRDF
in interfaceRDFHandler
- Throws:
RDFHandlerException
- If the RDF handler has encountered an unrecoverable error.
-
getRDFFormat
public RDFFormat getRDFFormat()
Description copied from interface:RDFWriter
Gets the RDF format that this RDFWriter uses.- Specified by:
getRDFFormat
in interfaceRDFWriter
-
getSupportedSettings
public java.util.Collection<RioSetting<?>> getSupportedSettings()
- Specified by:
getSupportedSettings
in interfaceRDFWriter
- Overrides:
getSupportedSettings
in classAbstractRDFWriter
- Returns:
- A collection of
RioSetting
s that are supported by this RDFWriter.
-
handleComment
public void handleComment(java.lang.String comment) throws RDFHandlerException
Description copied from interface:RDFHandler
Handles a comment.- Specified by:
handleComment
in interfaceRDFHandler
- Parameters:
comment
- The comment.- Throws:
RDFHandlerException
- If the RDF handler has encountered an unrecoverable error.
-
handleNamespace
public void handleNamespace(java.lang.String prefix, java.lang.String uri) throws RDFHandlerException
Description copied from interface:RDFHandler
Handles a namespace declaration/definition. A namespace declaration associates a (short) prefix string with the namespace's URI. The prefix for default namespaces, which do not have an associated prefix, are represented as empty strings.- Specified by:
handleNamespace
in interfaceRDFHandler
- Overrides:
handleNamespace
in classAbstractRDFWriter
- Parameters:
prefix
- The prefix for the namespace, or an empty string in case of a default namespace.uri
- The URI that the prefix maps to.- Throws:
RDFHandlerException
- If the RDF handler has encountered an unrecoverable error.
-
consumeStatement
public void consumeStatement(Statement statement) throws RDFHandlerException
Description copied from class:AbstractRDFWriter
Consume a statement.Extending classes must override this method instead of overriding
AbstractRDFWriter.handleStatement(Statement)
in order to benefit from automatic handling of RDF-star conversion or encoding.- Overrides:
consumeStatement
in classAbstractRDFWriter
- Parameters:
statement
- the statement to consume.- Throws:
RDFHandlerException
-
writeObject
public static void writeObject(Value object, java.util.Set<Resource> contexts, com.fasterxml.jackson.core.JsonGenerator jg) throws com.fasterxml.jackson.core.JsonGenerationException, java.io.IOException
Helper method to reduce complexity of the JSON serialisation algorithm Any null contexts will only be serialised to JSON if there are also non-null contexts in the contexts array- Parameters:
object
- The RDF value to serialisecontexts
- The set of contexts that are relevant to this object, including null contexts as they are found.jg
- theJsonGenerator
to write to.- Throws:
java.io.IOException
com.fasterxml.jackson.core.JsonGenerationException
-
resourceToString
public static java.lang.String resourceToString(Resource uriOrBnode)
Returns the correct syntax for a Resource, depending on whether it is a URI or a Blank Node (ie, BNode)- Parameters:
uriOrBnode
- The resource to serialise to a string- Returns:
- The string value of the RDF4J resource
-
modelToRdfJsonInternal
public static void modelToRdfJsonInternal(Model graph, WriterConfig writerConfig, com.fasterxml.jackson.core.JsonGenerator jg) throws java.io.IOException
- Throws:
java.io.IOException
-
configureNewJsonFactory
private com.fasterxml.jackson.core.JsonFactory configureNewJsonFactory()
Get an instance of JsonFactory.- Returns:
- A newly configured JsonFactory based on the currently enabled settings
-
consumeStreamingStatement
private void consumeStreamingStatement(Statement statement) throws RDFHandlerException
Consumes statement when RDF/JSON writer is streaming output.- Throws:
RDFHandlerException
-
-