如何将YAML属性序列化为Map

时间:2019-05-07 11:53:13

标签: java snakeyaml

我想使用SnakeYAML 1.24读取这样的YAML配置文件:

monitor: monitor 0
folders:
  - path/folder1
  - path/folder2
filters:
  includes:
    - first filter 0
    - second filter 0
  excludes:
    - first exclusion 0
    - second exclusion 0

includeSubdirs: true
processors:
  - processor: processor#0
    args:
      - arg0: value0
      - arg1: value1
  - processor: processor#1
    args:
      - arg2: value2

---

monitor: myMonitor 1
folders:
  - path/folder3
  - path/folder4
filters:
  includes:
    - first filter 1
  excludes:
    - first exclusion 1
    - second exclusion 1
    - third exclusion 1

includeSubdirs: true
processors:
  - processor: processor#2
    args:
      - arg3: value3
      - arg4: value4
      - arg5: value5
  - processor: processor#3
    args:
      - arg6: value6
      - arg7: value7

每个文档都被序列化为Monitor类实例,但是我希望args被序列化为java.util.Map

这是我的主要课程:

public class WWatchdog {
    public static void main(String[] args) {
        Constructor constructor = new Constructor(MonitorSettings.class);
        Yaml settings = new Yaml(constructor);

        try {
            for (Object object : settings.loadAll(new FileReader("wwatchdog-core/monitors.yml"))) {
                if (object instanceof MonitorSettings) {
                    MonitorSettings monitorSettings = (MonitorSettings)object;
                    System.out.println(settings.dump(monitorSettings));
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

和POJO

public class MonitorSettings {
    private String monitor;
    private List<String> folders;
    private MonitorFilterSettings filters;
    private boolean includeSubdirs;
    private List<ProcessorSettings> processors;

// Getters and setters
}

public class MonitorFilterSettings {
    private List<String> includes;
    private List<String> excludes;

// Getters and setters
}

public class ProcessorSettings {
    private String processor;
    private Map<String, String> args;

// Getters and setters
}

我遇到以下错误:

Exception in thread "main" Cannot create property=processors for JavaBean=org.nemert.wwatchdog.Monitor@6f2b958e
 in 'reader', line 1, column 1:
    monitor: myMonitor
    ^
Cannot create property=args for JavaBean=org.nemert.wwatchdog.ProcessorConfig@3d012ddd
 in 'reader', line 15, column 5:
      - processor: my processor
        ^
No suitable constructor with 2 arguments found for interface java.util.Map
 in 'reader', line 17, column 7:
          - arg0: value0
          ^

 in 'reader', line 15, column 3:
      - processor: my processor
      ^

    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:268)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:149)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:309)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:215)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:205)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:164)
    at org.yaml.snakeyaml.constructor.BaseConstructor.getData(BaseConstructor.java:129)
    at org.yaml.snakeyaml.Yaml$1.next(Yaml.java:548)
    at org.nemert.wwatchdog.WWatchdog.main(WWatchdog.java:17)
Caused by: Cannot create property=args for JavaBean=org.nemert.wwatchdog.ProcessorConfig@3d012ddd
 in 'reader', line 15, column 5:
      - processor: my processor
        ^
No suitable constructor with 2 arguments found for interface java.util.Map
 in 'reader', line 17, column 7:
          - arg0: value0
          ^

    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:268)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:149)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:215)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:205)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstructor.java:376)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor.java:360)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructSequence.construct(Constructor.java:516)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:215)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:205)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.newInstance(Constructor.java:283)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:245)
    ... 8 more
Caused by: org.yaml.snakeyaml.error.YAMLException: No suitable constructor with 2 arguments found for interface java.util.Map
    at org.yaml.snakeyaml.constructor.Constructor$ConstructSequence.construct(Constructor.java:582)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:215)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:205)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:245)
    ... 18 more

可以做我想做的吗?我是SnakeYAML的新手,但我一直在Google寻找解决方案,但没有成功。

1 个答案:

答案 0 :(得分:0)

我正在回答自己,因为我已经意识到这是一个愚蠢的问题。

删除标记arg<n>前的连字符就足够了。即像- arg0: value0这样的行变成arg0: value0,它就像一个超级按钮。

对不起,如果我浪费了别人的时间。