How to read a list of strings using SnakeYaml

5.1k views Asked by At

I want to read a simple YAML file using SnakeYaml

#My project 
  name: glog
  dependencies: 
    bling
    blong

Using this simple Java class:

public class Project {
    private String name;

    private ArrayList<String> dependencies = new ArrayList<String>();


    private final IvyManager ivyManager = new IvyManager();

    public Project() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<String> getDependencies() {
        return dependencies;
    }

    public void setDependencies(ArrayList<String> dependencies) {
        this.dependencies = dependencies;
    }
}

To be able to read the file, I need to prepare the Yaml parser because of the dependencies collection like this:

    Constructor constructor = new Constructor(Project.class);
    TypeDescription projectDesc = new TypeDescription(Project.class);
    projectDesc.putListPropertyType("dependencies", String.class);
    constructor.addTypeDescription(projectDesc);

    Yaml yaml = new Yaml(constructor);
    Project result = yaml.loadAs(FileUtils.readFileToString(f),
            Project.class);

Unfortunately, this doesn't work. I have the following error:

Cannot create property=dependencies for JavaBean=ninja.core.Project@26653222
 in 'string', line 2, column 3:
      name: glog
      ^
Can't construct a java object for scalar tag:yaml.org,2002:str; No String constructor found. Exception=java.util.ArrayList.<init>(java.lang.String)
 in 'string', line 4, column 5:
        bling
        ^

    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:308)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:189)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:341)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
    at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
    at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450)
    at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:427)
    at ninja.core.ProjectLoader.load(ProjectLoader.java:20)
    at ninja.command.Command.loadCurrentProject(Command.java:44)
    at ninja.command.EclipseCommand.execute(EclipseCommand.java:18)
    at ninja.cmd.Main.execute(Main.java:39)
    at ninja.cmd.Main.main(Main.java:15)
Caused by: org.yaml.snakeyaml.error.YAMLException: Can't construct a java object for scalar tag:yaml.org,2002:str; No String constructor found. Exception=java.util.ArrayList.<init>(java.lang.String)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructScalar.construct(Constructor.java:406)
    at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:297)
    ... 12 more
Caused by: java.lang.NoSuchMethodException: java.util.ArrayList.<init>(java.lang.String)
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructScalar.construct(Constructor.java:404)
    ... 14 more

Is there something wrong with the String class there?

3

There are 3 answers

0
Anthon On BEST ANSWER

Your YAML is correct and dependencies is a scalar, in this case the string "bling blong".

It always helps to check your YAML e.g. by using some online verifier/converter. E.g. the Online YAML Parser. If you convert to JSON there you see the problem and moreover you can interactively try out things until the output is a better fit for what you expect in your code and then use that:

#My project
  name: glog
  dependencies:
    - bling
    - blong

Beware that some online parsers cannot handle your code (and they are IMO incorrect not to do so: e.g. Convert YAML to JSON Online and Code Beautify cannot handle your example in the original, nor in the final form.)

0
Name is carl On

The Yaml is not valid for the expected use case.

Instead write:

#My project 
  name: glog
  dependencies: 
    - bling
    - blong
1
Andrey On

dependencies is no scalar, but a list. List values must be explicitly defined:

#My project 
  name: glog
  dependencies: 
    - bling
    - blong