当前位置:   article > 正文

DevOps系列文章 之 SnakeYAML解析与序列化YAML_snakeyml 竖线

snakeyml 竖线

1、简述

如何使用SnakeYAML库将
YAML文档转换为Java对象,以及JAVA对象如何序列化为YAML文档

DevOps平台系统中是基础的能力支持,不管是spring boot 的配置还是K8S 资源清单yaml

2、项目设置

要在项目中使用SnakeYAML,需要添加Maven依赖项(可在此处找到最新版本)

  1. <dependency>
  2. <groupId>org.yaml</groupId>
  3. <artifactId>snakeyaml</artifactId>
  4. <version>1.25</version>
  5. </dependency>

3、入口点

YAML类是API的入口点:

Yaml yaml = new Yaml()

由于实现不是线程安全的,因此不同的线程必须具有自己的Yaml实例。

4、加载YAML文档

SnakeYAML支持从StringInputStream加载文档,我们从定义一个简单的YAML文档开始,然后将文件命名为customer.yaml

基本用法

现在,我们将使用Yaml类来解析上述YAML文档:

  1. public class YamlTest {
  2. public static void main(String[] args) {
  3. Yaml yaml = new Yaml();
  4. InputStream inputStream = YamlTest.class
  5. .getClassLoader()
  6. .getResourceAsStream("customer.yaml");
  7. Map<String, Object> obj = yaml.load(inputStream);
  8. System.out.println(obj);
  9. }
  10. }

上面的代码生成以下输出: 

 

默认情况下,load()方法返回一个Map对象。查询Map对象时,我们需要事先知道属性键的名称,否则容易出错。更好的办法是自定义类型。

自定义类型解析

SnakeYAML提供了一种将文档解析为自定义类型的方法

让我们定义一个Customer类,然后尝试再次加载该文档:

  1. package com.devops.autocicdstore.yaml;
  2. /**
  3. * @Author 虎哥
  4. * @Description //TODO
  5. * |要带着问题去学习,多猜想多验证|
  6. **/
  7. public class Customer {
  8. private String firstName;
  9. private String lastName;
  10. private int age;
  11. // getters and setters
  12. public String getFirstName() {
  13. return firstName;
  14. }
  15. public void setFirstName(String firstName) {
  16. this.firstName = firstName;
  17. }
  18. public String getLastName() {
  19. return lastName;
  20. }
  21. public void setLastName(String lastName) {
  22. this.lastName = lastName;
  23. }
  24. public int getAge() {
  25. return age;
  26. }
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30. @Override
  31. public String toString() {
  32. return "Customer{" +
  33. "firstName='" + firstName + '\'' +
  34. ", lastName='" + lastName + '\'' +
  35. ", age=" + age +
  36. '}';
  37. }
  38. }

现在我么来加载: 

  1. Yaml yaml = new Yaml(new Constructor(Customer.class));
  2. InputStream inputStream = YamlTest.class
  3. .getClassLoader()
  4. .getResourceAsStream("customer.yaml");
  5. Customer customer = yaml.load(inputStream);
  6. System.out.println(customer);

隐式类型

如果没有为给定属性定义类型,则库会自动将值转换为隐式type

例如:

  1. 1.0 -> Float
  2. 42 -> Integer
  3. 2009-03-30 -> Date

让我们使用一个TestCase来测试这种隐式类型转换:

  1. @org.junit.Test
  2. public void whenLoadYAML_thenLoadCorrectImplicitTypes() {
  3. Yaml yaml = new Yaml();
  4. Map<Object, Object> document = yaml.load("3.0: 2018-07-22");
  5. System.out.println(document);
  6. assertNotNull(document);
  7. assertEquals(1, document.size());
  8. assertTrue(document.containsKey(3.0d));
  9. }

嵌套对象

SnakeYAML 支持嵌套的复杂类型。

让我们向“ customer.yaml”添加“ 联系方式”  和“ 地址” 详细信息并将新文件另存为customer_with_contact_details_and_address.yaml.

现在,我们将分析新的YAML文档

  1. firstName: "John"
  2. lastName: "Doe"
  3. age: 31
  4. contactDetails:
  5. - type: "mobile"
  6. number: 123456789
  7. - type: "landline"
  8. number: 456786868
  9. homeAddress:
  10. line: "Xyz, DEF Street"
  11. city: "City Y"
  12. state: "State Y"
  13. zip: 345657

我们来更新java类:

  1. package com.devops.autocicdstore.yaml;
  2. import java.util.List;
  3. /**
  4. * @Author 虎哥
  5. * @Description //TODO
  6. * |要带着问题去学习,多猜想多验证|
  7. **/
  8. public class Customer {
  9. private String firstName;
  10. private String lastName;
  11. private int age;
  12. private List<Contact> contactDetails;
  13. private Address homeAddress;
  14. public String getFirstName() {
  15. return firstName;
  16. }
  17. public void setFirstName(String firstName) {
  18. this.firstName = firstName;
  19. }
  20. public String getLastName() {
  21. return lastName;
  22. }
  23. public void setLastName(String lastName) {
  24. this.lastName = lastName;
  25. }
  26. public int getAge() {
  27. return age;
  28. }
  29. public void setAge(int age) {
  30. this.age = age;
  31. }
  32. public List<Contact> getContactDetails() {
  33. return contactDetails;
  34. }
  35. public void setContactDetails(List<Contact> contactDetails) {
  36. this.contactDetails = contactDetails;
  37. }
  38. public Address getHomeAddress() {
  39. return homeAddress;
  40. }
  41. public void setHomeAddress(Address homeAddress) {
  42. this.homeAddress = homeAddress;
  43. }
  44. @Override
  45. public String toString() {
  46. return "Customer{" +
  47. "firstName='" + firstName + '\'' +
  48. ", lastName='" + lastName + '\'' +
  49. ", age=" + age +
  50. ", contactDetails=" + contactDetails +
  51. ", homeAddress=" + homeAddress +
  52. '}';
  53. }
  54. }
  1. package com.devops.autocicdstore.yaml;
  2. /**
  3. * @Author 虎哥
  4. * @Description //TODO
  5. * |要带着问题去学习,多猜想多验证|
  6. **/
  7. public class Address {
  8. private String line;
  9. private String city;
  10. private String state;
  11. private Integer zip;
  12. public String getLine() {
  13. return line;
  14. }
  15. public void setLine(String line) {
  16. this.line = line;
  17. }
  18. public String getCity() {
  19. return city;
  20. }
  21. public void setCity(String city) {
  22. this.city = city;
  23. }
  24. public String getState() {
  25. return state;
  26. }
  27. public void setState(String state) {
  28. this.state = state;
  29. }
  30. public Integer getZip() {
  31. return zip;
  32. }
  33. public void setZip(Integer zip) {
  34. this.zip = zip;
  35. }
  36. @Override
  37. public String toString() {
  38. return "Address{" +
  39. "line='" + line + '\'' +
  40. ", city='" + city + '\'' +
  41. ", state='" + state + '\'' +
  42. ", zip=" + zip +
  43. '}';
  44. }
  45. }
  1. package com.devops.autocicdstore.yaml;
  2. /**
  3. * @Author 虎哥
  4. * @Description //TODO
  5. * |要带着问题去学习,多猜想多验证|
  6. **/
  7. public class Contact {
  8. private String type;
  9. private int number;
  10. public String getType() {
  11. return type;
  12. }
  13. public void setType(String type) {
  14. this.type = type;
  15. }
  16. public int getNumber() {
  17. return number;
  18. }
  19. public void setNumber(int number) {
  20. this.number = number;
  21. }
  22. @Override
  23. public String toString() {
  24. return "Contact{" +
  25. "type='" + type + '\'' +
  26. ", number=" + number +
  27. '}';
  28. }
  29. }

现在,我们来测试下Yamlload()

  1. @Test
  2. public void
  3. whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObjectWithNestedObjects() {
  4. Yaml yaml = new Yaml(new Constructor(Customer.class));
  5. InputStream inputStream = this.getClass()
  6. .getClassLoader()
  7. .getResourceAsStream("customer_with_contact_details_and_address.yaml");
  8. Customer customer = yaml.load(inputStream);
  9. System.out.println(customer);
  10. assertNotNull(customer);
  11. assertEquals("John", customer.getFirstName());
  12. assertEquals("Doe", customer.getLastName());
  13. assertEquals(31, customer.getAge());
  14. assertNotNull(customer.getContactDetails());
  15. assertEquals(2, customer.getContactDetails().size());
  16. assertEquals("mobile", customer.getContactDetails()
  17. .get(0)
  18. .getType());
  19. assertEquals(123456789, customer.getContactDetails()
  20. .get(0)
  21. .getNumber());
  22. assertEquals("landline", customer.getContactDetails()
  23. .get(1)
  24. .getType());
  25. assertEquals(456786868, customer.getContactDetails()
  26. .get(1)
  27. .getNumber());
  28. assertNotNull(customer.getHomeAddress());
  29. assertEquals("Xyz, DEF Street", customer.getHomeAddress()
  30. .getLine());
  31. }

 

类型安全的集合

当给定Java类的一个或多个属性是泛型集合类时,需要通过TypeDescription来指定泛型类型,以便可以正确解析。

让我们假设 一个Customer拥有多个Contact

  1. firstName: "John"
  2. lastName: "Doe"
  3. age: 31
  4. contactDetails:
  5. - { type: "mobile", number: 123456789}
  6. - { type: "landline", number: 123456789}

为了能正确解析,我们可以在顶级类上为给定属性指定TypeDescription 

  1. @Test
  2. public void test1(){
  3. Constructor constructor = new Constructor(Customer.class);
  4. TypeDescription customTypeDescription = new TypeDescription(Customer.class);
  5. customTypeDescription.addPropertyParameters("contactDetails", Contact.class);
  6. constructor.addTypeDescription(customTypeDescription);
  7. Yaml yaml = new Yaml(new Constructor(Customer.class));
  8. InputStream inputStream = this.getClass()
  9. .getClassLoader()
  10. .getResourceAsStream("customer_with_contact_details_and_address.yaml");
  11. Customer customer = yaml.load(inputStream);
  12. System.out.println(customer);
  13. }

 

 

载入多个文件

在某些情况下,单个文件中可能有多个YAML文档,而我们想解析所有文档。所述YAML类提供了一个LOADALL()方法来完成这种类型的解析。

假设下面的内容在一个文件中:

  1. ---
  2. firstName: "John"
  3. lastName: "Doe"
  4. age: 20
  5. ---
  6. firstName: "Jack"
  7. lastName: "Jones"
  8. age: 25

我们可以使用loadAll()方法解析以上内容,如以下代码示例所示:

  1. @Test
  2. public void whenLoadMultipleYAMLDocuments_thenLoadCorrectJavaObjects() {
  3. Yaml yaml = new Yaml(new Constructor(Customer.class));
  4. InputStream inputStream = this.getClass()
  5. .getClassLoader()
  6. .getResourceAsStream("customers.yaml");
  7. int count = 0;
  8. for (Object object : yaml.loadAll(inputStream)) {
  9. count++;
  10. assertTrue(object instanceof Customer);
  11. System.out.println(object);
  12. }
  13. assertEquals(2,count);
  14. }

5、生成YAML文件

SnakeYAML 支持 将java对象序列化为yml。

基本用法

我们将从一个将Map <String,Object>的实例转储到YAML文档(String)的简单示例开始:

  1. @Test
  2. public void whenDumpMap_thenGenerateCorrectYAML() {
  3. Map<String, Object> data = new LinkedHashMap<String, Object>();
  4. data.put("name", "Silenthand Olleander");
  5. data.put("race", "Human");
  6. data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
  7. Yaml yaml = new Yaml();
  8. StringWriter writer = new StringWriter();
  9. yaml.dump(data, writer);
  10. String expectedYaml = "name: Silenthand Olleander\nrace: Human\ntraits: [ONE_HAND, ONE_EYE]\n";
  11. System.out.println(writer);
  12. assertEquals(expectedYaml, writer.toString());
  13. }

上面的代码产生以下输出(请注意,使用LinkedHashMap的实例将保留输出数据的顺序):

自定义Java对象

我们还可以选择将自定义Java类型转储到输出流中

  1. @Test
  2. public void whenDumpACustomType_thenGenerateCorrectYAML() {
  3. Customer customer = new Customer();
  4. customer.setAge(45);
  5. customer.setFirstName("Greg");
  6. customer.setLastName("McDowell");
  7. Yaml yaml = new Yaml();
  8. StringWriter writer = new StringWriter();
  9. yaml.dump(customer, writer);
  10. String expectedYaml = "!!com.devops.autocicdstore.yaml.Customer {age: 45, contactDetails: null, firstName: Greg,\n homeAddress: null, lastName: McDowell}\n";
  11. System.out.println(writer);
  12. assertEquals(expectedYaml, writer.toString());
  13. }

 

 

生成内容会包含!!com.devops.autocicdstore.yaml.Customer,为了避免在输出文件中使用标签名,我们可以使用库提供的  dumpAs()方法。

因此,在上面的代码中,我们可以进行以下调整以删除标记:

yaml.dumpAs(customer, Tag.MAP, null);
String ccustomerStr = yaml.dumpAs(customer, Tag.MAP, null);
System.out.println(ccustomerStr);

 

 本文说明了SnakeYAML库解析和序列化YAML文档。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/449162
推荐阅读
相关标签
  

闽ICP备14008679号