Export to GitHub

snakeyaml - issue #3

does not dump maps of beans correctly


Posted on Jul 2, 2009 by Happy Panda

What steps will reproduce the problem?

import java.io.; import java.util.; import junit.framework.TestCase; import org.yaml.snakeyaml.Yaml;

public class MapYamlTest extends TestCase {

public void testYamlMap() throws IOException {
    Map<String, Bean> data = new TreeMap<String, Bean>();
    data.put("gold1", new Bean());
    data.put("gold2", new Bean());

    Yaml yaml = new Yaml();
    File file = new File("beantest.yml");

    FileOutputStream os = new FileOutputStream(file);
    yaml.dump(data, new OutputStreamWriter(os));
    os.close();

    FileInputStream is = new FileInputStream(file);
    Object o = yaml.load(new InputStreamReader(is));
    is.close();

    assertTrue(o instanceof Map);
    Map m = (Map)o;
    assertTrue(m.get("gold1") instanceof Bean);
    assertTrue(m.get("gold2") instanceof Bean);
}

public static class Bean {
    private String a;
    public Bean() { a = ""; }
    public String getA() { return a; }
    public void setA(String s) { a = s; }

}

}

What is the expected output? What do you see instead?

Test should pass. However, it fails on the last assert. If you look at beantest.yml it has this:

gold1: !![package omitted]MapYamlTest$Bean {a: ''} gold2: {a: ''}

In other words, it's only writing the tag for the first entry in the map.

What version of the product are you using? On what operating system?

Latest SnakeYAML from the hg repo, on Debian

Please provide any additional information below.

N/A

Comment #1

Posted on Jul 2, 2009 by Happy Panda

The same issue occurs if you replace a Map with a List.

Comment #2

Posted on Jul 3, 2009 by Massive Rhino

(No comment was entered for this change.)

Comment #3

Posted on Jul 3, 2009 by Massive Rhino

(No comment was entered for this change.)

Comment #4

Posted on Jul 3, 2009 by Grumpy Horse

Fixed. Now the document looks like: gold1: !!org.yaml.MapYamlTest$Bean {a: ''} gold2: !!org.yaml.MapYamlTest$Bean {a: ''}

The problem was that SnakeYAML assumes that a JavaBean is the root of the YAML document. This is how it is typically used. It also allows to completely eliminate the class declaration and exchange the YAML documents with other programming languages. For instance if the example above is emitted as simply:

gold1: {a: ''} gold2: {a: ''}

then it is very well possible to parse this document in Python.

Thank you for the contribution.

-Andrey

Comment #5

Posted on Jul 5, 2009 by Happy Panda

ah, ok. thanks for the quick fix! :)

Status: Fixed

Labels:
Type-Defect Priority-Medium