Skip to content
Advertisement

How to parse TOML document with Java

In my Java app I get a TOML document from REST service response as String and I look how to parse it and convert to Java objects.

Ideally I’d like to use some existing mapper library for deserialization, something like Jackson or Gson for JSON objects. Are there any options in Java ecosystem for that?

Advertisement

Answer

There are several libraries to work with TOML files in Java, such as mwanji/toml4j and tomlj/tomlj. The toml4j looks more maintained and has the ability to map the TOML file to your POJO classes, so it should perfectly match your needs.

From its documentation:

Toml#to(Class<T>) maps a Toml instance to the given class.

TOML file example:

JavaScript

POJO classes example:

JavaScript

How to map TOML file to POJO classes:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement