oxigraph::sparql::results

Struct QueryResultsSerializer

Source
pub struct QueryResultsSerializer { /* private fields */ }
Expand description

A serializer for SPARQL query results serialization formats.

It currently supports the following formats:

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

Source

pub fn from_format(format: QueryResultsFormat) -> QueryResultsSerializer

Builds a serializer for the given format.

Source

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>"#);
Source

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
Source

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>"#);
Source

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§

Source§

impl Clone for QueryResultsSerializer

Source§

fn clone(&self) -> QueryResultsSerializer

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl From<QueryResultsFormat> for QueryResultsSerializer

Source§

fn from(format: QueryResultsFormat) -> QueryResultsSerializer

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V