Snarl connection: how to change query result format

Hi,
This pseudo-code shows how I usually use snarl to execute my queries.

snarlTemplate.execute(
        connection -> { SelectQuery query =
                connection.select(sparql).dataset(dataset);
//rest of My things here}
)

During the runtime, I can see that under the object query here:

((QueryResultFormatImpl) ((HttpClientImpl) ((HttpConnection) ((SelectQueryImpl) query).mConn).mClient).mDefaultQueryResultFormat).mName

There is a DefaultQueryResultFormat that is set to BINARY. I'm interested to get the result in another formats. I have a question: how can we change it to JSON?

I saw something like this, but I do not know how it applies.

Thank you.

I'm interested to change the query result to something like this:

public static final QueryResultFormat JSON = new QueryResultFormatImpl("SPARQL/JSON", Values.iri("http://www.w3.org/ns/formats/SPARQL_Results_JSON"), ImmutableList.of("application/sparql-results+json", "application/json"), ImmutableList.of("srj", "json"));

I've found an example here: stardog-examples/ExampleService.java at 6c3afd3d33b4c267349898c549d1fdc1e5bf2884 · stardog-union/stardog-examples · GitHub

I am not sure if I can change the QueryResultFormat without using queryResultParser.
Thank you.

Hi Cesar,

We offer a number of utility/static classes/methods for query and result i/o. In this case you can use QueryResultWriters to get what you want:

snarlTemplate.<Void>execute(connection -> {
			try (SelectQueryResult result = connection.select(sparql).dataset(dataset).execute()) {
				QueryResultWriters.write(result, Paths.get("/var/opt/stardog/out.json"), QueryResultFormats.JSON);
			}
			catch (IOException theE) {
				theE.printStackTrace();
			}
			return null;
		});