Struct QueryResultsSerializer
pub struct QueryResultsSerializer {
format: QueryResultsFormat,
}
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"}}]}}"#);
Fields§
§format: QueryResultsFormat
Implementations§
§impl QueryResultsSerializer
impl QueryResultsSerializer
pub fn from_format(format: QueryResultsFormat) -> QueryResultsSerializer
pub fn from_format(format: QueryResultsFormat) -> QueryResultsSerializer
Builds a serializer for the given format.
pub 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>"#);
pub fn write_boolean_result<W>(
&self,
writer: W,
value: bool,
) -> Result<W, Error>where
W: Write,
👎Deprecated since 0.4.0: use serialize_boolean_to_writer
pub 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>"#);
pub fn solutions_writer<W>(
&self,
writer: W,
variables: Vec<Variable>,
) -> Result<WriterSolutionsSerializer<W>, Error>where
W: Write,
👎Deprecated since 0.4.0: use serialize_solutions_to_writer
Trait Implementations§
§impl Clone for QueryResultsSerializer
impl Clone for QueryResultsSerializer
§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 more§impl From<QueryResultsFormat> for QueryResultsSerializer
impl From<QueryResultsFormat> for QueryResultsSerializer
§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§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
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,
§impl<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
§fn deserialize(
&self,
deserializer: &mut D,
) -> Result<With<T, W>, <D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut D, ) -> Result<With<T, W>, <D as Fallible>::Error>
Deserializes using the given deserializer
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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