Changing default configurations specific to YamlReader

In this post under YamlBeans, I will explain with example how to change default configurations specific to YamlReader.

We take the help of YamlConfig.ReadConfig class. It is a inner static class. It has getters and setters that can be used to change default configurations specific to YamlReader.

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

Employee Class


import java.util.List;

public class Employee {
    private String id;
    private String name;
    private String status;
    private float salary;
    private List phoneNumbers;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public float getSalary() {
        return salary;
    }
    public void setSalary(float salary) {
        this.salary = salary;
    }
    public List getPhoneNumbers() {
        return phoneNumbers;
    }
    public void setPhoneNumbers(List phoneNumbers) {
        this.phoneNumbers = 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);
        return sb.toString();
    }
}

If we use the below code snippet, the YamlBeans will create instance of YamlReader with default configurations.


YamlReader yamlReader = new YamlReader(fileReader);

One of the configurations in YamlConfig.ReadConfig is to toggle the feature of throwing exceptions when a field in YAML data is not found in its corresponding class. In other words if this configuration is false and while deseralizing a YAML data, if YamlBeans find a field that is not available in its corresponding class, it throws an YamlException. If this configuration is true and while deseralizing a YAML data, if YamlBeans find a field that is not available in its corresponding class, it doesn’t throw the YamlException, it just ignores it. We toggle this configuration by calling “setIgnoreUnknownProperties”.

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


1   YamlConfig yamlConfig = new YamlConfig();
2   yamlConfig.readConfig.setIgnoreUnknownProperties(true);
3   YamlReader yamlReader = new YamlReader(fileReader, yamlConfig);

At line 1, we create an instance of YamlConfig.

At line 2, we call “setIgnoreUnknownProperties” on readConfig instance accessed through yamlConfig instance.

At line 3, we create an instance of YamlReader passing yamlConfig (an instance of YamlConfig) as argument to the constructor methods. This will create an instance of YamlReader which will not throw YamlException if field from YAML data doesn’t available in its corresponding class.

Below is the complete Main class

Main class


import java.io.File;
import java.io.FileReader;
import java.io.IOException;

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

public class YamlDemo4 {
    public static void main(String[] args) {
        File file = new File("input2.yml");
        YamlReader yamlReader = null;
        
        System.out.println("Setting Ignore Unknown Properties to false");
        try(FileReader fileReader = new FileReader(file);) {
            yamlReader = new YamlReader(fileReader);
            Employee employee = yamlReader.read(Employee.class);
            yamlReader.close();
            System.out.println(employee);
        } catch(YamlException excep) {
            excep.printStackTrace();
        } catch(IOException excep) {
            excep.printStackTrace();
        }
        
        System.out.println();
        System.out.println("Setting Ignore Unknown Properties to true");
        try(FileReader fileReader = new FileReader(file);) {
            YamlConfig yamlConfig = new YamlConfig();
            yamlConfig.readConfig.setIgnoreUnknownProperties(true);
            yamlReader = new YamlReader(fileReader, yamlConfig);
            Employee employee = yamlReader.read(Employee.class);
            yamlReader.close();
            System.out.println(employee);
        } catch(YamlException excep) {
            excep.printStackTrace();
        } catch(IOException excep) {
            excep.printStackTrace();
        }
    }
}

Below is the output generated by the Main class

Output

Setting Ignore Unknown Properties to false
com.esotericsoftware.yamlbeans.YamlReader$YamlReaderException: Line 8, column 4: Unable to find property ‘age’ on class: Employee
at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:374)
at com.esotericsoftware.yamlbeans.YamlReader.readValue(YamlReader.java:138)
at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:105)
at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:92)
at YamlDemo4.main(YamlDemo4.java:17)

Setting Ignore Unknown Properties to true
1,name1,NEW,1000.0

In the main class we are reading “input2.yml” file twice. When we read the file first time, the boolean “IgnoreUnknownProperties” is set to false, as a result it throws the exception for the field “age” as we don’t have a corresponding property in Employee class. When we read the file again, this time we set the boolean “IgnoreUnknownProperties” to true, as a result it doesn’t throw any exception, it ignores the field “age”.

Below is the data of input2.yml file

input2.yml


!Employee
id: '1'
name: name1
phoneNumbers: 
- phoneNumber1
- phoneNumber2
salary: 1000.0
status: NEW
age: 35

Leave a Reply