53564

How to load a java.util.Set with snakeYAML

Question:

I try to load the following yaml sequence :

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

which i tried to load in the following bean :

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

I got this error which is due to the fact that snakeYaml load my sequence in a java.util.List instead of java.util.Set.

Is it a way to force snakeYAML to load a sequence in a 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)

Answer1:

According to the yaml <a href="http://yaml.org/type/set.html" rel="nofollow">specification</a> the set syntax is the following one :

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

Answer2:

You force SnakeYAML to use List instead of Set. Sets have different representation in YAML: <a href="http://yaml.org/type/set.html" rel="nofollow">http://yaml.org/type/set.html</a>

You can change the YAML document or instruct SnakeYAML to use sets instead of lists. You may need to look <a href="http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyaml/issues/issue73/SetAsSequenceTest.java" rel="nofollow">in tests</a>

Recommend

  • C# how to read index variables linked to the class in a list
  • How to get and validate application/json data in Laravel?
  • Wpf list async update
  • Extracting Names and Email address from String with regex
  • Good algorithm/technique to find overlapping values from objects' properties?
  • Rspec doesn't reload changed password attribute - why?
  • Perl regular expressions in the stringr package
  • Very Basic JS Coding, and SoundManager2 or not?
  • Hoisting and variable scope
  • How to sort an array considering localization?
  • SQL: Query for a group that contains an exact set of users
  • Find 4 minimal values in 4 __m256d registers
  • Convert date string value from U.S. to native Date with current locale U.S.?
  • Init child with Parent instance
  • How to apply async task into this
  • Firefox extension testing and developing - I'm confused
  • goJS dropdown remove items
  • Separate ID and Class for JS and CSS
  • Feature detection of foreignObject in SVG
  • XSD with multi occurrences unordered
  • Any nice way to generate a timeline view of commits from subversion?
  • Sybase Error Implicit Conversion from datatype 'VARCHAR' to 'INT' not allowed
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • drawing random circles, storing their coorindates in an array
  • How to retrieve information from antrun back to maven?
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Do query loads all the data in memory
  • aapt.exe'' finished with non-zero exit value 1
  • Salesforce Different WSDL files and when to use
  • AndEngine Applying Transparancy to AndEngine View
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Jetty 9 HashLoginService
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • Read a local file using javascript
  • ImageMagick, replace semi-transparent white with opaque white
  • Cannot connect to cassandra from Spark
  • Cross-Platform Protobuf Serialization
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How do I use LINQ to get all the Items that have a particular SubItem?