Tuesday 27 September 2022

Jackson: Parse TOML string in Java

In this post, I am going to explain how to parse TOML data using JsonParser.

 

Step 1: Get an instance of TomlFactory.

TomlFactory tomlFactory = new TomlFactory();

 

Step 2: Get an instance of JsonParser from toml content.

JsonParser parser = tomlFactory.createParser(new File(FileUtil.resourceFilePath("tomlParserDemo1.toml")));

 

Step 3: Use parser APIS like nextToken, getText to process the toml content.

 

Find the below working application.

 

tomlParserDemo1.toml

 

id = "1"
firstName = "krishna"
lastName = "Gurram"
hobbies = [ "Trekking", "Blogging", "Cooking" ]

 

FileUtil.java

package com.sample.app.util;

import java.net.URL;

public class FileUtil {

	public static String resourceFilePath(String resourceFile) {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceFile);
		return url.getPath();
	}

}

ParseTomlString.java

package com.sample.app;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.dataformat.toml.TomlFactory;
import com.sample.app.util.FileUtil;

public class ParseTomlString {

	public static void main(String[] args) throws IOException {

		TomlFactory tomlFactory = new TomlFactory();
		JsonParser parser = tomlFactory.createParser(new File(FileUtil.resourceFilePath("tomlParserDemo1.toml")));

		while (!parser.isClosed()) {
			JsonToken token = parser.nextToken();

			/* null indicates end-of-input */
			if (token == null)
				break;

			String fieldName = parser.getCurrentName();

			if ("id".equals(fieldName)) {
				parser.nextToken();
				System.out.println("id : " + parser.getText());
			} else if ("firstName".equals(fieldName)) {
				parser.nextToken();
				System.out.println("firstName : " + parser.getText());
			} else if ("lastName".equals(fieldName)) {
				parser.nextToken();
				System.out.println("lastName : " + parser.getText());
			} else if ("hobbies".equals(fieldName)) {
				System.out.println("Hobbies:");
				parser.nextToken();
				while (parser.nextToken() != JsonToken.END_ARRAY) {
					String field = parser.getText();
					System.out.println("\t" + field);
				}
			}
		}

		parser.close();

	}

}

Output

id : 1
firstName : krishna
lastName : Gurram
Hobbies:
	Trekking
	Blogging
	Cooking

Dependency used

<dependencies>
  <dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-toml</artifactId>
    <version>2.13.4</version>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.4</version>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.13.4</version>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.13.4</version>
  </dependency>

</dependencies>

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment