如何使用snakeYAML加载java.util.Set

时间:2012-01-17 16:22:03

标签: java set yaml snakeyaml

我尝试加载以下yaml序列:

- Person(paul):
firstName: Paul
lastName: Lumbergh
children :
    - Person(bill)
    - Person(jane)

我试图在以下bean中加载:

public class Person {

private long id;
private String firstName;
private String lastName;
private Person father;
private Set<Person> children;
}

我收到此错误,原因是snakeYaml在java.util.List而不是java.util.Set中加载我的序列。

这是强制snakeYAML在java.util.Set中加载序列的一种方法吗?

org.yaml.snakeyaml.error.YAMLException: org.yaml.snakeyaml.error.YAMLException: Cannot set property='children' with value='[Person [firstName=Bill, secondName=Lumbergh], Person [firstName=Jane, secondName=Lumbergh]]' (class java.util.ArrayList) in Person [firstName=Paul, secondName=Lumbergh]
at org.yaml.snakeyaml.extensions.compactnotation.CompactConstructor$ConstructCompactObject.construct(CompactConstructor.java:163)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstructor.java:277)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor.java:248)
at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlSeq.construct(SafeConstructor.java:440)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:142)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:128)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480)
at org.yaml.snakeyaml.Yaml.load(Yaml.java:411)
at com.pearson.fixy.Fixy.loadEntities(Fixy.java:105)
at com.pearson.fixy.Fixy.load(Fixy.java:126)
at com.dvidea.TestFixy.test(TestFixy.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:2)

根据yaml specification,设置语法如下:

- Person(paul): 
  firstName: Paul 
  lastName: Lumbergh 
  children : !!set ? Person(bill) ? Person(jane) – 

答案 1 :(得分:1)

您强制SnakeYAML使用List而不是Set。集合在YAML中具有不同的表示形式:http://yaml.org/type/set.html

您可以更改YAML文档或指示SnakeYAML使用集而不是列表。 您可能需要查看in tests