linux cpu占用率如何看
467
2022-11-16
YAML-学习笔记!
YAML学习笔记
一、YAML简介
YAML,即YAML Ain’t Markup Language的缩写,YAML 是一种简洁的非标记语言。YAML以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁易读。
二、YAML语法
1、基本规则
YAML有以下基本规则:
大小写敏感
使用缩进表示层级关系禁止使用tab缩进,只能使用空格键缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级。使用#表示注释
字符串可以不用引号标注
2、数据类型
字符串
#YAMLstr: abc
#JSON{ str:'abc'}
#YAML#包含特殊字符需要加引号str: '内容:字符串'
#JSON{ str:'内容:字符串'}
#YAML#单双引号均可,双引号不会对特殊符号转义s1: '内容\n字符串's2: "内容\n字符串"
#JSON{ s1:'内容\\n字符串', s2:'内容\n字符串'}
布尔值布尔值用true和false表示
#YAMLisTrue: trueisTrue: false
#JSON{ isTrue:true, isTrue:false}
整数数值直接以字面形式表示
#YAMLint: 10
#JSON{ int:10}
浮点数数值直接以字面形式表示
float: 1.23double: 2.34
#JSON{ float:1.23, double:2.34}
Nullnull值用~表示
#YAMLperson: ~
{ person:null}
时间时间采用ISO8601格式表示
#YAMLiso8601: 2018-05-20t10:59:43.10-05:00
#JSON{ iso8601:new Date('2018-05-20t10:59:43.10-05:00')}
日期日期采用ISO8601的格式yyyy-MM-dd表示
#YAMLdate: 2018-05-20
{ date:new Date('2018-05-20')}
注:YAML允许使用两个感叹号强制转换类型#YAMLstr1: !!str 123str2: !!str true#JSON{ str1:'123', str2:'true'}
3、数据结构
1. Map,散列表 使用:表示键值对,统一缩进的所有键值对属于一个Map
name: Johnage: 18#也可以写在一行{ name: John, age: 18}
#JSON{ 'name':'John', 'age': 18}
2、List,数组 使用-来表示数组中的一个元素
#YAML- a- b- c#也可以写在一行[a, b, c]
#JSON['a', 'b', 'c']
3、scalar,纯量 数据的最小单位,不可再分割
4、数据结构的嵌套
YAML中的数据结构可以相互嵌套,嵌套方式有如下几种:1、Map嵌套Map
#YAMLwebsites: YAML: yaml.org Ruby: ruby-lang.org Python: python.org Perl: use.perl.org
#JSON{ websites: { YAML: 'yaml.org', Ruby: 'ruby-lang.org', Python: 'python.org', Perl: 'use.perl.org' } }
2、Map嵌套List
#YAMLlanguages: - Ruby - Perl - Python - c
#JSON{ languages:[ 'Ruby', 'Perl', 'Python', 'c'] }
3、List嵌套List
#YAML- - Ruby - Perl - Python - - c - c++ - java#或者- [Ruby,Perl,Python]- [c,c++,java]
#JSON[ [ 'Ruby', 'Perl', 'Python' ], [ 'c', 'c++', 'java' ]]
4、List嵌套Map
#YAML- name: John age: 18- name: Lucy age: 16
#JSON[ { 'name':'John', 'age':18 }, { 'name':'Lucy', 'age':16 }]
三、Java对YAML文件的操作
1、SnakeYAML简介
SnakeYAML是一个完整的YAML1.1规范Processor,支持UTF-8/UTF-16,支持Java对象的序列化/反序列化,支持所有YAML定义的类型。
2、SnakeYAML依赖添加
在pom文件中加入依赖
3、SnakeYAML的使用方法
1. 建立Person类
import lombok.Data;//lombok为一种Java工具框架@Datapublic class Person { private String name; private Integer age;}
2、建立person.yml文件
# person.yml!!com.demo.Person {age: 24, name: Adam}
3、读取并解析YAML文件
//获取YAML中的单个对象@Testpublic void testLoadYaml() throws FileNotFoundException { Yaml yaml = new Yaml(); File ymlFile = new File(System.getProperty("user.dir") + "/src/main/resources/person.yml"); Person person = yaml.load(new FileInputStream(ymlFile)); System.out.println(person);}
输出结果为:
Person(name=Adam, age=24)
//获取YAML中的单个对象@Testpublic void testLoadYaml2() throws FileNotFoundException { Yaml yaml = new Yaml(); File ymlFile = new File(System.getProperty("user.dir") + "/src/main/resources/person.yml"); List
输出结果为:
[Person(name=Adam, age=24), Person(name=Jack, age=24), Person(name=Steve, age=24)]
//读取YAML文件并返回一个对应类的对象@Testpublic void testLoadYaml() throws FileNotFoundException { Yaml yaml = new Yaml(); File ymlFile = new File(System.getProperty("user.dir") + "/src/main/resources/person.yml"); Person person = yaml.loadAs(new FileInputStream(ymlFile), Person.class); System.out.println(person);}
输出结果为:
Person(name=John, age=20)
Iterable
//读取YAML文件并返回一个Iterable接口的子类@Testpublic void testLoadAllYaml() throws FileNotFoundException { Yaml yaml = new Yaml(); File ymlFile = new File(System.getProperty("user.dir") + "/src/main/resources/person.yml"); Iterable
输出结果为:
[{name=John, age=20}, {name=Steven, age=30}, {name=Jenny, age=18}]
void dump(Object data, Writer output)
//将POJO写入YAML文件@Testpublic void testDumpYaml() throws IOException { Yaml yaml = new Yaml(); File ymlFile = new File(System.getProperty("user.dir") + "/src/main/resources/person.yml"); Person person = new Person(); person.setName("Adam"); person.setAge(24); yaml.dump(person, new FileWriter(ymlFile));}
输出结果为:
!!com.liheng.demo.Person {age: 24, name: Adam}注:dump方法会将YAML文件中的数据覆盖
void dumpAll(Iterator extends Object> data, Writer output)
//通过Iterator迭代器批量写入YAML文件@Testpublic void testDumpAllYaml() throws IOException { Yaml yaml = new Yaml(); File ymlFile = new File(System.getProperty("user.dir") + "/src/main/resources/person.yml"); Iterable
输出结果为:
!!com.liheng.demo.Person {age: 20, name: John} --- !!com.liheng.demo.Person {age: 30, name: Steven} --- !!com.liheng.demo.Person {age: 18, name: Jenny}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~