ilmoeuro / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
0 stars 0 forks source link

does not dump maps of beans correctly #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by infinity0x@gmail.com on 2 Jul 2009 at 4:54

GoogleCodeExporter commented 9 years ago
The same issue occurs if you replace a Map with a List.

Original comment by infinity0x@gmail.com on 2 Jul 2009 at 4:56

GoogleCodeExporter commented 9 years ago

Original comment by py4fun@gmail.com on 3 Jul 2009 at 9:13

GoogleCodeExporter commented 9 years ago

Original comment by py4fun@gmail.com on 3 Jul 2009 at 9:13

GoogleCodeExporter commented 9 years ago
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

Original comment by aso...@gmail.com on 3 Jul 2009 at 4:15

GoogleCodeExporter commented 9 years ago
ah, ok. thanks for the quick fix! :)

Original comment by infinity0x@gmail.com on 5 Jul 2009 at 1:40