UcasRichard / snakeyaml

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

Problems deserializing bean properties of type "Long" when scalars are in double quotes #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

Snakeyaml doesn't seem to be able to properly deserialize Long values
in beans when the original was serialized with
DumperOptions.ScalarStyle.DOUBLE_QUOTED enabled.

The way I see it, at deserialization time the numeric value
is parsed as Integer or Long depending on its size, and then snakeyaml
attempts to feed the bean's setter with it as-is.

This works fine if the value is actually outside the range of an
integer (which seems to be the case covered by the tests), but once
the value would fit in an Integer this system breaks down (trying to
pass an Integer to a Long setter).

Example test:

-----------
package org.yaml.snakeyaml.javabeans;

import junit.framework.TestCase;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.DumperOptions;

public class LongTest extends TestCase {
    public void testLong() {
        DumperOptions options = new DumperOptions();
        options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);

        Yaml yaml = new Yaml(options);

        Foo foo = new Foo();
        String output = yaml.dump(foo);
        System.out.println(output);
        Foo foo2 = (Foo) yaml.load(output);
        assertEquals(new Long(42L), foo2.getBar());
    }

    public static class Foo {
        private Long bar = Long.valueOf(42L);

        public Long getBar() {
            return bar;
        }

        public void setBar(Long bar) {
            this.bar = bar;
        }
    }
}
------------

I'm using snakeyaml-1.4 (release package).

Original issue reported on code.google.com by creini...@googlemail.com on 3 Sep 2009 at 11:05

GoogleCodeExporter commented 9 years ago
This is a rather complex issue. It is related to the problem that standard YAML 
tags 
have more then one Java implementation.
In general the runtime class is defined in the following order:
Explicit tag (!!int) -> runtime class (java.lang.Long)-> implicit tag (!!int)
As you can see when you use ScalarStyle.DOUBLE_QUOTED the explicit tag forces 
an 
Integer to be created and it breaks the test.
I will think how I can improve the situation. Meanwhile I have implemented a 
change 
in the Representer to allow custom tags for standard Java classes.
Check the latest source. Look here LongTest.testLongRepresenter() 
(http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeya
ml/ja
vabeans/LongTest.java)
Please let me know whether it is enough for you.

Original comment by py4fun@gmail.com on 3 Sep 2009 at 2:40

GoogleCodeExporter commented 9 years ago
I've simply disabled DumperOptions.ScalarStyle.DOUBLE_QUOTED for now (only 
activated it before because it sounded like a Good Idea (TM)), which solved the 
problem for me.

Original comment by creini...@googlemail.com on 3 Sep 2009 at 2:48

GoogleCodeExporter commented 9 years ago

Original comment by py4fun@gmail.com on 4 Sep 2009 at 7:15