Wrong deserialization of decimal values if decimal point is

Posted by Community Admin on 04-Aug-2018 10:12

Wrong deserialization of decimal values if decimal point is not dot

All Replies

Posted by Community Admin on 08-Nov-2016 00:00

To reproduce problem

1. Create controller

I have created mvc widget controller with some decimal properties. Controller code is:

[ControllerToolboxItem(Name = "MapWidget", Title = "Zemljevid", SectionName = "SplosniPrikaziSection")]
    public class MapWidgetController : ControllerBase, IMapConfiguration
    
        public decimal? CenterLongitude get; set;
        public decimal? CenterLatitude get; set;
...


2. Using widget designer :
Now with the testing. I enter value like: 46.084162. I then click save and error pops up: Error! 46.084162 is not a valid value for Decimal. 
Ok, I presume because I have set Slovenian culture for backend, I should write comma instead of dot. So, I wrote 46,084162 and saved. Seams to work. I click page preview in editor(/Action/Preview/sl) and it seams to work.

3. I save(publish) page and go to url. The exception pops up: Input string was not in a correct format. Inner exception: 46.084162 is not a valid value for Decimal. The source of exception is Telerik.Sitefinity.Mvc.ControllerActionInvoker.DeserializeControllerProperties.

To debug problem I wrote custom TypeConverter and I watch CultureInfo value comes as attribute to ConvertTo:
- On opening designer: Invariant language (LCID 127) 
- On opening preview: Invariant language (LCID 127)
- On opening public page: null

Then I search some more and found out problem using decompiler:
It is in class MvcPropertySerializer in method ConvertValue.
On javascript serialization culture must be invariant, but it is null.
Call to ConvertFromString presume using current culture and not invariant.
The method should call ConvertFromInvariantString, at least for SetProperties method.
In testing: at this place string like "43.09" can't properly convert to value if current culture info uses anything else as dot for decimal point.

 

The only hackish solution I found is writing custom typeconverter and using this code in ConvertTo and ConvertFrom methods.

if(culture == null)
            
                object item = HttpContext.Current.Items["SfPageDesignMode"];
                bool isDesignMode = item != null ? (bool)item : false;
 
                culture = isDesignMode ? CultureInfo.CurrentCulture : CultureInfo.InvariantCulture;
            





This thread is closed