4

I'm using JavaScriptSerializer.Deserialize() to get data from a JSON file.
But it ignores the decimal period, although using .GetType() on the value, returns System.Decimal.

This is the C# code:

JavaScriptSerializer jss = new JavaScriptSerializer();          
Dictionary< string, object > dic = jss.Deserialize< Dictionary< string, object >>( json );

This is the JSON:

{ "num": 3.14 }  

I try this: Console.WriteLine ( "{0} {1}", dic["num"].GetType(), dic["num"] );
And get this: System.Decimal 314

PS: I'm new to .NET as you can see.

Petruza
  • 11,744
  • 25
  • 84
  • 136
  • It could be the way the value is displayed using Console.WriteLine. Try to assign it to a variable first to see if it has the correct value. – Rui Jarimba Nov 22 '12 at 16:26
  • 1
    @Rui: that's unlikely. With no additional formatting specifiers, a floating point number should be printed correctly (using a separator depending on the current `CultureInfo`). `JavaScriptSerializer`, on the other hand, should use `InvariantCulture`, so this seems weird. – vgru Nov 22 '12 at 16:30
  • Works on my machine :-/ It shouldn't work, but try `{ "num": 3,14 }` instead. – Rawling Nov 22 '12 at 16:39
  • 1
    Nope, it crashes with `3,14`. You say the JSON interpretation varies with the locale? but the JSON specification does not depend on any locale, decimals use always period. – Petruza Nov 22 '12 at 17:11
  • I am having the same problem and it does seem to be locale related. I am trying to nail it down now. – Sentinel Oct 14 '13 at 13:27

2 Answers2

1

You must be doing something else you are not telling us.

Here is complete working code:

String json = " { \"num\": 3.14 }";
JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string, object> dic = jss.Deserialize<Dictionary<string, object>>(json);

String test = String.Format("{0} {1}", dic["num"].GetType(), dic["num"]);
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
0

I was having the same problem in a WCF service hosted in IIS/WAS, where I was using this class to deserialise some JSON. The problem appeared after the service was moved from one production server to another, where the culture settings were different.

The root problem turned out to be different. Even though the App Pool under which the WCF service was running was for an identity with the correct culture, immediately after a server reboot the process under which this service was running turned out to have either the wrong identity or wrong culture. We still haven't figured out where the problem is exactly, but it is either due to ThreadPool contamination (threads returned to thread pool do not reset their culture), or a bug in AppFabric autostart... perhaps.

In any case, the problem in the original post is down to wrong CurrentCulture, and perhaps an incorrect implementation of the JavaScriptSerializer (is it not the case that JSON norms mandate a decimal dot for decimals?) . Decimals will not deserialise correctly with an incompatible CurrentCulture.

Sentinel
  • 3,582
  • 1
  • 30
  • 44