Skip to content

Commit 3e810e1

Browse files
authored
Fix: serializing blank node graphs in TriG and N-Quads (#414)
1 parent ff1ccee commit 3e810e1

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

private/rdf4cpp/writer/WriteQuad.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ bool write_pred(Node const pred, writer::BufWriterParts const writer) {
3838
}
3939

4040
[[nodiscard]] inline bool print_graph(Node const &graph) noexcept {
41-
auto const g = graph.as_iri();
42-
return !g.is_default_graph();
41+
return !graph.null() && graph.as_iri().is_default_graph() != TriBool::True;
4342
}
4443

4544
template<writer::OutputFormat F, typename Q>

tests/serializer/tests_Serialization.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,46 @@ TEST_CASE("basic trig") {
273273
"<http://ex/sub> a \"7\"^^xsd:int .\n");
274274
}
275275

276+
TEST_CASE("nquads bnode graph") {
277+
Quad q{
278+
BlankNode::make("G"),
279+
IRI::make("http://example.com#s"),
280+
IRI::make("http://example.com#p"),
281+
IRI::make("http://example.com#o"),
282+
};
283+
284+
auto s = writer::StringWriter::oneshot([&q](auto &w) {
285+
q.serialize_nquads(w);
286+
return true;
287+
});
288+
289+
CHECK_EQ(s, "<http://example.com#s> <http://example.com#p> <http://example.com#o> _:G .\n");
290+
}
291+
292+
TEST_CASE("trig bnode graph") {
293+
Quad q{
294+
BlankNode::make("G"),
295+
IRI::make("http://example.com#s"),
296+
IRI::make("http://example.com#p"),
297+
IRI::make("http://example.com#o"),
298+
};
299+
300+
auto s = writer::StringWriter::oneshot([&q](auto &w) {
301+
writer::SerializationState st{};
302+
st.begin(w);
303+
q.serialize_trig(st, w);
304+
st.flush(w);
305+
return true;
306+
});
307+
308+
CHECK_EQ(s, R"(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
309+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
310+
_:G {
311+
<http://example.com#s> <http://example.com#p> <http://example.com#o> .
312+
}
313+
)");
314+
}
315+
276316
template<OutputFormat F>
277317
std::string write_ext_data() {
278318
std::string buf;

0 commit comments

Comments
 (0)