YAML Problems

Discussion in 'Plugin Development' started by Stmeter, Mar 16, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Stmeter

    Hey guys,

    I soon plan to add YAML to my plugins and I've run into a problem I can't figure. Here's my YAML file:
    Code:
    name: MyName
    type: 5
    codes:
     - name: Person1
       code: 555
    
    I can get name and type perfectly fine, but when it comes to getting information from codes it doesn't seem to work out too well.

    Here's my code:
    Code:
    import org.yaml.snakeyaml.*;
    import java.io.*;
    import java.util.Map;
    
    public class YamlTesting {
    
        public static void main(String[] args){
            runMe();
        }
    
        public static void runMe(){
            Yaml yaml = new Yaml();
            Reader reader = null;
    
            try{
                reader = new FileReader("foo.yml");
            } catch(FileNotFoundException fnfe){
                System.out.println("Error!");
            }
    
            Map<String, Map<String, Map<String, Object>>> data = (Map<String, Map<String, Map<String, Object>>>) yaml.load(reader);
            System.out.println(data.get("codes").get("name"));
        }
    }
    Any suggestions?
     
  2. Offline

    Sammy

    Try using the yml like this:
    Code:
    name: MyName
    type: 5
    codes: |
           name: Person1
           code: 555
    
    and I think you have one extra Map no ? I can only see 2 trees the one with name types and codes, and the sub-tree of codes:

    Code:
    Map<String, Map<String, Object>>> data = ( Map<String, Map<String, Object>>>) yaml.load(reader);
    
     
  3. Offline

    Stmeter

    Just tried your suggestions, Sammy and I'm getting this error.

    Which is the exact same error I was getting before.

    Code:
    Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
    	at YamlTesting.runMe(YamlTesting.java:25)
    	at YamlTesting.main(YamlTesting.java:11)
    
     
Thread Status:
Not open for further replies.

Share This Page