如何将 YAML 属性 序列化为 Map
How to serialize a YAML property into a Map
我想使用 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
class 实例,但我希望 args
被序列化为一个 java.util.Map
这是我的主class:
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.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 的新手,我在谷歌上搜索了一个没有成功的解决方案。
我正在回答自己,因为我意识到这是一个愚蠢的问题。
删除标记前的连字符就足够了 arg<n>
。 IE。 - arg0: value0
之类的行变成了 arg0: value0
,而且效果很好。
对不起,如果我浪费了别人的时间。
我想使用 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
class 实例,但我希望 args
被序列化为一个 java.util.Map
这是我的主class:
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.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 的新手,我在谷歌上搜索了一个没有成功的解决方案。
我正在回答自己,因为我意识到这是一个愚蠢的问题。
删除标记前的连字符就足够了 arg<n>
。 IE。 - arg0: value0
之类的行变成了 arg0: value0
,而且效果很好。
对不起,如果我浪费了别人的时间。