pub struct WriterSolutionsSerializer<W: Write> { /* private fields */ }
Expand description
Allows writing query results into a Write
implementation.
Could be built using a QueryResultsSerializer
.
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 TSV (the API is the same for JSON, XML and CSV):
use oxrdf::{LiteralRef, Variable, VariableRef};
use sparesults::{QueryResultsFormat, QueryResultsSerializer};
use std::iter::once;
let tsv_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Tsv);
let mut buffer = Vec::new();
let mut serializer = tsv_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, b"?foo\t?bar\n\"test\"\t\n");
Implementations§
Source§impl<W: Write> WriterSolutionsSerializer<W>
impl<W: Write> WriterSolutionsSerializer<W>
Sourcepub fn serialize<'a>(
&mut self,
solution: impl IntoIterator<Item = (impl Into<VariableRef<'a>>, impl Into<TermRef<'a>>)>,
) -> Result<()>
pub fn serialize<'a>( &mut self, solution: impl IntoIterator<Item = (impl Into<VariableRef<'a>>, impl Into<TermRef<'a>>)>, ) -> Result<()>
Writes a solution.
Example in JSON (the API is the same for XML, CSV and TSV):
use sparesults::{QueryResultsFormat, QueryResultsSerializer, QuerySolution};
use oxrdf::{Literal, LiteralRef, Variable, VariableRef};
use std::iter::once;
let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Json);
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.serialize(&QuerySolution::from((vec![Variable::new("bar")?], vec![Some(Literal::from("test").into())])))?;
serializer.finish()?;
assert_eq!(buffer, br#"{"head":{"vars":["foo","bar"]},"results":{"bindings":[{"foo":{"type":"literal","value":"test"}},{"bar":{"type":"literal","value":"test"}}]}}"#);
Auto Trait Implementations§
impl<W> Freeze for WriterSolutionsSerializer<W>where
W: Freeze,
impl<W> RefUnwindSafe for WriterSolutionsSerializer<W>where
W: RefUnwindSafe,
impl<W> Send for WriterSolutionsSerializer<W>where
W: Send,
impl<W> Sync for WriterSolutionsSerializer<W>where
W: Sync,
impl<W> Unpin for WriterSolutionsSerializer<W>where
W: Unpin,
impl<W> UnwindSafe for WriterSolutionsSerializer<W>where
W: UnwindSafe,
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