Sunday, 3 June 2018

YAML: - : represent list members

List members are denoted by a leading hyphen (-) with one member per line, or enclosed in square brackets ([ ]) and separated by comma space (, ).


employee.yaml
# YAML file represent employee details
---
- id: 1
  firstName: "Krishna"
  lastName: "Gurram"
  address:
    city: "Bangalore"
    country: "India"
    street: "temple street"
    pinCode: "12345"
- id: 2
  firstName: "Siva"
  lastName: "Ponnam"
  address:
    city: "Nuzvid"
    country: "India"
    street: "KV Palem"
    pinCode: "187652345"

Below java application reads above employees information and convert it into list of employees.

Address.java
package com.sample.model;

public class Address {
 private String city;
 private String country;
 private String street;
 private String pinCode;

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public String getStreet() {
  return street;
 }

 public void setStreet(String street) {
  this.street = street;
 }

 public String getPinCode() {
  return pinCode;
 }

 public void setPinCode(String pinCode) {
  this.pinCode = pinCode;
 }

 @Override
 public String toString() {
  return "Address [city=" + city + ", country=" + country + ", street=" + street + ", pinCode=" + pinCode + "]";
 }

}

Employee.java

package com.sample.model;

public class Employee {

 private int id;
 private String firstName;
 private String lastName;
 private Address address;

 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 Address getAddress() {
  return address;
 }

 public void setAddress(Address address) {
  this.address = address;
 }

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

}

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 java.util.List;

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

  List<Employee> emps = YAMLUtil.getListOfObjects(fileString, Employee.class);

  for(int i=0; i < emps.size(); i++) {
   Employee emp = emps.get(i);
   System.out.println(emp);
  }
 }
}

Output

Employee [id=1, firstName=Krishna, lastName=Gurram, address=Address [city=Bangalore, country=India, street=temple street, pinCode=12345]]
Employee [id=2, firstName=Siva, lastName=Ponnam, address=Address [city=Nuzvid, country=India, street=KV Palem, pinCode=187652345]]


Previous                                                 Next                                                 Home

No comments:

Post a Comment