Tuesday 27 September 2022

Jackson: Convert TOML to a map in java

Using TypeReference , we can map JSON data to a map.

Map<Integer, Employee> map = tomlMapper.readValue(
		toml, new TypeReference<Map<Integer, Employee>>() {
});

 

Find the below working application.

 

Employee.java
package com.sample.app.dto;

public class Employee {
	private int id;

	private String firstName;

	private String lastName;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

TomlToMap.java

package com.sample.app;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import com.sample.app.dto.Employee;

public class TomlToMap {

	public static void main(String[] args) throws JsonProcessingException {
		Employee emp1 = new Employee();
		emp1.setFirstName("Krishna");
		emp1.setId(1);
		emp1.setLastName("Gurram");

		Employee emp2 = new Employee();
		emp2.setFirstName("Ram");
		emp2.setId(2);
		emp2.setLastName("Maj");

		Map<Integer, Employee> empsMap = new HashMap<>();
		empsMap.put(1, emp1);
		empsMap.put(2, emp2);

		TomlMapper tomlMapper = new TomlMapper();
		String toml = tomlMapper.writeValueAsString(empsMap);
		System.out.println(toml);

		Map<Integer, Employee> map = tomlMapper.readValue(
				toml, new TypeReference<Map<Integer, Employee>>() {
		});

		for (Integer key : map.keySet()) {
			Employee emp = map.get(key);
			System.out.println(emp);
		}

	}

}

Output

1.id = 1
1.firstName = 'Krishna'
1.lastName = 'Gurram'
2.id = 2
2.firstName = 'Ram'
2.lastName = 'Maj'

Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Ram, lastName=Maj]

Following toml

1.id = 1
1.firstName = 'Krishna'
1.lastName = 'Gurram'
2.id = 2
2.firstName = 'Ram'
2.lastName = 'Maj'

is equivalent to below json.

{
  "1": {
    "id": 1,
    "firstName": "Krishna",
    "lastName": "Gurram"
  },
  "2": {
    "id": 2,
    "firstName": "Ram",
    "lastName": "Maj"
  }
}

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