Bug 21583 - DataContractJsonSerializer fails to deserialize floating-point numbers as object
Summary: DataContractJsonSerializer fails to deserialize floating-point numbers as object
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies ()
Version: 3.2.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: marcos.henrich
Depends on:
Reported: 2014-07-24 17:23 UTC by Fredrik Høisæther Rasch
Modified: 2017-09-06 16:55 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Test code to replicate the error (698 bytes, text/x-csharp)
2014-07-24 17:23 UTC, Fredrik Høisæther Rasch
Proposed bugfix containing only changes made to the buggy method (1.66 KB, text/x-csharp)
2014-07-24 17:33 UTC, Fredrik Høisæther Rasch

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Fredrik Høisæther Rasch 2014-07-24 17:23:42 UTC
Created attachment 7484 [details]
Test code to replicate the error

Attached to this bug report is a test program that replicates this bug.

Steps to reproduce:
Suppose you have a System.IO.Stream containing a JSON string that contains at least one floating-point number (e.g. the JSON string "42.42"). Deserialize that string using an instance of the System.Runtime.Serialization.Json.DataContractJsonSerializer class using System.Object as type-information (NOT one of the CLR floating-point datatypes!).

Current buggy behaviour:
The call to ReadObject(System.IO.Stream) of the created System.Runtime.Serialization.Json.DataContractJsonSerializer instance throws a System.Runtime.Serialization.SerializationException.

Behaviour in the .NET framework:
The call to ReadObject(System.IO.Stream) successfully deserializes the stream to an object value. The floating-point value is actually a System.Decimal value that is boxed in a System.Object instance.

I discovered this bug when I attempted to deserialize the JSON response of a web-service that returns the fields of a data as a JSON array where each element is a field of the returned data row. In that case I have to deserialize the response with System.Object[] as the type parameter passed to the System.Runtime.Serialization.Json.DataContractJsonSerializer constructor. This works fine in the .NET framework in Windows, but Mono fails to do this.

Proposed fix:
As I browsed through the source code of the Mono implementation I discovered that the ReadInstanceDrivenObject() method of the System.Runtime.Serialization.Json.JsonSerializationReader class uses a series of TryParse calls when it recognizes the JSON value as JSON number type. The calls to the TryParse functions all use System.Globalization.NumberStyles.None as the second argument, however, and that is the problem.
Quote from http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx :
System.Globalization.NumberStyles.None: Indicates that no style elements, such as leading or trailing white space, thousands separators, or a decimal separator, can be present in the parsed string. The string to be parsed must consist of integral decimal digits only.
The ReadInstanceDrivenObject() method should use System.Globalization.NumberStyles.Integer for the interger types, and System.Globalization.NumberStyles.Float for the floating-point types.

The file attached file test.cs can be compiled to test.exe using the following command:
mcs -r:System.Runtime.Serialization test.cs
Comment 1 Fredrik Høisæther Rasch 2014-07-24 17:26:47 UTC
In the Ubuntu source package the implementation of the System.Runtime.Serialization.Json.JsonSerializationReader class can be found at the following location:
Comment 2 Fredrik Høisæther Rasch 2014-07-24 17:33:44 UTC
Created attachment 7485 [details]
Proposed bugfix containing only changes made to the buggy method
Comment 3 marcos.henrich 2014-09-25 10:46:12 UTC
Hi Fredrik,

Thank you for the detailed bug report.

The pull request for this issue can be found in the link below.
Comment 4 marcos.henrich 2014-09-25 12:54:06 UTC
Fixed in master 410af9c0cc1b781cbe434d4706411f67a28b5eb9.