UcasRichard / snakeyaml

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

Yaml.load(String) fails with dates with milliseconds with a zero #80

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Parse a date with a zero (i.e. 0xx milliseconds or 00x milliseconds)

What is the expected output? What do you see instead?
given: "some time.001"
expected: 001 milliseconds when given 001 milliseconds
actual: 100 milliseconds

What version of the product are you using? On what operating system?
version: 1.7 on Ubuntu 8.04 32 bits

Please provide any additional information below.

Here's a unit test that proves it:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import junit.framework.TestCase;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.representer.Representer;

public class TestYamlDate extends TestCase {

    private static final Yaml YAML = createDefaultYAML();
    private static SimpleDateFormat PARSE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public void testDeserializeDate() throws ParseException {
        Date date = PARSE_FORMAT.parse("2010-05-16 03:06:11.003");
        assertEquals(date.getTime(), ((Date)YAML.load(YAML.dump(date))).getTime());
    }

    public static final Yaml createDefaultYAML() {
        // Configure Dumper
        Representer representer = new Representer();
        DumperOptions options = new DumperOptions() {{ 
            setExplicitStart(true);
            setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
            setAllowUnicode(false);
        }};

        // Configure Loader
        Constructor constructor = new Constructor();
        constructor.addTypeDescription(new TypeDescription(String.class, "!str"));
        constructor.addTypeDescription(new TypeDescription(String.class, "!binary"));

        return new Yaml(constructor, representer, options);
    }

}

Original issue reported on code.google.com by Sebastie...@gmail.com on 24 Aug 2010 at 3:32

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Fixed. Thank you for your time. Check the source or 1.8-SNAPSHOT

http://code.google.com/p/snakeyaml/source/detail?r=38582a6389da16f4cf9b89360fbea
bf0030db414

N.B. Normally you do not need to define TypeDescription for standard Java 
classes (String, Integer). Feel free to ask a question in the mailing list.

Original comment by py4fun@gmail.com on 24 Aug 2010 at 9:18