如何在SnakeYaml中解析YAML文件的一部分 [英] How to parse part of a YAML file in SnakeYaml

查看:327
本文介绍了如何在SnakeYaml中解析YAML文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是YAML的新手,并且已经解析了一个YAML配置文件,如下所示:

I am new to YAML and have parse a YAML config file that looks like:

applications:
  authentication:
    service-version: 2.0
    service-url: https://myapp.corp/auth
    app-env: DEV
    timeout-in-ms: 5000
    enable-log: true

  service1:
    enable-log: true
    auth-required: true
    app-env: DEV
    timeout-in-ms: 5000
    service-url: https://myapp.corp/service1
    service-name: SomeService1
    service-version: 1.1
    service-namespace: http://myapp.corp/ns/service1

  service2:
    enable-log: true
    auth-required: true
    app-env: DEV
    timeout-in-ms: 5000
    service-url: https://myapp.corp/service2
    service-name: SomeService2
    service-version: 2.0
    service-namespace: http://myapp.corp/ns/service2

我必须解析为以下Map结构

+==================================+
| Key              |               |
+==================================+
| authentication   | AuthConfig    |
+----------------------------------+
| service1         | ServiceConfig |
+----------------------------------+
| service2         | ServiceConfig |
+----------------------------------+

AuthConfigServiceConfig是我们系统中的自定义对象.

AuthConfig and ServiceConfig are the custom objects in our system.

有人可以提供一些提示怎么做吗?

Can someone provide some hints how to do it?

推荐答案

有一个名为 Jackson 处理 YAML (以及JSON,CSV和XML)之间的映射和Java对象.您将遇到的大多数示例都是针对JSON的,但是YAML链接显示切换是直接的.一切都通过ObjectMapper:

There is a package for Java called Jackson that handles mapping between YAML (and JSON, and CSV, and XML) and Java objects. Most examples you will come across are for JSON, but the YAML link shows that switching is straight-forward. Everything goes through an ObjectMapper:

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

然后可以通过反射来反序列化对象:

That can then be used to deserialize your object via reflection:

ApplicationCatalog catalog = mapper.readValue(yamlSource, ApplicationCatalog.class);

您可以像这样设置您的课程(为便于示例,我公开了所有内容):

You would set up your classes something like this (I've made everything public for ease of example):

class ApplicationCatalog {
  public AuthConfig authentication;
  public ServiceConfig service1;
  public ServiceConfig service2;
}

class AuthConfig {
  @JsonProperty("service-version")
  public String serviceVersion;
  @JsonProperty("service-url")
  public String serviceUrl;
  @JsonProperty("app-env")
  public String appEnv;
  @JsonProperty("timeout-in-ms")
  public int timeoutInMs;
  @JsonProperty("enable-log")
  public boolean enableLog;
}

class ServiceConfig {
  ...
}

注意JsonProperty 注释,该注释将Java字段重命名为YAML字段.我发现这是用Java处理JSON和YAML的最方便的方法.我还必须对大型对象使用流API .

Notice the JsonProperty annotation which is renaming your Java field to YAML field. I find this the most convenient way of dealing with JSON and YAML in Java. I've also had to use the streaming API for really large objects.

这篇关于如何在SnakeYaml中解析YAML文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆