Changing default configurations common to both YamlWriter and YamlReader

In this post under YamlBeans, I will explain with example how to change default configurations common to both YamlWriter and YamlReader.

The YamlConfig class has getter and setters that can be used to change default configurations.

For this example, we will use the below Employee1 class structure.

Employee1 class


import java.util.List;

public class Employee1 {
    public String id;
    public String name;
    public String status;
    public float salary;
    public List phoneNumbers;
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.id).append(",");
        sb.append(this.name).append(",");
        sb.append(this.status).append(",");
        sb.append(this.salary);
        for(String phoneNumber : phoneNumbers) {
            sb.append("\n" + phoneNumber);
        }
        return sb.toString();
    }
}

In the above Employee1 structure, we haven’t added any accessor methods.

If we use the below code snippet, the YamlBeans will create two instances, one for YamlWriter and another for YamlReader with default configurations.


YamlWriter yamlWriter = new YamlWriter(stringWriter);
YamlReader yamlReader = new YamlReader(stringReader);

One of the configurations in YamlConfig enable us to instruct the YamlWriter and YamlReader to access values using bean properties instead of bean accessor methods. The method name to enable this feature is “setBeanProperties”. If set to true bean accessor methods are used to read/write values to/from beans. If set to false properties are accessed directly to read/write values to/from beans.

We can provide modified configurations to the YamlReader and YamlWriter as shown in the below code snippet


1   YamlConfig yamlConfig = new YamlConfig();
2   yamlConfig.setBeanProperties(false);
3   
4   YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig);
5   YamlReader yamlReader = new YamlReader(stringReader, yamlConfig);

In the above code snippet at line 1, We create an instance of YamlConfig.

At line 2, call the method “setBeanProperties” and set it to false.

At line 4 and 5 we create an instance of YamlReader and YamlWriter passing yamlConfig (an instance of YamlConfig) as argument to the constructor methods. This will create an instance of YamlReader which will set fields of Employee1 instance by accessing the properties directly and an instance of YamlWriter which will read values by accessing the properties directly.

Below is the complete Main class

Main class


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

import com.esotericsoftware.yamlbeans.YamlConfig;
import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.esotericsoftware.yamlbeans.YamlWriter;

public class YamlDemo3 {
    public static void main(String[] args) {
        YamlConfig yamlConfig = new YamlConfig();
        yamlConfig.setBeanProperties(false);
        
        List phoneNumbers = new ArrayList();
        phoneNumbers.add("phoneNumber1");
        phoneNumbers.add("phoneNumber2");
        
        Employee1 employee = new Employee1();
        employee.id = "1";
        employee.name = "name1";
        employee.salary = 1000;
        employee.status = "NEW";
        employee.phoneNumbers = phoneNumbers;
        
        StringWriter stringWriter = new StringWriter();
        String result = null;
        try {
            YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig);
            yamlWriter.write(employee);
            yamlWriter.close();
            result = stringWriter.toString();
            System.out.println(result);
        } catch(YamlException yamlException) {
            yamlException.printStackTrace();
        }
        
        employee = null;
        
        try {
            StringReader stringReader = new StringReader(result);
            YamlReader yamlReader = new YamlReader(stringReader, yamlConfig);
            employee = yamlReader.read(Employee1.class);
            yamlReader.close();
            System.out.println(employee);
        } catch(IOException ioException) {
            ioException.printStackTrace();
        }
    }
}

Below is the output generated by the Main class

Output

!Employee1
id: ‘1’
name: name1
phoneNumbers:
– phoneNumber1
– phoneNumber2
salary: 1000.0
status: NEW

1,name1,NEW,1000.0
phoneNumber1
phoneNumber2

Leave a Reply