oxigraph/sparql/
results.rs

1//! Utilities to read and write RDF results formats  using [sparesults](https://crates.io/crates/sparesults).
2//!
3//! It supports [SPARQL Query Results XML Format (Second Edition)](https://www.w3.org/TR/rdf-sparql-XMLres/), [SPARQL 1.1 Query Results JSON Format](https://www.w3.org/TR/sparql11-results-json/) and [SPARQL 1.1 Query Results CSV and TSV Formats](https://www.w3.org/TR/sparql11-results-csv-tsv/).
4//!
5//! Usage example converting a JSON result file into a TSV result file:
6//!
7//! ```
8//! use oxigraph::sparql::results::{QueryResultsFormat, QueryResultsParser, ReaderQueryResultsParserOutput, QueryResultsSerializer};
9//! use std::io::Result;
10//!
11//! fn convert_json_to_tsv(json_file: &[u8]) -> Result<Vec<u8>> {
12//!     let json_parser = QueryResultsParser::from_format(QueryResultsFormat::Json);
13//!     let tsv_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Tsv);
14//!     // We start to read the JSON file and see which kind of results it is
15//!     match json_parser.for_reader(json_file)? {
16//!         ReaderQueryResultsParserOutput::Boolean(value) => {
17//!             // it's a boolean result, we copy it in TSV to the output buffer
18//!             tsv_serializer.serialize_boolean_to_writer(Vec::new(), value)
19//!         }
20//!         ReaderQueryResultsParserOutput::Solutions(solutions_reader) => {
21//!             // it's a set of solutions, we create a writer and we write to it while reading in streaming from the JSON file
22//!             let mut tsv_solutions_serializer = tsv_serializer.serialize_solutions_to_writer(Vec::new(), solutions_reader.variables().to_vec())?;
23//!             for solution in solutions_reader {
24//!                 tsv_solutions_serializer.serialize(&solution?)?;
25//!             }
26//!             tsv_solutions_serializer.finish()
27//!         }
28//!     }
29//! }
30//!
31//! // Let's test with a boolean
32//! assert_eq!(
33//!     convert_json_to_tsv(br#"{"boolean":true}"#.as_slice()).unwrap(),
34//!     b"true"
35//! );
36//!
37//! // And with a set of solutions
38//! assert_eq!(
39//!     convert_json_to_tsv(br#"{"head":{"vars":["foo","bar"]},"results":{"bindings":[{"foo":{"type":"literal","value":"test"}}]}}"#.as_slice()).unwrap(),
40//!     b"?foo\t?bar\n\"test\"\t\n"
41//! );
42//! ```
43
44pub use sparesults::*;