pub struct QueryResultsSerializer { /* private fields */ }
Expand description
A serializer for SPARQL query results serialization formats.
It currently supports the following formats:
- SPARQL Query Results XML Format (
QueryResultsFormat::Xml
) - SPARQL Query Results JSON Format (
QueryResultsFormat::Json
) - SPARQL Query Results CSV Format (
QueryResultsFormat::Csv
) - SPARQL Query Results TSV Format (
QueryResultsFormat::Tsv
)
Example in JSON (the API is the same for XML, CSV and TSV):
use sparesults::{QueryResultsFormat, QueryResultsSerializer};
use oxrdf::{LiteralRef, Variable, VariableRef};
use std::iter::once;
let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Json);
// boolean
let mut buffer = Vec::new();
json_serializer.clone().serialize_boolean_to_writer(&mut buffer, true)?;
assert_eq!(buffer, br#"{"head":{},"boolean":true}"#);
// solutions
let mut buffer = Vec::new();
let mut serializer = json_serializer.serialize_solutions_to_writer(&mut buffer, vec![Variable::new("foo")?, Variable::new("bar")?])?;
serializer.serialize(once((VariableRef::new("foo")?, LiteralRef::from("test"))))?;
serializer.finish()?;
assert_eq!(buffer, br#"{"head":{"vars":["foo","bar"]},"results":{"bindings":[{"foo":{"type":"literal","value":"test"}}]}}"#);
Implementations§
Source§impl QueryResultsSerializer
impl QueryResultsSerializer
Sourcepub fn from_format(format: QueryResultsFormat) -> QueryResultsSerializer
pub fn from_format(format: QueryResultsFormat) -> QueryResultsSerializer
Builds a serializer for the given format.
Sourcepub fn serialize_boolean_to_writer<W>(
self,
writer: W,
value: bool,
) -> Result<W, Error>where
W: Write,
pub fn serialize_boolean_to_writer<W>(
self,
writer: W,
value: bool,
) -> Result<W, Error>where
W: Write,
Write a boolean query result (from an ASK
query) into the given Write
implementation.
Example in XML (the API is the same for JSON, CSV and TSV):
use sparesults::{QueryResultsFormat, QueryResultsSerializer};
let xml_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Xml);
let mut buffer = Vec::new();
xml_serializer.serialize_boolean_to_writer(&mut buffer, true)?;
assert_eq!(buffer, br#"<?xml version="1.0"?><sparql xmlns="http://www.w3.org/2005/sparql-results#"><head></head><boolean>true</boolean></sparql>"#);
Sourcepub fn serialize_solutions_to_writer<W>(
self,
writer: W,
variables: Vec<Variable>,
) -> Result<WriterSolutionsSerializer<W>, Error>where
W: Write,
pub fn serialize_solutions_to_writer<W>(
self,
writer: W,
variables: Vec<Variable>,
) -> Result<WriterSolutionsSerializer<W>, Error>where
W: Write,
Returns a SolutionsSerializer
allowing writing query solutions into the given 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.
Example in XML (the API is the same for JSON, CSV and TSV):
use sparesults::{QueryResultsFormat, QueryResultsSerializer};
use oxrdf::{LiteralRef, Variable, VariableRef};
use std::iter::once;
let xml_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Xml);
let mut buffer = Vec::new();
let mut serializer = xml_serializer.serialize_solutions_to_writer(&mut buffer, vec![Variable::new("foo")?, Variable::new("bar")?])?;
serializer.serialize(once((VariableRef::new("foo")?, LiteralRef::from("test"))))?;
serializer.finish()?;
assert_eq!(buffer, br#"<?xml version="1.0"?><sparql xmlns="http://www.w3.org/2005/sparql-results#"><head><variable name="foo"/><variable name="bar"/></head><results><result><binding name="foo"><literal>test</literal></binding></result></results></sparql>"#);
Trait Implementations§
Source§impl Clone for QueryResultsSerializer
impl Clone for QueryResultsSerializer
Source§fn clone(&self) -> QueryResultsSerializer
fn clone(&self) -> QueryResultsSerializer
Returns a duplicate 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 moreSource§impl From<QueryResultsFormat> for QueryResultsSerializer
impl From<QueryResultsFormat> for QueryResultsSerializer
Source§fn from(format: QueryResultsFormat) -> QueryResultsSerializer
fn from(format: QueryResultsFormat) -> QueryResultsSerializer
Converts to this type from the input type.
Auto Trait Implementations§
impl Freeze for QueryResultsSerializer
impl RefUnwindSafe for QueryResultsSerializer
impl Send for QueryResultsSerializer
impl Sync for QueryResultsSerializer
impl Unpin for QueryResultsSerializer
impl UnwindSafe for QueryResultsSerializer
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more