youxinren / snakeyaml

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

Using a locale with minimum number fraction digits breaks serializer #172

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use a NumberProvider that generates a NumberFormat with a minimum number of 
fraction digits
2. Try to dump a structure with anchors

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

I try to dump a bean which yaml structure contains anchors. I'm using a 
specialized locale that generates the following default number format:

public class IHLNumberFormatProvider extends NumberFormatProvider {
       @Override
       public NumberFormat getNumberInstance(Locale locale) {       
        return new DecimalFormat("0.00", getDecimalFormatSymbols(locale));
    }
}

you see, minimum number of fraction digitis is 2.

The serializer now generates anchors in the form " id001,00" which are illegal.
Exception in thread "main" org.yaml.snakeyaml.emitter.EmitterException: invalid 
character in the anchor: id001,00

What version of SnakeYAML are you using? On what Java version?
Testet with SnakeYAML 1.7 to 1.11

Fix would be easy, just define maximum number of fraction digits like so:

       private String generateAnchor() {
           this.lastAnchorId++;
           NumberFormat format = NumberFormat.getNumberInstance();
           format.setMinimumIntegerDigits(3);
           format.setMaximumFractionDigits(0);
           format.setGroupingUsed(false);
           String anchorId = format.format(this.lastAnchorId);
           return "id" + anchorId;
       }

 or provide the possibility to define a locale that the serializer should use.

Original issue reported on code.google.com by mich...@simons.ac on 29 Mar 2013 at 11:19

GoogleCodeExporter commented 9 years ago
Fixed. Feel free try the source or the latest snapshot. Please check if it 
works for you.

http://code.google.com/p/snakeyaml/source/list

Original comment by py4fun@gmail.com on 30 Mar 2013 at 6:32

GoogleCodeExporter commented 9 years ago
Thank you very much, works as expected. 

Have a nice day,
Michael.

Original comment by mich...@simons.ac on 30 Mar 2013 at 6:47

GoogleCodeExporter commented 9 years ago
It will be delivered in version 1.13

http://code.google.com/p/snakeyaml/wiki/changes

Original comment by py4fun@gmail.com on 30 Mar 2013 at 8:16