JAVA基于SnakeYAML实现解析与序列化YAML

学习SnakeYAML

第一个例子:基本用法

1、创建一个不适用框架的maven工程,引入坐标

        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.23</version>
        </dependency>

2、定义一个简单的yaml文档:customer.yaml

firstName: "John"
lastName: "Doe"
age: 20

3、读取

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Map;

public class SnakeYaml {
    public static void main(String[] args) {
        Yaml yaml = new Yaml();//该YAML类是API的入口点:由于实现不是线程安全的,因此不同的线程必须具有自己的Yaml实例

        // 加载文档
       // InputStream input = SnakeYaml.class.getResourceAsStream("customer.yaml");
        InputStream input = SnakeYaml.class.getClassLoader().getResourceAsStream("customer.yaml");
        /*
        默认情况下,load()方法返回一个Map对象。
        查询Map对象时,我们需要事先知道属性键的名称,否则容易出错。更好的办法是自定义类型。
         */
        Map<String, Object> obj = yaml.load(input);
        System.out.println(obj);
    }
}

4、输出
在这里插入图片描述
5、程序目录
在这里插入图片描述

第二个例子:使用自定义类型解析

在第一个例子上修改:
1、定义一个Customer类

public class Customer {

    private String firstName;
    private String lastName;
    private int age;

    // getters and setters 、toString
}

2、修改SnakeYaml

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;


public class SnakeYaml {
    public static void main(String[] args) {
        Yaml yaml = new Yaml();
        InputStream inputStream = SnakeYaml.class
                .getClassLoader()
                .getResourceAsStream("customer.yaml");
        Customer c = yaml.loadAs(inputStream, Customer.class);
        System.out.println(c);
    }
}

3、运行结果
在这里插入图片描述
4、目录
在这里插入图片描述

第三个例子: 隐式转换

1、引入坐标:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

2、测试

import org.junit.Test;
import org.yaml.snakeyaml.Yaml;


import java.util.Map;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;


public class testC {
    @Test
    public void whenLoadYAML_thenLoadCorrectImplicitTypes() {
        Yaml yaml = new Yaml();
        Map<Object, Object> document = yaml.load("3.0: 2018-07-22");
        // 3.0---> float
        // 2018-07-22---> data
        assertNotNull(document);
        assertEquals(1, document.size());
        assertTrue(document.containsKey(3.0d));

        document = yaml.load("42: aa");
        assertTrue(document.containsKey(42));
    }
}

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

第三个例子: 一维数组

在第一个例子上修改

1、修改customer.yaml

firstName: "frist"
lastName: "aa"
age:
  - 20
  - 30
  - 40

或者

firstName: "frist"
lastName: "aa"
age: [20, 30, 40]

2、修改Customer.java

public class Customer {

    private String firstName;
    private String lastName;
    private int[] age;
    ******

3、读取

        Yaml yaml = new Yaml();
        InputStream inputStream = SnakeYaml.class
                .getClassLoader()
                .getResourceAsStream("customer.yaml");
        Customer c = yaml.loadAs(inputStream, Customer.class);
        System.out.println(c.toString());

第三个例子: 二维数组

在第一个例子上修改

1、修改customer.yaml

firstName: "frist"
lastName: "aa"
age:
  - [20, 30, 40]
  - [20, 30, 40]

2、修改bean

public class Customer {

    private String firstName;
    private String lastName;
    private int[][] age;

3、读取

        Yaml yaml = new Yaml();
        InputStream inputStream = SnakeYaml.class
                .getClassLoader()
                .getResourceAsStream("customer.yaml");
        Customer c = yaml.loadAs(inputStream, Customer.class);
        System.out.println(c.toString());

第四个例子:yaml如何写入

    @Test
    public void testDumpWriter() throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("name", "Silenthand Olleander");
        data.put("race", "Human");
        data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
        Yaml yaml = new Yaml();
        FileWriter writer = new FileWriter(System.getProperty("user.dir") + "/src/main/resources/Chessboard.yaml");
        yaml.dump(data, writer);
        System.out.println(writer.toString());
    }

XXXXX

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值