pub struct RdfSerializer { /* private fields */ }
Expand description
A serializer for RDF serialization formats.
It currently supports the following formats:
- N3 (
RdfFormat::N3
) - N-Quads (
RdfFormat::NQuads
) - canonical N-Triples (
RdfFormat::NTriples
) - RDF/XML (
RdfFormat::RdfXml
) - TriG (
RdfFormat::TriG
) - Turtle (
RdfFormat::Turtle
)
use oxrdfio::{RdfFormat, RdfSerializer};
use oxrdf::{Quad, NamedNode};
let mut serializer = RdfSerializer::from_format(RdfFormat::NQuads).for_writer(Vec::new());
serializer.serialize_quad(&Quad {
subject: NamedNode::new("http://example.com/s")?.into(),
predicate: NamedNode::new("http://example.com/p")?,
object: NamedNode::new("http://example.com/o")?.into(),
graph_name: NamedNode::new("http://example.com/g")?.into()
})?;
assert_eq!(serializer.finish()?, b"<http://example.com/s> <http://example.com/p> <http://example.com/o> <http://example.com/g> .\n");
Implementations§
Source§impl RdfSerializer
impl RdfSerializer
Sourcepub fn from_format(format: RdfFormat) -> Self
pub fn from_format(format: RdfFormat) -> Self
Builds a serializer for the given format
Sourcepub fn format(&self) -> RdfFormat
pub fn format(&self) -> RdfFormat
The format the serializer serializes to.
use oxrdfio::{RdfFormat, RdfSerializer};
assert_eq!(
RdfSerializer::from_format(RdfFormat::Turtle).format(),
RdfFormat::Turtle
);
Sourcepub fn with_prefix(
self,
prefix_name: impl Into<String>,
prefix_iri: impl Into<String>,
) -> Result<Self, IriParseError>
pub fn with_prefix( self, prefix_name: impl Into<String>, prefix_iri: impl Into<String>, ) -> Result<Self, IriParseError>
If the format supports it, sets a prefix.
use oxrdf::vocab::rdf;
use oxrdf::{NamedNodeRef, TripleRef};
use oxrdfio::{RdfFormat, RdfSerializer};
let mut serializer = RdfSerializer::from_format(RdfFormat::Turtle)
.with_prefix("schema", "http://schema.org/")?
.for_writer(Vec::new());
serializer.serialize_triple(TripleRef {
subject: NamedNodeRef::new("http://example.com/s")?.into(),
predicate: rdf::TYPE.into(),
object: NamedNodeRef::new("http://schema.org/Person")?.into(),
})?;
assert_eq!(
serializer.finish()?,
b"@prefix schema: <http://schema.org/> .\n<http://example.com/s> a schema:Person .\n"
);
Sourcepub fn with_base_iri(
self,
base_iri: impl Into<String>,
) -> Result<Self, IriParseError>
pub fn with_base_iri( self, base_iri: impl Into<String>, ) -> Result<Self, IriParseError>
If the format supports it, sets a base IRI.
use oxrdf::vocab::rdf;
use oxrdf::{NamedNodeRef, TripleRef};
use oxrdfio::{RdfFormat, RdfSerializer};
let mut serializer = RdfSerializer::from_format(RdfFormat::Turtle)
.with_base_iri("http://example.com")?
.with_prefix("ex", "http://example.com/ns#")?
.for_writer(Vec::new());
serializer.serialize_triple(TripleRef::new(
NamedNodeRef::new("http://example.com/me")?,
rdf::TYPE,
NamedNodeRef::new("http://example.com/ns#Person")?,
))?;
assert_eq!(
serializer.finish()?,
b"@base <http://example.com> .\n@prefix ex: </ns#> .\n</me> a ex:Person .\n",
);
Sourcepub fn for_writer<W: Write>(self, writer: W) -> WriterQuadSerializer<W>
pub fn for_writer<W: Write>(self, writer: W) -> WriterQuadSerializer<W>
Serializes to a Write
implementation.
Do not forget to run the finish
method to properly write the last bytes of the file.
This writer does unbuffered writes. You might want to use BufWriter
to avoid that.
use oxrdfio::{RdfFormat, RdfSerializer};
use oxrdf::{Quad, NamedNode};
let mut serializer = RdfSerializer::from_format(RdfFormat::NQuads).for_writer(Vec::new());
serializer.serialize_quad(&Quad {
subject: NamedNode::new("http://example.com/s")?.into(),
predicate: NamedNode::new("http://example.com/p")?,
object: NamedNode::new("http://example.com/o")?.into(),
graph_name: NamedNode::new("http://example.com/g")?.into()
})?;
assert_eq!(serializer.finish()?, b"<http://example.com/s> <http://example.com/p> <http://example.com/o> <http://example.com/g> .\n");
Trait Implementations§
Source§impl Clone for RdfSerializer
impl Clone for RdfSerializer
Source§fn clone(&self) -> RdfSerializer
fn clone(&self) -> RdfSerializer
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for RdfSerializer
impl RefUnwindSafe for RdfSerializer
impl Send for RdfSerializer
impl Sync for RdfSerializer
impl Unpin for RdfSerializer
impl UnwindSafe for RdfSerializer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more