In this post under YamlBeans, I will explain with example how to change default configurations specific to YamlWriter.
We take the help of YamlConfig.WriteConfig class. It is a inner static class. It has getters and setters that can be used to change default configurations specific to YamlWriter.
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 YamlWriter with default configurations.
YamlWriter yamlWriter = new YamlWriter(fileWriter);
One of the configurations in YamlConfig.WriteConfig is to set whether to write class name in the serialized data or not, as shown at line 1 in below json. At line we have Employee class name prefixed by “!” symbol.
1 !Employee
2 id: '1'
3 name: name1
4 phoneNumbers:
5 - phoneNumber1
6 - phoneNumber2
7 salary: 1000.0
8 status: NEW
We can set this configuration by calling “setWriteClassname” on YamlConfig.WriteConfig instance and passing one of the below three Enums as arguments
YamlConfig.WriteClassName.ALWAYS
YamlConfig.WriteClassName.AUTO
YamlConfig.WriteClassName.NEVER
We can provide a modified configurations to the YamlWriter as shown in the below code snippet
1 YamlConfig yamlConfig = new YamlConfig();
2 yamlConfig.writeConfig.setWriteClassname(WriteClassName.NEVER);
3 YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig);
At line 1, we create an instance of YamlConfig.
At line 2, we call “setWriteClassname” on writeConfig instance accessed through yamlConfig instance.
At line 3, we create an instance of YamlWriter passing yamlConfig (an instance of YamlConfig) as argument to the constructor methods. This will create an instance of YamlWriter which when serializing the java object doesn’t write the class name, since we call “setWriteClassname” setting it with “WriteClassName.NEVER” enum.
Below is the complete Main class
Main Class
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import com.esotericsoftware.yamlbeans.YamlConfig;
import com.esotericsoftware.yamlbeans.YamlConfig.WriteClassName;
import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlWriter;
public class YamlDemo5 {
public static void main(String[] args) {
List phoneNumbers = new ArrayList();
phoneNumbers.add("phoneNumber1");
phoneNumbers.add("phoneNumber2");
Employee employee = new Employee();
employee.setId("1");
employee.setName("name1");
employee.setSalary(1000);
employee.setStatus("NEW");
employee.setPhoneNumbers(phoneNumbers);
YamlWriter yamlWriter = null;
StringWriter stringWriter = new StringWriter();
String result = null;
try {
yamlWriter = new YamlWriter(stringWriter);
yamlWriter.write(employee);
yamlWriter.close();
result = stringWriter.toString();
System.out.println(result);
} catch(YamlException yamlException) {
yamlException.printStackTrace();
}
System.out.println("-------------------------");
YamlConfig yamlConfig = new YamlConfig();
yamlConfig.writeConfig.setWriteClassname(WriteClassName.NEVER);
stringWriter = new StringWriter();
try {
yamlWriter = new YamlWriter(stringWriter, yamlConfig);
yamlWriter.write(employee);
yamlWriter.close();
result = stringWriter.toString();
System.out.println(result);
} catch(IOException excep) {
excep.printStackTrace();
}
}
}
Below is the output generated by the Main class
Output
!Employee
id: ‘1’
name: name1
phoneNumbers:
– phoneNumber1
– phoneNumber2
salary: 1000.0
status: NEW
id: ‘1’
name: name1
phoneNumbers:
– phoneNumber1
– phoneNumber2
salary: 1000.0
status: NEW
As you see in the Main class, we created an instance of Employee and serialized it to String. In the output (before the separator) we see the serialized data containing the class name. Then we again serialized the employee object this time we are saying not to write the class name. As you can see from the output after the separator the class name is not written.