Yaml 配置文件解析Map-SnakeYAML

本文介绍了如何在Java中使用SnakeYAML库读取YML配置文件并将其转换为Map对象,详细阐述了配置文件的存储方式,并提供了一段测试代码展示解析过程。

 

  1. 如何使用SnakeYAML来读取YAML(YML为其简写)配置文件;
  2. 读取后,装载成Map,而Map如何存储的配置文件的数据。

Snake POM引入

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

读取YAML配置文件

InputStream in = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_RESOURCES);
Yaml yaml = new Yaml();
Map<String, Object> load = yaml.loadAs(in, Map.class);

Map是如何存储的配置文件数据

以下面的.yml配置文件为例:

log:
  location: /home/iwu/runningstat
  cache:
    path: /home/iwu/runningstat/temp
  local: false
appid: 123
server:
  port: 2020
app:
  testList:
    -
      name: wangchao
      age: 23
    -
      name: sff
      age: 20

使用SnakeYAML进行装载,实例化成Map对象(确切的说,应该是Map的子类,LinkedHashMap)。值得注意的是,要装载如此复杂的配置文件的,必须使用Map的嵌套,即:Map嵌套Map。

先来一段测试代码:

    private void printYmalMap(String prefixKey, Map<String, Object> map) {
        String fullKey = "";
        for (String key : map.keySet()) {
            fullKey = prefixKey == null || "".equals(prefixKey) ? key : prefixKey + "." + key;
            Object value = map.get(key);
            if (value instanceof Map) {
                //System.out.println("there is a map  " + prefixKey + "." + key);
                printYmalMap(fullKey, (Map) value);
            } else if (value instanceof List) {
                System.out.println("there is a list  " + fullKey);
            } else {
                System.out.println("there is a  "+value.getClass().getName() +"...   "+ fullKey);
            }
        }
    }

看看Snake解析上面yml文件打印结果

分析完成,看看完整的yaml加载 代码。这里我们将基本类型转换为了String类型。如有需要我们可以进一步进行处理。

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.*;

public class YamlResourcesProcessor{
    private static final String DEFAULT_RESOURCES = "application.yml";
    /**
     * ymal 文件中数据解析缓存
     */
    private static volatile Map<String, String> cache = Collections.synchronizedMap(new HashMap<>());

    public YamlResourcesProcessor() {
        load();
    }

    private void load() {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_RESOURCES);
        Yaml yaml = new Yaml();
        Map<String, Object> load = yaml.loadAs(in, Map.class);
        printYmalMap(null, load);
        cacheYmal(null, load);
    }

    private void cacheYmal(String prefixKey, Map<String, Object> map) {
        String fullKey = "";
        for (String key : map.keySet()) {
            Object value = map.get(key);
            fullKey = prefixKey == null || "".equals(prefixKey) ? key : prefixKey + "." + key;
            if (value instanceof Map) {
                cacheYmal(fullKey, (Map) value);
            } else if (value instanceof List) {
                // Ymal转Map 暂时不处理List
                cache.put(fullKey,  String.valueOf(value));
                //continue;
            } else {
                cache.put(fullKey,  String.valueOf(value));
            }
        }
    }

    private void printYmalMap(String prefixKey, Map<String, Object> map) {
        String fullKey = "";
        for (String key : map.keySet()) {
            fullKey = prefixKey == null || "".equals(prefixKey) ? key : prefixKey + "." + key;
            Object value = map.get(key);
            if (value instanceof Map) {
                //System.out.println("there is a map  " + prefixKey + "." + key);
                printYmalMap(fullKey, (Map) value);
            } else if (value instanceof List) {
                System.out.println("there is a list  " + fullKey);
            } else {
                System.out.println("there is a  "+value.getClass().getName() +"...   "+ fullKey);
            }
        }
    }

    public Map<String, String> getResources() {
        return cache;
    }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仰望星空@脚踏实地

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值