简体   繁体   English

读取 java 2022 中的 toml 文件

[英]Read a toml file in java 2022

After a quick research, I found the following three libraries for parsing Toml files in java.经过快速研究,我在 java 中找到了以下三个用于解析 Toml 文件的库。 toml4j tomlj jackson-dataformats-text toml4j tomlj 杰克逊-数据格式-文本

What I am looking for is a library that can parse toml files without a corresponding POJO class.我正在寻找的是一个可以在没有相应 POJO class 的情况下解析 toml 文件的库。 While both toml4j , and tomlj can achieve that, they do not seem to be maintained.虽然toml4jtomlj都可以实现这一点,但它们似乎没有得到维护。

jackson-dataformats-text on the other is actively maintained but I can not parse a toml file without the corresponding POJO class.另一方面, jackson-dataformats-text得到积极维护,但如果没有相应的 POJO class,我无法解析 toml 文件。 Is there a way to create a dynamic class in java that I can use to parse any toml file?有没有办法在 java 中创建一个动态 class ,我可以用它来解析任何 toml 文件?

If you just need to read a TOML file without a POJO, FasterXML Jackson libraries are a great choice.如果您只需要读取没有 POJO 的 TOML 文件,FasterXML Jackson 库是一个不错的选择。 The simplest method is to just read the content as a java.util.Map :最简单的方法是将内容读取为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 .之后,文件的内容将在data中可用。

If you need even lower level parsing, all formats supported by FasterXML Jackson can be read using the stream API.如果您需要更低级别的解析,可以使用 stream API 读取 FasterXML Jackson 支持的所有格式。 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 ).如果您需要,请阅读核心模块上的 stream API: FasterXML/jackson-core ,只需确保使用正确的工厂 class ( JsonFactory TomlFactory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM