Hi,
I am unable to load an attribute whose class is a specialization of a
map,
I am getting the following error:
Caused by: org.yaml.snakeyaml.error.YAMLException: Cannot create
property=moreFeatures for JavaBean=DerivedMapTest$MyObject@13a317a;
argument type mismatch
at org.yaml.snakeyaml.constructor.Constructor
$ConstructMapping.constructJavaBean2ndStep(Constructor.java:299)
at org.yaml.snakeyaml.constructor.Constructor
$ConstructMapping.construct(Constructor.java:189)
at org.yaml.snakeyaml.constructor.Constructor
$ConstructYamlObject.construct(Constructor.java:331)
I tried to use a custom constructor, but seems that the result is the
same.
Looks like SnakeYaml whenever recognises a map, creates a linked hash
map and
then tries to set the attribute property (that obviously fails). Any
workaround
suggestion?
Here is a simple complete snippet to test. MyObject has two map
attributes:
features (works fine) and moreFreatures, that causes the problem
because
it is of Features type that extends LinkedHashmap:
import java.util.LinkedHashMap;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
public class DerivedMapTest {
public static class Features extends LinkedHashMap<String, Object>
{
}
public static class MyObject {
private String name;
private Map<String, Object> features = new LinkedHashMap<String,
Object>();
private Features moreFeatures = new Features();
public Map<String, Object> getFeatures() {
return features;
}
public void setFeatures(Map<String, Object> features) {
this.features = features;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Features getMoreFeatures() {
return moreFeatures;
}
public void setMoreFeatures(Features moreFeatures) {
this.moreFeatures = moreFeatures;
}
}
public static void main(String[] args) {
MyObject o = new MyObject();
o.setName("Mickey");
o.getFeatures().put("Address", "Disney");
o.getMoreFeatures().put("Address", "Disney");
Yaml yaml = new Yaml();
String asYaml = yaml.dump(o);
System.out.println(asYaml);
MyObject o2 = (MyObject) yaml.load(asYaml);
}
}