Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a toml file in java 2022

After a quick research, I found the following three libraries for parsing Toml files in java. toml4j tomlj jackson-dataformats-text

What I am looking for is a library that can parse toml files without a corresponding POJO class. While both toml4j, and tomlj can achieve that, they do not seem to be maintained.

jackson-dataformats-text on the other is actively maintained but I can not parse a toml file without the corresponding POJO class. Is there a way to create a dynamic class in java that I can use to parse any toml file?

like image 990
Rami ZK Avatar asked Oct 27 '25 08:10

Rami ZK


1 Answers

If you just need to read a TOML file without a POJO, FasterXML Jackson libraries are a great choice. The simplest method is to just read the content as a java.util.Map:

final var tomlMapper = new TomlMapper();
final var data = tomlMapper.readValue(new File("config.toml"), Map.class);

After that, the content of the file will be available in data.

If you need even lower level parsing, all formats supported by FasterXML Jackson can be read using the stream API. In case you need that, read about the stream API on the core module: FasterXML/jackson-core, just make sure you use the right factory class (TomlFactory instead of JsonFactory).

like image 139
Ramon Rivas Avatar answered Oct 28 '25 20:10

Ramon Rivas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!