包含snakeyaml的YAML文件

Bla*_*zes 11 yaml snakeyaml

我希望YAML文件包含一个include,类似于这个问题,但是使用Snakeyaml: 我如何在另一个文件中包含一个YAML文件?

例如:

%YAML 1.2
---
!include "load.yml"

!include "load2.yml"
Run Code Online (Sandbox Code Playgroud)

我遇到了很多麻烦.我定义了构造函数,我可以导入一个文档,但不能导入两个文档.我得到的错误是:

Exception in thread "main" expected '<document start>', but found Tag
 in 'reader', line 5, column 1:
    !include "load2.yml"
    ^
Run Code Online (Sandbox Code Playgroud)

有一个包含,Snakeyaml很高兴它找到了一个EOF并处理导入.有两个,它不开心(上图).

我的java源代码是:

package yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.AbstractConstruct;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.Tag;

public class Main {
    final static Constructor constructor = new MyConstructor();

    private static class ImportConstruct extends AbstractConstruct {
        @Override
        public Object construct(Node node) {
            if (!(node instanceof ScalarNode)) {
                throw new IllegalArgumentException("Non-scalar !import: " + node.toString());
            }

            final ScalarNode scalarNode = (ScalarNode)node;
            final String value = scalarNode.getValue();

            File file = new File("src/imports/" + value);
            if (!file.exists()) {
                return null;
            }

            try {
                final InputStream input = new FileInputStream(new File("src/imports/" + value));
                final Yaml yaml = new Yaml(constructor);
                return yaml.loadAll(input);
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
            return null;
        }
    }

    private static class MyConstructor extends Constructor {
        public MyConstructor() {
            yamlConstructors.put(new Tag("!include"), new ImportConstruct());
        }
    }

    public static void main(String[] args) {
        try {
            final InputStream input = new FileInputStream(new File("src/imports/example.yml"));
            final Yaml yaml = new Yaml(constructor);
            Object object = yaml.load(input);
            System.out.println("Loaded");

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        finally {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,有没有人和Snakeyaml做过类似的事情?关于我可能做错什么的任何想法?

Jas*_*n S 6

我看到两个问题:

final InputStream input = new FileInputStream(new File("src/imports/" + value));
            final Yaml yaml = new Yaml(constructor);
            return yaml.loadAll(input);
Run Code Online (Sandbox Code Playgroud)

你应该使用yaml.load(input),而不是yaml.loadAll(input)。该loadAll()方法返回多个对象,但该construct()方法期望返回单个对象。

另一个问题是,您可能对YAML 处理管道的工作方式有一些不一致的期望:

如果您认为您的!include工作方式类似于 C 语言,其中预处理器坚持包含文件的内容,那么实现它的方法是在演示阶段(解析)或序列化阶段(组合)中处理它。但是你已经在Representation阶段(构造)实现了它,所以!include返回一个对象,并且你的YAML文件的结构必须与此一致。

假设您有以下文件:

test1a.yaml

activity: "herding cats"
Run Code Online (Sandbox Code Playgroud)

test1b.yaml

33
Run Code Online (Sandbox Code Playgroud)

test1.yaml

favorites: !include test1a.yaml
age:       !include test1b.yaml
Run Code Online (Sandbox Code Playgroud)

这可以正常工作,并且相当于

favorites:
  activity: "herding cats"
age: 33
Run Code Online (Sandbox Code Playgroud)

但以下文件不起作用:

!include test1a.yaml
!include test1b.yaml
Run Code Online (Sandbox Code Playgroud)

因为没有什么可说的如何在更大的层次结构中组合这两个值。如果你想要一个数组,你需要这样做:

- !include test1a.yaml
- !include test1b.yaml
Run Code Online (Sandbox Code Playgroud)

或者,再次在早期阶段处理此自定义逻辑,例如解析或组合。

或者,您需要告诉 YAML 库您正在启动第二个文档(这就是错误所抱怨的:),expected '<document start>'因为 YAML 支持单个 .yaml 文件中的多个“文档”(顶级值)。