Repository navigation
Configuration Options
TOMLy provides two sets of options: PreserveOptions for parsing and WriterOptions for serialization.
Control what elements are preserved during parsing.
By default, nothing is preserved for maximum performance:
PreserveOptions options = PreserveOptions.defaultOptions();
// Equivalent to:
// preserveBlankLines = false
// preserveHeaderComments = false
// preserveInlineComments = falseUse the builder for custom configuration:
PreserveOptions options = PreserveOptions.builder()
.preserveBlankLines(true)
.preserveHeaderComments(true)
.preserveInlineComments(true)
.build();Empty lines between sections:
name = "MyApp"
[server]
host = "localhost"PreserveOptions options = PreserveOptions.builder()
.preserveBlankLines(true)
.build();
TomlDocument doc = Tomly.parse(toml, false, options);
Node nameNode = doc.get("name");
List<String> comments = nameNode.headerComments(); // May contain empty strings for blank lines- Maintaining visual structure in round-trip serialization
- Preserving document formatting conventions
Performance impact: Minimal
Comments appearing before key-value pairs:
# This is a header comment
# It can span multiple lines
name = "MyApp"PreserveOptions options = PreserveOptions.builder()
.preserveHeaderComments(true)
.build();
TomlDocument doc = Tomly.parse(toml, false, options);
Node nameNode = doc.get("name");
List<String> headers = nameNode.headerComments();
// ["# This is a header comment", "# It can span multiple lines"]- Generating documented configuration files
- Preserving documentation during config updates
- Configuration management tools
Performance impact: Small (requires comment tracking)
Comments at the end of value lines:
name = "MyApp" # The application name
port = 8080 # Default portPreserveOptions options = PreserveOptions.builder()
.preserveInlineComments(true)
.build();
TomlDocument doc = Tomly.parse(toml, false, options);
Node nameNode = doc.get("name");
String inline = nameNode.inlineComment(); // "The application name"- Preserving field-level documentation
- Maintaining hints for configuration values
Performance impact: Small (requires comment parsing)
Enable all preservation features:
PreserveOptions fullPreservation = PreserveOptions.builder()
.preserveBlankLines(true)
.preserveHeaderComments(true)
.preserveInlineComments(true)
.build();Preserve only comments:
PreserveOptions commentsOnly = PreserveOptions.builder()
.preserveHeaderComments(true)
.preserveInlineComments(true)
.build();Control formatting when writing TOML.
WriterOptions options = WriterOptions.defaultOptions();
// writeBlankLines = false
// writeHeaderComments = false
// writeInlineComments = false
// maxLineLength = 100WriterOptions options = WriterOptions.builder()
.writeBlankLines(true)
.writeHeaderComments(true)
.writeInlineComments(true)
.maxLineLength(80)
.build();Preserve blank lines in output:
WriterOptions options = WriterOptions.builder()
.writeBlankLines(true)
.build();
doc.save(Path.of("output.toml"), options);Output:
name = "MyApp"
[server]
host = "localhost"Include header comments in output:
WriterOptions options = WriterOptions.builder()
.writeHeaderComments(true)
.build();
doc.save(Path.of("output.toml"), options);Output:
# Application name
name = "MyApp"Include inline comments:
WriterOptions options = WriterOptions.builder()
.writeInlineComments(true)
.build();
doc.save(Path.of("output.toml"), options);Output:
name = "MyApp" # The application name
port = 8080 # Default portControl when to break long values:
// Default: 100 characters
WriterOptions standard = WriterOptions.builder()
.maxLineLength(100)
.build();
// Compact: no line breaks
WriterOptions compact = WriterOptions.builder()
.maxLineLength(Integer.MAX_VALUE)
.build();
// Narrow: 80 characters
WriterOptions narrow = WriterOptions.builder()
.maxLineLength(80)
.build();Effect on arrays:
// With maxLineLength = 10
[1, 2, 3, 4, 5]
// Becomes:
[
1,
2,
3,
4,
5
]For serializing individual values without line breaks:
WriterOptions options = WriterOptions.elementOptions();
// Equivalent to maxLineLength = Integer.MAX_VALUENode arrayNode = doc.get("servers");
String oneLine = arrayNode.saveToString(WriterOptions.elementOptions());
// Always single line regardless of contentProduction Configuration
PreserveOptions parseOpts = PreserveOptions.defaultOptions();
WriterOptions writeOpts = WriterOptions.defaultOptions();
TomlDocument doc = Tomly.parse(configPath, false, parseOpts);
doc.save(outputPath, writeOpts);Preserve and write all comments:
PreserveOptions parseOpts = PreserveOptions.builder()
.preserveHeaderComments(true)
.preserveInlineComments(true)
.preserveBlankLines(true)
.build();
WriterOptions writeOpts = WriterOptions.builder()
.writeHeaderComments(true)
.writeInlineComments(true)
.writeBlankLines(true)
.maxLineLength(80)
.build();
TomlDocument doc = Tomly.parse(templatePath, false, parseOpts);
// Modify values...
doc.save(outputPath, writeOpts);Preserve structure, update values:
PreserveOptions parseOpts = PreserveOptions.builder()
.preserveHeaderComments(true)
.preserveBlankLines(true)
.build();
WriterOptions writeOpts = WriterOptions.builder()
.writeHeaderComments(true)
.writeBlankLines(true)
.build();
TomlDocument doc = Tomly.parse(configPath, false, parseOpts);
doc.set("version", new StringNode("2.0.0", List.of(), null));
doc.save(configPath, writeOpts);Minimize file size(amount of lines):
WriterOptions compact = WriterOptions.builder()
.maxLineLength(Integer.MAX_VALUE)
.build();
doc.save(Path.of("compact.toml"), compact);- Learn about the Type-Safe API for accessing values
- See Working With Comments for detailed comment handling