YAML入门

来源:互联网 发布:lg25um58p分屏软件 编辑:程序博客网 时间:2024/05/16 11:41

一、YAML是什么?

YAML是YAML Ain't Markup Language递归缩写,是YAML不是标记语言的意思,读音“yamel”(或者“雅梅尔”)。YAML是便于人阅读基于unicode编码的各种语言的序列号标准。它的用途广泛,用于配置文件,日志文件,跨语言数据共享,对象持久化,复杂的数据结构。


二、yaml有什么特性,为什么使用yaml?

1、易于阅读;使用缩进,冒号(:),破折号(-)等人易于理解方式显示复杂的数据结构。

2、YAML数据在语言之间可以移植的。

3、符合敏捷语言的数据结构。

4、支持一次性操作。

5、具有表现力和可扩展性。

6、易于实现和使用

三、YAML与JSON

形同点:

1、YAML和JSON都是便于人阅读的数据交换格式。

不同点:

1、JSON设计主要在于简单和通用。YAML设计主要在于易于阅读和支持任何复杂的数据结构。

2、YAML可以视为JSON的超集,提供更易于阅读和复杂的信息模型。

3、每个JSON文件都是一个YAML文件,从JSON到YAML迁移是很容易的事情。

四、YAML与XML

YAML主要是数据序列号语言。XXML设计是对SGML的向后兼容。XML有设计约束,而YAML没有。


五、YAML示例

[java] view plain copy
  1. 1. Invoice  
  2.   
  3. --- !<tag:clarkevans.com,2002:invoice>  
  4. invoice: 34843  
  5. date   : 2001-01-23  
  6. bill-to: &id001  
  7.     given  : Chris  
  8.     family : Dumars  
  9.     address:  
  10.         lines: |  
  11.             458 Walkman Dr.  
  12.             Suite #292  
  13.         city    : Royal Oak  
  14.         state   : MI  
  15.         postal  : 48046  
  16. ship-to: *id001  
  17. product:  
  18.     - sku         : BL394D  
  19.       quantity    : 4  
  20.       description : Basketball  
  21.       price       : 450.00  
  22.     - sku         : BL4438H  
  23.       quantity    : 1  
  24.       description : Super Hoop  
  25.       price       : 2392.00  
  26. tax  : 251.42  
  27. total: 4443.52  
  28. comments:  
  29.     Late afternoon is best.  
  30.     Backup contact is Nancy  
  31.     Billsmer @ 338-433  

[java] view plain copy
  1. 2. Log File  
  2.   
  3. ---  
  4. Time: 2001-11-23 15:01:42 -5  
  5. User: ed  
  6. Warning:  
  7.   This is an error message  
  8.   for the log file  
  9. ---  
  10. Time: 2001-11-23 15:02:31 -5  
  11. User: ed  
  12. Warning:  
  13.   A slightly different error  
  14.   message.  
  15. ---  
  16. Date: 2001-11-23 15:03:17 -5  
  17. User: ed  
  18. Fatal:  
  19.   Unknown variable "bar"  
  20. Stack:  
  21.   - file: TopClass.py  
  22.     line: 23  
  23.     code: |  
  24.       x = MoreObject("345\n")  
  25.   - file: MoreClass.py  
  26.     line: 58  
  27.     code: |-  
  28.       foo = bar  

六、JAVA中如何使用YAML?

1、yamlbeans

使用yamlbeans操作YAML,maven依赖:

[java] view plain copy
  1. <span style="white-space:pre">    </span><dependency>  
  2.             <groupId>com.esotericsoftware.yamlbeans</groupId>  
  3.             <artifactId>yamlbeans</artifactId>  
  4.             <version>1.08</version>  
  5.         </dependency>  
[java] view plain copy
  1. java代码:  
[java] view plain copy
  1. public class Yamlbeans {  
  2.     public static void main(String[] args) throws  Exception{  
  3.         Contact contact = new Contact();  
  4.         contact.name = "Nathan Sweet";  
  5.         contact.age = 28;  
  6.         YamlWriter writer = new YamlWriter(new FileWriter("/Contact1.yaml"));  
  7.         writer.write(contact);  
  8.         writer.close();  
  9.   
  10.         YamlReader reader = new YamlReader(new FileReader(" /Contact.yaml"));  
  11.         Map<String,Contact> contact = reader.read(Map.class);  
  12.         System.out.println(contact.size());  
  13.         YamlWriter writer = new YamlWriter(new FileWriter(" /Contacts2.yaml"));  
  14.         writer.write(contact);  
  15.         writer.close();  
  16.     }  
  17. }  

Contact.yaml:

[java] view plain copy
  1. friends:  
  2.   - !com.example.Contact  
  3.     name: Bob  
  4.     age: 29  
  5.     phoneNumbers:  
  6.         - !com.example.Phone  
  7.           name: Home  
  8.           number: 206-555-1234  
  9.         - !com.example.Phone  
  10.           name: Work  
  11.           number: 206-555-5678  
  12.   - !com.example.Contact  
  13.     name: Mike  
  14.     age: 31  
  15.     phoneNumbers:  
  16.         - !com.example.Phone  
  17.           number: 206-555-4321  
  18. enemies:  
  19.   - !com.example.Contact  
  20.     name: Bill  
  21.     phoneNumbers:  
  22.         - !com.example.Phone  
  23.           name: Cell  
  24.           number: 206-555-1234  

2、使用SnakeYaml

使用SnakeYaml操作yaml,maven依赖:

[java] view plain copy
  1. <span style="white-space:pre">    </span><dependency>  
  2.             <groupId>org.yaml</groupId>  
  3.             <artifactId>snakeyaml</artifactId>  
  4.             <version>1.11</version>  
  5.         </dependency>  
java代码:

[java] view plain copy
  1. public class SnakeYaml {  
  2.     public static void main(String[] args){  
  3.         DumperOptions options = new DumperOptions();  
  4.         options.setWidth(1000);  
  5.         options.setIndent(5);  
  6.         Yaml yaml = new Yaml();  
  7.         try {  
  8.             Map<String,Contact> contactMap = yaml.loadAs(new FileInputStream("/Contacts.yaml"),Map.class);  
  9.             System.out.println(contactMap);  
  10.             String result = yaml.dump(contactMap);  
  11.         } catch (FileNotFoundException e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.     }  
  15. }  
原创粉丝点击