Sunday, 3 June 2018

YAML: [] : represent list or array members

In my previous post, I explained, how to represent list of members using – symbol. You can also use [] notation to represent list (or) array of objects.



employee.yaml
# YAML file represent employee details
id: 1
firstName: "Krishna"
lastName: "Gurram"
hobbies : ["Chess", "Cricket"]

Employee.java
package com.sample.model;

import java.util.Arrays;

public class Employee {

 private int id;
 private String firstName;
 private String lastName;
 private String[] hobbies;

 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;
 }

 public String[] getHobbies() {
  return hobbies;
 }

 public void setHobbies(String[] hobbies) {
  this.hobbies = hobbies;
 }

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

}

YAMLUtil.java

package com.sample.util;

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

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
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 <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;

 }
 
}

Test.java

package com.sample.app;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.sample.model.Employee;
import com.sample.util.YAMLUtil;

public class Test {

 private static final String filePath = "C:\\Users\\krishna\\Documents\\Study\\YAML\\yaml files\\employee.yml";

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

  String fileString = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);

  Employee emp = YAMLUtil.getObject(fileString, Employee.class);

  System.out.println(emp);

 }
}


Output

Employee [id=1, firstName=Krishna, lastName=Gurram, hobbies=[Chess, Cricket]]


Previous                                                 Next                                                 Home

No comments:

Post a Comment