YAML - YAML An't a Markup Lanague

P.S. YAML was originally was side to mean Yet Another Markup Language, referencing its purpose as a markup lanaguage with the yet another construct; but it was then repurposed as YAML Anit Markup Language (仍是标记语言,只是为了强调不同,YAML 是以数据设计为重点,XML以标记为重点), a recursive acronym, to distinguish its purpose as data-orinted, rather than document markup.

为什么用YAML -> 对比XML的几点优势

可读性高 && 和脚本语言的交互性好 && 使用实现语言的数据类型 && 有一个一致的信息模型 && 易于实现 && 可以基于流来处理 && 表达能力强,扩展性好

YAML 语法规则 -> Structure 用空格表示 && Sequence里的项用"-"表示 && MAP 里的键值对用 ":"分隔

#John.yaml

name: John Smith
age: 37
spouse:
name: Jane Smith
age: 25
children:
- name: Jimmy Smith
age: 15
- name: Jenny Smith
age 12

JYaml - > 基于Java 的YAML实现 (使用JAVA的数据类型)

JYaml 支持的Java数据类型

原始数据和封装类 && JavaBean 兼容对象(Structure 支持)&& Collection [List, Set] (Sequence 支持)&& Map (map 支持) && Arrays (sequence 支持) && Date

#John.yaml 的Java描述

public class Person {
private String name;
private int age;
private Person sponse;
private Person[] children;
// setXXX, getXXX方法略.

  # 添加John

    Person john = new Person();
john.setAge(37);
john.setName("John Smith");
Person sponse = new Person();
sponse.setName("Jane Smith");
sponse.setAge(25);
john.setSponse(sponse);
Person[] children = {new Person(), new Person()};
children[0].setName("Jimmy Smith");
children[0].setAge(15);
children[1].setName("Jenny Smith");
children[1].setAge(12);
john.setChildren(children); 

  #使用JYaml 将John dump出来

File dumpfile = new File("John_dump.yaml");
Yaml.dump(john, dumpfile);

# 用JYaml 把Jone_dump.yaml load 进来

Person john2 = (Person) Yaml.loadType(dumpfile, Person.class);

JYaml 对流的处理

#把同一个John 写10次

 YamlEncoder enc = new YamlEncoder(new FileOutputStream(dumpfile));
for(int i=0; i<10; i++){
john.setAge(37+i);
enc.writeObject(john);
enc.flush();
}
enc.close();

  #依次读出此10个对象

YamlDecoder dec = new YamlDecoder(new FileInputStream(dumpfile));
int age = 37;
while(true){
try{
john = (Person) dec.readObject();
assertEquals(age, john.getAge());
age++;
}catch(EOFException eofe){
break;
}
}

YAML 的适用范围

a. 由于解析成本低,YAML比较适合在脚本语言中使用, 现有的语言有: Ruby, Java, Perl, Python, PHP, JavaScript & Java

b. YAML 比较适合做序列化, 因为它支持对宿主语言的直接转化

c. 做配置文件

Note: 由于兼容性问题,不同语言间的数据流转不建议使用YAML

YAML 存在的意义

YAML和XML不同,没有自己的数据类型的定义,而是使用实现语言的数据类型。这一点,有可能是出奇制胜的地方,也可能是一个败笔。如果兼容性保证的不好的话,YAML数据在不同语言间流转会有问题。如果兼容性好的话,YAML就会成为不同语言间数据流通的桥梁。建议yaml.org设立兼容认证机制,每个语言的实现必须通过认证。

Reference -> https://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/index.html

最新文章

  1. bzoj 4066: 简单题
  2. malloc原理和内存碎片[转]
  3. ssh(sturts2_spring_hibernate) 框架搭建之struts2
  4. 谈谈 Google 的 Test Certified
  5. Runner站立会议07
  6. Linux内核设计第五周——扒开系统调用三层皮(下)
  7. Search API 官方文档 可以用了查看自己的app
  8. TestNG传参的几种方式
  9. iOS应用中URL地址如何重定向-b
  10. The Same Game(模拟)
  11. 1、roboguide新建工程文件
  12. svn打分支和合并操作
  13. IDEA中静态资源无法找到的原因
  14. Daily Scrumming* 2015.12.18(Day 10)
  15. javascript 类型比较方法
  16. FFmpeg简易播放器的实现-音视频同步
  17. 对Java8 stream的简单实践
  18. bzoj3143 游走
  19. yocto-sumo源码解析(九): ProcessServer.main
  20. js基础篇string&amp;&amp;array(应YX同学面试复习要求 - -)

热门文章

  1. css实现选项卡
  2. apache 基本vhost配置
  3. mysql 列类型以及属性特点
  4. React 在服务端渲染的实现
  5. Python读入CIFAR-10数据库
  6. 在vs2010中显示代码的行数
  7. H3CNE实验:配置VLAN和VLAN端口
  8. 极客圈(一)树莓派3B协同Python打造个性化天气闹钟
  9. day4作业小代码练习
  10. document.querySelectorAll() 与document.getElementTagName() 的区别