tl24 / jsonexserializer

Automatically exported from code.google.com/p/jsonexserializer
1 stars 1 forks source link

Deserialisation of Double is CultureSpecific #37

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Version 2.1.1.170

the deserialisation of double-values is culture specific (that is the used 
Convert.ChangeType(..) is culture specific)
in german a dot is used the delimit thousand and a comma is used as 
decimal delimiter
en: 1234.13
de: 1.234,13

so deserialisation of "1234.13" leads to "123413.0".
My quick fix was to always use the "en-US" culture to convert doubles.

File:ValueEvaluator.cs
My changed "Evaluate"-Method looks like this:

public override object Evaluate() {
    if (Expression.ResultType.IsEnum)
        return Enum.Parse(Expression.ResultType, Expression.Value);
    else if (Expression.ResultType == typeof(object))
        return Expression.Value;
    else {                
        if (Expression.ResultType.Name == "Double") {
            IFormatProvider formatter = 
System.Globalization.CultureInfo.CreateSpecificCulture("en-
US").NumberFormat;
            return Convert.ChangeType(Expression.Value, 
Expression.ResultType, formatter);
        } else {
            return Convert.ChangeType(Expression.Value, 
Expression.ResultType);
        }
    }
}

Original issue reported on code.google.com by armin...@gmx.de on 20 Jan 2009 at 9:57

GoogleCodeExporter commented 9 years ago
Same is true for serialisation.
So changing the JsonWriter-Methods Value(double value) and Value(float value)
to

_writer.Write(value.ToString("R", 
System.Globalization.CultureInfo.CreateSpecificCulture("en-US").NumberFormat));

instead of 

_writer.Write(value.ToString("R");

fixes the problem for me

Original comment by armin...@gmx.de on 22 Jan 2009 at 2:11

GoogleCodeExporter commented 9 years ago
Thanks for the code.  I think I will try using the InvariantCulture.

Original comment by elliott....@gmail.com on 22 Jan 2009 at 4:19

GoogleCodeExporter commented 9 years ago

Original comment by elliott....@gmail.com on 26 Jan 2009 at 12:57

GoogleCodeExporter commented 9 years ago
3.0 Release

Original comment by elliott....@gmail.com on 26 Jan 2009 at 2:22