Sunday, 3 June 2018

Jackson: Convert yaml string to java object

Below statements convert yaml string to an employee object.

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Employee emp = mapper.readValue(yaml, Employee.class);

Find the below working application.

Employee.java
package com.sample.model;

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 + "]";
 }

}


YAMLUtil.java
package com.sample.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class YAMLUtil {

 private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

 public static void printYAML(Object obj) throws JsonGenerationException, JsonMappingException, IOException {
  mapper.writeValue(System.out, obj);
 }

 public static String getYAML(Object obj) throws JsonProcessingException {
  return mapper.writeValueAsString(obj);
 }

 public static <T> T getObject(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  return mapper.readValue(yaml, clazz);

 }

 public static <T> List<T> getListOfObjects(String yaml, Class<T> clazz)
   throws JsonParseException, JsonMappingException, IOException {
  CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
  List<T> ts = mapper.readValue(yaml, listType);
  return ts;
 }

 public static <K, V> Map<K, V> getMapOfObjects(String yaml, Class<K> keyClazz, Class<V> valueClazz)
   throws JsonParseException, JsonMappingException, IOException {
  TypeFactory typeFactory = mapper.getTypeFactory();
  MapType mapType = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
  return mapper.readValue(yaml, mapType);
 }
}

Test.java
package com.sample.app;

import java.io.IOException;

import com.sample.model.Employee;
import com.sample.util.YAMLUtil;

public class Test {

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

  String yaml = YAMLUtil.getYAML(emp);

  Employee emp1 = YAMLUtil.getObject(yaml, Employee.class);

  System.out.println("id : " + emp1.getId());
  System.out.println("first name : " + emp1.getFirstName());
  System.out.println("last name : " + emp1.getLastName());
 }
}


Output
id : 1
first name : Krishna
last name : Gurram







Previous                                                 Next                                                 Home

2 comments:

  1. гора статей, и нигде нет исходного yaml, сразу java код, ценность статьи примерно ноль.

    ReplyDelete
  2. String yaml = YAMLUtil.getYAML(emp)

    Above line get u yaml string..spend some time to understand the code

    ReplyDelete