Thinkofname / snakeyaml

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

No support for Number class #183

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Construct a class with a populated Number field
2. Dump to yaml
3. Load to new object

What is the expected output? What do you see instead?

The Number-fielded object.
Unsupported class exception.

What version of SnakeYAML are you using? On what Java version?

1.13
Java 1.7

Please provide any additional information below. (Often a failing test is
the best way to describe the problem.)

Failure demonstrated in following code:

import org.yaml.snakeyaml.Yaml;

public class YamlReader {

  public static class YamlTest {
    public Number numero;
  }

  public static void main (String ... args) {
    Yaml yam = new Yaml();
    YamlTest x = new YamlTest();
    x.numero = 1;

    String dumpage = yam.dump(x);

    System.out.println(dumpage);

    YamlTest recreated = (YamlTest)yam.load(dumpage);
  }
}

Workable patches follow:

Index: org/yaml/snakeyaml/constructor/Constructor.java
===================================================================
--- org/yaml/snakeyaml/constructor/Constructor.java 
+++ org/yaml/snakeyaml/constructor/Constructor.java (working copy)
@@ -18,6 +18,7 @@
 import java.beans.IntrospectionException;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collection;
@@ -485,7 +486,11 @@
                 ConstructYamlTimestamp contr = new ConstructYamlTimestamp();
                 contr.construct(node);
                 result = contr.getCalendar();
-            } else {
+            } else if (Number.class.isAssignableFrom(type)){
+                ConstructYamlNumber contr = new ConstructYamlNumber();
+                result = contr.construct(node);
+            }
+            else {
                 throw new YAMLException("Unsupported class: " + type);
             }
             return result;
Index: org/yaml/snakeyaml/constructor/SafeConstructor.java
===================================================================
--- org/yaml/snakeyaml/constructor/SafeConstructor.java 
+++ org/yaml/snakeyaml/constructor/SafeConstructor.java (working copy)
@@ -16,6 +16,8 @@
 package org.yaml.snakeyaml.constructor;

 import java.math.BigInteger;
+import java.text.NumberFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
@@ -281,6 +283,20 @@
     private final static Pattern YMD_REGEXP = Pattern
             .compile("^([0-9][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)$");

+    public static class ConstructYamlNumber extends AbstractConstruct {
+        private final NumberFormat nf = NumberFormat.getInstance();
+        
+        @Override
+        public Object construct(Node node) {
+            ScalarNode scalar = (ScalarNode) node;
+            try {
+                return nf.parse(scalar.getValue());
+            } catch (ParseException e) {
+                throw new IllegalArgumentException("Unable to parse as Number: 
" + scalar.getValue());
+            }
+        }
+    }
+    
     public static class ConstructYamlTimestamp extends AbstractConstruct {
         private Calendar calendar;

Original issue reported on code.google.com by darren.j...@gmail.com on 31 Oct 2013 at 6:11

GoogleCodeExporter commented 9 years ago
Created a test and a patch that does not break other tests.

Original comment by darren.j...@gmail.com on 4 Nov 2013 at 4:24

Attachments:

GoogleCodeExporter commented 9 years ago
Applied. Please check.

Original comment by py4fun@gmail.com on 7 Nov 2013 at 6:29

GoogleCodeExporter commented 9 years ago
Looks good, thanks.

Original comment by darren.j...@gmail.com on 8 Nov 2013 at 6:04

GoogleCodeExporter commented 9 years ago
Found another sub-issue: Number fields failed to parse dumped values for +/- 
Infinity and NaN (but only as Doubles).
This patch corrects the problem and adds some tests.

Original comment by darren.j...@gmail.com on 18 Dec 2013 at 7:44

Attachments:

GoogleCodeExporter commented 9 years ago
Thank you. I will try it this weekend.

Original comment by py4fun@gmail.com on 20 Dec 2013 at 2:36

GoogleCodeExporter commented 9 years ago
Sorry, it took so long.
The patch is applied. The latest SNAPSHOT contains the changes. It will be 
delivered in version 1.14
Feel free to test.

Original comment by py4fun@gmail.com on 3 Apr 2014 at 4:41