Search code examples
javaturtle-rdfrdf4j

How to configure RDFWriter to explicit write data type in literals?


I'm saving a TTL file using RDFWriter. How can I explicitly save literals with their data type?

For example, I want "5.36289"^^xsd:float but I get 5.36289E0 instead.

I had the same problem with strings, but I found the BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL property that solved. I cannot find any similar configuration for other data types.

I am creating the literals using the method Values.literal.

This is the source code:

FileOutputStream out = new FileOutputStream(ttlOutputFile);
RDFWriter writer = Rio.createWriter(RDFFormat.TURTLE, out);

final WriterConfig config = writer.getWriterConfig();
config.set(BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL, false);

writer.startRDF();

for (Statement st : model) {
    writer.handleStatement(st);
}
writer.endRDF();

Solution

  • There is a configuration setting called ABBREVIATE_NUMBERS that works like a charm (and it needs to be used only when PRETTY_PRINT is true, which is the default value).

    final WriterConfig config = writer.getWriterConfig();
    config.set(BasicWriterSettings.PRETTY_PRINT, true);
    config.set(BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL, false);
    config.set(TurtleWriterSettings.ABBREVIATE_NUMBERS, false);