1、簡(jiǎn)述
如何使用SnakeYAML庫(kù)將
YAML文檔轉(zhuǎn)換為Java對(duì)象,以及JAVA對(duì)象如何序列化為YAML文檔。
在DevOps平臺(tái)系統(tǒng)中是基礎(chǔ)的能力支持,不管是spring boot 的配置還是K8S 資源清單yaml
2、項(xiàng)目設(shè)置
要在項(xiàng)目中使用SnakeYAML,需要添加Maven依賴項(xiàng)(可在此處找到最新版本)
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>
3、入口點(diǎn)
該YAML
類是API的入口點(diǎn):
Yaml yaml = new Yaml()
由于實(shí)現(xiàn)不是線程安全的,因此不同的線程必須具有自己的Yaml
實(shí)例。
4、加載YAML文檔
SnakeYAML
支持從String
或InputStream
加載文檔,我們從定義一個(gè)簡(jiǎn)單的YAML文檔開(kāi)始,然后將文件命名為customer.yaml
:
基本用法
現(xiàn)在,我們將使用Yaml
類來(lái)解析上述YAML文檔:
public class YamlTest {
public static void main(String[] args) {
Yaml yaml = new Yaml();
InputStream inputStream = YamlTest.class
.getClassLoader()
.getResourceAsStream("customer.yaml");
Map<String, Object> obj = yaml.load(inputStream);
System.out.println(obj);
}
}
上面的代碼生成以下輸出:?
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-536040.html
默認(rèn)情況下,load()
方法返回一個(gè)Map
對(duì)象。查詢Map
對(duì)象時(shí),我們需要事先知道屬性鍵的名稱,否則容易出錯(cuò)。更好的辦法是自定義類型。
自定義類型解析
SnakeYAML
提供了一種將文檔解析為自定義類型的方法
讓我們定義一個(gè)Customer
類,然后嘗試再次加載該文檔:
package com.devops.autocicdstore.yaml;
/**
* @Author 虎哥
* @Description //TODO
* |要帶著問(wèn)題去學(xué)習(xí),多猜想多驗(yàn)證|
**/
public class Customer {
private String firstName;
private String lastName;
private int age;
// getters and setters
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Customer{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
'}';
}
}
現(xiàn)在我么來(lái)加載:?
Yaml yaml = new Yaml(new Constructor(Customer.class));
InputStream inputStream = YamlTest.class
.getClassLoader()
.getResourceAsStream("customer.yaml");
Customer customer = yaml.load(inputStream);
System.out.println(customer);
隱式類型
如果沒(méi)有為給定屬性定義類型,則庫(kù)會(huì)自動(dòng)將值轉(zhuǎn)換為隱式type。
例如:
1.0 -> Float
42 -> Integer
2009-03-30 -> Date
讓我們使用一個(gè)TestCase來(lái)測(cè)試這種隱式類型轉(zhuǎn)換:
@org.junit.Test
public void whenLoadYAML_thenLoadCorrectImplicitTypes() {
Yaml yaml = new Yaml();
Map<Object, Object> document = yaml.load("3.0: 2018-07-22");
System.out.println(document);
assertNotNull(document);
assertEquals(1, document.size());
assertTrue(document.containsKey(3.0d));
}
嵌套對(duì)象
SnakeYAML
?支持嵌套的復(fù)雜類型。
讓我們向“?customer.yaml”
添加“?聯(lián)系方式”
??和“?地址”?
詳細(xì)信息,
并將新文件另存為customer_with_contact_details_and_address.yaml.
。
現(xiàn)在,我們將分析新的YAML文檔
firstName: "John"
lastName: "Doe"
age: 31
contactDetails:
- type: "mobile"
number: 123456789
- type: "landline"
number: 456786868
homeAddress:
line: "Xyz, DEF Street"
city: "City Y"
state: "State Y"
zip: 345657
我們來(lái)更新java類:
package com.devops.autocicdstore.yaml;
import java.util.List;
/**
* @Author 虎哥
* @Description //TODO
* |要帶著問(wèn)題去學(xué)習(xí),多猜想多驗(yàn)證|
**/
public class Customer {
private String firstName;
private String lastName;
private int age;
private List<Contact> contactDetails;
private Address homeAddress;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Contact> getContactDetails() {
return contactDetails;
}
public void setContactDetails(List<Contact> contactDetails) {
this.contactDetails = contactDetails;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
@Override
public String toString() {
return "Customer{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
", contactDetails=" + contactDetails +
", homeAddress=" + homeAddress +
'}';
}
}
package com.devops.autocicdstore.yaml;
/**
* @Author 虎哥
* @Description //TODO
* |要帶著問(wèn)題去學(xué)習(xí),多猜想多驗(yàn)證|
**/
public class Address {
private String line;
private String city;
private String state;
private Integer zip;
public String getLine() {
return line;
}
public void setLine(String line) {
this.line = line;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Integer getZip() {
return zip;
}
public void setZip(Integer zip) {
this.zip = zip;
}
@Override
public String toString() {
return "Address{" +
"line='" + line + '\'' +
", city='" + city + '\'' +
", state='" + state + '\'' +
", zip=" + zip +
'}';
}
}
package com.devops.autocicdstore.yaml;
/**
* @Author 虎哥
* @Description //TODO
* |要帶著問(wèn)題去學(xué)習(xí),多猜想多驗(yàn)證|
**/
public class Contact {
private String type;
private int number;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public String toString() {
return "Contact{" +
"type='" + type + '\'' +
", number=" + number +
'}';
}
}
現(xiàn)在,我們來(lái)測(cè)試下Yaml
#load()
:
@Test
public void
whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObjectWithNestedObjects() {
Yaml yaml = new Yaml(new Constructor(Customer.class));
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("customer_with_contact_details_and_address.yaml");
Customer customer = yaml.load(inputStream);
System.out.println(customer);
assertNotNull(customer);
assertEquals("John", customer.getFirstName());
assertEquals("Doe", customer.getLastName());
assertEquals(31, customer.getAge());
assertNotNull(customer.getContactDetails());
assertEquals(2, customer.getContactDetails().size());
assertEquals("mobile", customer.getContactDetails()
.get(0)
.getType());
assertEquals(123456789, customer.getContactDetails()
.get(0)
.getNumber());
assertEquals("landline", customer.getContactDetails()
.get(1)
.getType());
assertEquals(456786868, customer.getContactDetails()
.get(1)
.getNumber());
assertNotNull(customer.getHomeAddress());
assertEquals("Xyz, DEF Street", customer.getHomeAddress()
.getLine());
}
?
類型安全的集合
當(dāng)給定Java類的一個(gè)或多個(gè)屬性是泛型集合類時(shí),需要通過(guò)TypeDescription
來(lái)指定泛型類型,以便可以正確解析。
讓我們假設(shè)?一個(gè)Customer
擁有多個(gè)Contact
:
firstName: "John"
lastName: "Doe"
age: 31
contactDetails:
- { type: "mobile", number: 123456789}
- { type: "landline", number: 123456789}
為了能正確解析,我們可以在頂級(jí)類上為給定屬性指定TypeDescription?
:
@Test
public void test1(){
Constructor constructor = new Constructor(Customer.class);
TypeDescription customTypeDescription = new TypeDescription(Customer.class);
customTypeDescription.addPropertyParameters("contactDetails", Contact.class);
constructor.addTypeDescription(customTypeDescription);
Yaml yaml = new Yaml(new Constructor(Customer.class));
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("customer_with_contact_details_and_address.yaml");
Customer customer = yaml.load(inputStream);
System.out.println(customer);
}
?
?
載入多個(gè)文件
在某些情況下,單個(gè)文件中
可能有多個(gè)YAML文檔,而我們想解析所有文檔。所述YAML
類提供了一個(gè)LOADALL()
方法來(lái)完成這種類型的解析。
假設(shè)下面的內(nèi)容在一個(gè)文件中:
---
firstName: "John"
lastName: "Doe"
age: 20
---
firstName: "Jack"
lastName: "Jones"
age: 25
我們可以使用loadAll()
方法解析以上內(nèi)容,如以下代碼示例所示:
@Test
public void whenLoadMultipleYAMLDocuments_thenLoadCorrectJavaObjects() {
Yaml yaml = new Yaml(new Constructor(Customer.class));
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("customers.yaml");
int count = 0;
for (Object object : yaml.loadAll(inputStream)) {
count++;
assertTrue(object instanceof Customer);
System.out.println(object);
}
assertEquals(2,count);
}
5、生成YAML文件
SnakeYAML
?支持 將java對(duì)象序列化為yml。
基本用法
我們將從一個(gè)將Map <String,Object>
的實(shí)例轉(zhuǎn)儲(chǔ)到Y(jié)AML文檔(String
)的簡(jiǎn)單示例開(kāi)始:
@Test
public void whenDumpMap_thenGenerateCorrectYAML() {
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("name", "Silenthand Olleander");
data.put("race", "Human");
data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
Yaml yaml = new Yaml();
StringWriter writer = new StringWriter();
yaml.dump(data, writer);
String expectedYaml = "name: Silenthand Olleander\nrace: Human\ntraits: [ONE_HAND, ONE_EYE]\n";
System.out.println(writer);
assertEquals(expectedYaml, writer.toString());
}
上面的代碼產(chǎn)生以下輸出(請(qǐng)注意,使用LinkedHashMap
的實(shí)例將保留輸出數(shù)據(jù)的順序):
自定義Java對(duì)象
我們還可以選擇將自定義Java類型轉(zhuǎn)儲(chǔ)到輸出流中。
@Test
public void whenDumpACustomType_thenGenerateCorrectYAML() {
Customer customer = new Customer();
customer.setAge(45);
customer.setFirstName("Greg");
customer.setLastName("McDowell");
Yaml yaml = new Yaml();
StringWriter writer = new StringWriter();
yaml.dump(customer, writer);
String expectedYaml = "!!com.devops.autocicdstore.yaml.Customer {age: 45, contactDetails: null, firstName: Greg,\n homeAddress: null, lastName: McDowell}\n";
System.out.println(writer);
assertEquals(expectedYaml, writer.toString());
}
??
?
生成內(nèi)容會(huì)包含!!com.devops.autocicdstore.yaml.Customer,為了避免在輸出文件中使用標(biāo)簽名,我們可以使用庫(kù)提供的??dumpAs()
方法。
因此,在上面的代碼中,我們可以進(jìn)行以下調(diào)整以刪除標(biāo)記:
yaml.dumpAs(customer, Tag.MAP, null);
String ccustomerStr = yaml.dumpAs(customer, Tag.MAP, null);
System.out.println(ccustomerStr);
?
?
?本文說(shuō)明了SnakeYAML庫(kù)解析和序列化YAML文檔。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-536040.html
到了這里,關(guān)于DevOps系列文章 之 SnakeYAML解析與序列化YAML的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!