ilmoeuro / snakeyaml

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

Dump a java.io.File object #46

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

I would like to dump a simple java.io.File object.
But I get a StackOverflowError by the Representer.
Is this correct?

To load a simple java.io.File object like
file: !!java.io.File /path/name
works fine.

I'm using version 1.5 on SLES 10

Thanks in advance.

Andi

Original issue reported on code.google.com by ah...@ionosinst.de on 25 Jan 2010 at 10:57

GoogleCodeExporter commented 9 years ago
Let us have a look at the source of java.io.File.
public File getAbsoluteFile() {
        String absPath = getAbsolutePath();
    return new File(absPath, fs.prefixLength(absPath));
}

This method creates a cycle. Unfortunately the returned File has a different 
identity
(different memory address) then the owner. SnakeYAML gets an endless chain of 
objects
while trying to serialise the File object as a JavaBean.
(SnakeYAML can handle cycles which end up at the same object)

Solution: 
1) Implement Represent to use !!java.io.File tag
This is how it may look like: (Please note that Tag is used instead of String)
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/issues/issue46/FileTest.java

2) Do not use java.io.File at all. One may expect to serialise the whole 
content and
not only the filename. JDK7 will (almost) deprecate java.io.File in favor of 
Path and
others.(http://java.sun.com/developer/technicalArticles/javase/nio/)

Original comment by aso...@gmail.com on 25 Jan 2010 at 2:44

GoogleCodeExporter commented 9 years ago

Original comment by aso...@gmail.com on 28 Jan 2010 at 2:55