Sunday 3 June 2018

YAML: Represent multi line strings

There are different ways to write multi line strings in yaml.

Using |
| symbol preserve new lines.

Employee.yml
# YAML file represent employee details
---
id: 1
firstName: "Krishna"
lastName: "Gurram"
aboutMe: |
   Failure never overtake me
   if my determination to succeed is
   strong enough.


After converting above yml to employee object, I printed them.
System.out.println("id : " + emp.getId());
System.out.println("First Name : " + emp.getFirstName());
System.out.println("Last Name: " + emp.getLastName());
System.out.println("About Me : " + emp.getAboutMe());

In console, they are printed like below.

id : 1
First Name : Krishna
Last Name: Gurram
About Me : Failure never overtake me
if my determination to succeed is
strong enough.

Using >
‘>’ character folds the newlines.

employee.yml
# YAML file represent employee details
---
id: 1
firstName: "Krishna"
lastName: "Gurram"
aboutMe: >
   Failure never overtake me
   if my determination to succeed is
   strong enough.

After converting above yml to employee object, I printed them.
System.out.println("id : " + emp.getId());
System.out.println("First Name : " + emp.getFirstName());
System.out.println("Last Name: " + emp.getLastName());
System.out.println("About Me : " + emp.getAboutMe());

In console, they are printed like below.

id : 1
First Name : Krishna
Last Name: Gurram
About Me : Failure never overtake me if my determination to succeed is strong enough.

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {

 private int id;
 private String firstName;
 private String lastName;
 private String aboutMe;

 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 getAboutMe() {
  return aboutMe;
 }

 public void setAboutMe(String aboutMe) {
  this.aboutMe = aboutMe;
 }

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

}

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("id : " + emp.getId());
  System.out.println("First Name : " + emp.getFirstName());
  System.out.println("Last Name: " + emp.getLastName());
  System.out.println("About Me : " + emp.getAboutMe());

 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment