JAVA基于SnakeYAML实现解析与序列化YAML
这篇文章主要介绍了JAVA基于SnakeYAML实现解析与序列化YAML,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.概述
本文,我们将学习如何使用SnakeYAML库将
YAML文档转换为Java对象,以及JAVA对象如何序列化为YAML文档。
2.项目设置
要在项目中使用SnakeYAML,需要添加Maven依赖项(可在此处找到最新版本):
org.yaml snakeyaml 1.25
3.入口点
该YAML类是API的入口点:
Yamlyaml=newYaml()
由于实现不是线程安全的,因此不同的线程必须具有自己的Yaml实例。
4.加载YAML文档
SnakeYAML支持从String或InputStream加载文档,我们从定义一个简单的YAML文档开始,然后将文件命名为customer.yaml:
firstName:"John" lastName:"Doe" age:20
4.1基本用法
现在,我们将使用Yaml类来解析上述YAML文档:
Yamlyaml=newYaml(); InputStreaminputStream=this.getClass() .getClassLoader() .getResourceAsStream("customer.yaml"); Mapobj=yaml.load(inputStream); System.out.println(obj);
上面的代码生成以下输出:
{firstName=John,lastName=Doe,age=20}
默认情况下,load()方法返回一个Map对象。查询Map对象时,我们需要事先知道属性键的名称,否则容易出错。更好的办法是自定义类型。
4.2自定义类型解析
SnakeYAML提供了一种将文档解析为自定义类型的方法
让我们定义一个Customer类,然后尝试再次加载该文档:
publicclassCustomer{ privateStringfirstName; privateStringlastName; privateintage; //gettersandsetters }
现在我么来加载:
Yamlyaml=newYaml(); InputStreaminputStream=this.getClass() .getClassLoader() .getResourceAsStream("customer.yaml"); Customercustomer=yaml.load(inputStream);
还有一种方法是使用Constructor:
Yamlyaml=newYaml(newConstructor(Customer.class));
4.3隐式类型
如果没有为给定属性定义类型,则库会自动将值转换为隐式type。
例如:
1.0->Float 42->Integer 2009-03-30->Date
让我们使用一个TestCase来测试这种隐式类型转换:
@Test publicvoidwhenLoadYAML_thenLoadCorrectImplicitTypes(){ Yamlyaml=newYaml(); Map
4.4嵌套对象
SnakeYAML支持嵌套的复杂类型。
让我们向“customer.yaml”添加“联系方式”和“地址”详细信息,并将新文件另存为customer_with_contact_details_and_address.yaml.。
现在,我们将分析新的YAML文档:
firstName:"John" lastName:"Doe" age:31 contactDetails: -type:"mobile" number:123456789 -type:"landline" number:456786868 homeAddress: line:"Xyz,DEFStreet" city:"CityY" state:"StateY" zip:345657
我们来更新java类:
publicclassCustomer{ privateStringfirstName; privateStringlastName; privateintage; privateListcontactDetails; privateAddresshomeAddress; //gettersandsetters } publicclassContact{ privateStringtype; privateintnumber; //gettersandsetters } publicclassAddress{ privateStringline; privateStringcity; privateStringstate; privateIntegerzip; //gettersandsetters }
现在,我们来测试下Yaml#load():
@Test publicvoid whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObjectWithNestedObjects(){ Yamlyaml=newYaml(newConstructor(Customer.class)); InputStreaminputStream=this.getClass() .getClassLoader() .getResourceAsStream("yaml/customer_with_contact_details_and_address.yaml"); Customercustomer=yaml.load(inputStream); 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,DEFStreet",customer.getHomeAddress() .getLine()); }
4.5类型安全的集合
当给定Java类的一个或多个属性是泛型集合类时,需要通过TypeDescription来指定泛型类型,以以便可以正确解析。
让我们假设一个一个Customer拥有多个Contact:
firstName:"John" lastName:"Doe" age:31 contactDetails: -{type:"mobile",number:123456789} -{type:"landline",number:123456789}
为了能正确解析,我们可以在顶级类上为给定属性指定TypeDescription:
Constructorconstructor=newConstructor(Customer.class); TypeDescriptioncustomTypeDescription=newTypeDescription(Customer.class); customTypeDescription.addPropertyParameters("contactDetails",Contact.class); constructor.addTypeDescription(customTypeDescription); Yamlyaml=newYaml(constructor);
4.6载入多个文件
在某些情况下,单个文件中可能有多个YAML文档,而我们想解析所有文档。所述YAML类提供了一个LOADALL()方法来完成这种类型的解析。
假设下面的内容在一个文件中:
--- firstName:"John" lastName:"Doe" age:20 --- firstName:"Jack" lastName:"Jones" age:25
我们可以使用loadAll()方法解析以上内容,如以下代码示例所示:
@Test publicvoidwhenLoadMultipleYAMLDocuments_thenLoadCorrectJavaObjects(){ Yamlyaml=newYaml(newConstructor(Customer.class)); InputStreaminputStream=this.getClass() .getClassLoader() .getResourceAsStream("yaml/customers.yaml"); intcount=0; for(Objectobject:yaml.loadAll(inputStream)){ count++; assertTrue(objectinstanceofCustomer); } assertEquals(2,count); }
5.生成YAML文件
SnakeYAML支持将java对象序列化为yml。
5.1基本用法
我们将从一个将Map
@Test publicvoidwhenDumpMap_thenGenerateCorrectYAML(){ Mapdata=newLinkedHashMap (); data.put("name","SilenthandOlleander"); data.put("race","Human"); data.put("traits",newString[]{"ONE_HAND","ONE_EYE"}); Yamlyaml=newYaml(); StringWriterwriter=newStringWriter(); yaml.dump(data,writer); StringexpectedYaml="name:SilenthandOlleander\nrace:Human\ntraits:[ONE_HAND,ONE_EYE]\n"; assertEquals(expectedYaml,writer.toString()); }
上面的代码产生以下输出(请注意,使用LinkedHashMap的实例将保留输出数据的顺序):
name:SilenthandOlleander race:Human traits:[ONE_HAND,ONE_EYE]
5.2自定义Java对象
我们还可以选择将自定义Java类型转储到输出流中。
@Test publicvoidwhenDumpACustomType_thenGenerateCorrectYAML(){ Customercustomer=newCustomer(); customer.setAge(45); customer.setFirstName("Greg"); customer.setLastName("McDowell"); Yamlyaml=newYaml(); StringWriterwriter=newStringWriter(); yaml.dump(customer,writer); StringexpectedYaml="!!com.baeldung.snakeyaml.Customer{age:45,contactDetails:null,firstName:Greg,\nhomeAddress:null,lastName:McDowell}\n"; assertEquals(expectedYaml,writer.toString()); }
生成内容会包含!!com.baeldung.snakeyaml.Customer,为了避免在输出文件中使用标签名,我们可以使用库提供的dumpAs()方法。
因此,在上面的代码中,我们可以进行以下调整以删除标记:
yaml.dumpAs(customer,Tag.MAP,null);
六结语
本文说明了SnakeYAML库解析和序列化YAML文档。
所有示例都可以在GitHub项目中找到。
英文原文:ParsingYAMLwithSnakeYAML
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。