load yaml file to class

145 views
Skip to first unread message

Hemal Bavishi

unread,
Apr 21, 2017, 7:39:11 AM4/21/17
to Xtend Programming Language
Hello,

I am using snakeyaml to parse yaml file I've. Unfortunately I am facing strange error(please see below) and looking for help in this regard. 

public class HelloYaml {
@SuppressWarnings("unchecked")
public static def void main(String[] args) throws FileNotFoundException {
val yaml = new Yaml(new Constructor());
System.out.println(yaml.dump(yaml.load(new FileInputStream(new File(
"HelloYaml.yaml")))));
val value =  yaml.load(new FileInputStream(new File("HelloYaml.yaml"))) as Map<String, User>
for (key : value.keySet()) {
println ("key:" + key)
}
val userinstace = value.get("d1")
println("user" + userinstace)
}
def static test(Map<Integer, Integer> map) {
val list = new ArrayList<Integer>(map.values);
println (list.get(0))
}
@org.eclipse.xtend.lib.annotations.Data static class Users
{
Map<String, User> map
}
}

@org.eclipse.xtend.lib.annotations.Data class User {
    String name;
    Integer age;    
}

Error: Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.avin.yaml.reader.User
at com.avin.yaml.reader.HelloYaml.main(HelloYaml.java:93)

Christian Dietrich

unread,
Apr 22, 2017, 6:34:54 AM4/22/17
to Xtend Programming Language
Hi,

i think the problem is somewhere in the code you did not show (the load method)
Reply all
Reply to author
Forward
0 new messages