Module results

Source
Expand description

Utilities to read and write RDF results formats using sparesults.

It supports SPARQL Query Results XML Format (Second Edition), SPARQL 1.1 Query Results JSON Format and SPARQL 1.1 Query Results CSV and TSV Formats.

Usage example converting a JSON result file into a TSV result file:

use oxigraph::sparql::results::{QueryResultsFormat, QueryResultsParser, ReaderQueryResultsParserOutput, QueryResultsSerializer};
use std::io::Result;

fn convert_json_to_tsv(json_file: &[u8]) -> Result<Vec<u8>> {
    let json_parser = QueryResultsParser::from_format(QueryResultsFormat::Json);
    let tsv_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Tsv);
    // We start to read the JSON file and see which kind of results it is
    match json_parser.for_reader(json_file)? {
        ReaderQueryResultsParserOutput::Boolean(value) => {
            // it's a boolean result, we copy it in TSV to the output buffer
            tsv_serializer.serialize_boolean_to_writer(Vec::new(), value)
        }
        ReaderQueryResultsParserOutput::Solutions(solutions_reader) => {
            // it's a set of solutions, we create a writer and we write to it while reading in streaming from the JSON file
            let mut tsv_solutions_serializer = tsv_serializer.serialize_solutions_to_writer(Vec::new(), solutions_reader.variables().to_vec())?;
            for solution in solutions_reader {
                tsv_solutions_serializer.serialize(&solution?)?;
            }
            tsv_solutions_serializer.finish()
        }
    }
}

// Let's test with a boolean
assert_eq!(
    convert_json_to_tsv(br#"{"boolean":true}"#.as_slice()).unwrap(),
    b"true"
);

// And with a set of solutions
assert_eq!(
    convert_json_to_tsv(br#"{"head":{"vars":["foo","bar"]},"results":{"bindings":[{"foo":{"type":"literal","value":"test"}}]}}"#.as_slice()).unwrap(),
    b"?foo\t?bar\n\"test\"\t\n"
);

Modules§

solution
Definition of QuerySolution structure and associated utility constructions.

Structs§

QueryResultsParser
Parsers for SPARQL query results serialization formats.
QueryResultsSerializer
A serializer for SPARQL query results serialization formats.
QueryResultsSyntaxError
An error in the syntax of the parsed file.
QuerySolution
Tuple associating variables and terms that are the result of a SPARQL query.
ReaderSolutionsParser
A streaming parser of a set of QuerySolution solutions.
SliceSolutionsParser
A streaming parser of a set of QuerySolution solutions.
TextPosition
A position in a text i.e. a line number starting from 0, a column number starting from 0 (in number of code points) and a global file offset starting from 0 (in number of bytes).
WriterSolutionsSerializer
Allows writing query results into a Write implementation.

Enums§

QueryResultsFormat
SPARQL query results serialization formats.
QueryResultsParseError
Error returned during SPARQL result formats format parsing.
ReaderQueryResultsParserOutput
The reader for a given read of a results file.
SliceQueryResultsParserOutput
The reader for a given read of a results file.