Comments
are used to document YAML file. Comments begin with # symbol and they can start
anywhere in a line and continue till the end of the line.
employee.yml
# YAML file represent employee details id: 1 firstName: "Krishna" lastName: "Gurram" address: city: "Bangalore" country: "India" street: "temple street" pinCode: "12345"
Below
java application read the contents of employee.yml file and map them to an Employee
instance.
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 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.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); } }
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, address=Address [city=Bangalore, country=India, street=temple street, pinCode=12345]]
No comments:
Post a Comment