UcasRichard / snakeyaml

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

StackOverflowError #133

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello.

I have some problems with custom objects dumping.

Example:

HashMap<String, Object> m = new HashMap<String, Object>();

m.put("i", 1234);//works fine
m.put("b", false);//works fine
m.put("s", "text");//works fine

m.put("l", new Point());//Exception java.lang.StackOverflowError
//m.put("l", new AnotherObject());//I want to use another objects to dump/load

System.out.println(yaml.dump(m));

I'm using latest SnakeYaml version.

Original issue reported on code.google.com by GZe...@gmail.com on 14 Sep 2011 at 8:31

GoogleCodeExporter commented 9 years ago
Please provide more information.
Just looking in the example gives the impression that SnakeYAML has nothing to 
do with the problem because the exception occurs when you put an object to a 
Map.
Does the problem occur when you dump ?
Can you please provide a failing test? (SnankeYAML has a lot of examples with 
Map and custom objects. See the tests.

Original comment by py4fun@gmail.com on 14 Sep 2011 at 10:03

GoogleCodeExporter commented 9 years ago
Hello. I've seen all examples.

> Does the problem occur when you dump ?
Yes.

Here is a test:
<code>
    public void snakeyaml() {
        Yaml yaml = new Yaml();

        HashMap<String, Object> m = new HashMap<String, Object>();

        m.put("i", 1234);//works fine
        m.put("b", false);//works fine
        m.put("s", "text");//works fine

        m.put("l", new Point());//Will be Exception java.lang.StackOverflowError on DUMPING
        //m.put("l", new AnotherObject());//I want to use another objects to dump/load
        System.out.println(yaml.dump(m));
    }
</code>

Original comment by GZe...@gmail.com on 15 Sep 2011 at 6:39

GoogleCodeExporter commented 9 years ago
Test is the best way to explain your idea. It is not clear that Point is 
actually java.awt.Point (because AnotherObject is obviously not 
java.awt.AnotherObject). 

Now I see the problem. The stackoverflow happens because Point creates a new 
instance in the method 'getLocation()'. SnakeYAML works properly with recursive 
structures but it cannot prevent this case.
One of the possible solutions may be to skip the 'location' property when 
dumping.
I have put the complete solution to the tests:
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/issues/issue133/StackOverflowTest.java

There is also a brief description how to skip a JavaBean property:
http://code.google.com/p/snakeyaml/wiki/howto#How_to_skip_a_JavaBean_property

Original comment by py4fun@gmail.com on 15 Sep 2011 at 8:46

GoogleCodeExporter commented 9 years ago
Hello.

Thanks for answer and sorry for my bad code without import "java.awt." 
package...

> One of the possible solutions may be to skip the 'location' property when 
dumping.
I think, i will change "Point" for int.x and int.y variables, because I dont 
want to use custom representer.

> SnakeYAML works properly with recursive structures but it cannot prevent this 
case.
Will this feature be added to SnakeYAML?

Thanks a lot again!

Original comment by GZe...@gmail.com on 15 Sep 2011 at 6:01

GoogleCodeExporter commented 9 years ago
You are welcome!
It cannot be added as a feature. It is the same as if there is an exception 
when you get a value. It can only be solved on the application level.

Original comment by py4fun@gmail.com on 15 Sep 2011 at 8:04