Bug 28966 - Issue with adding nullable values to IDictionary
Summary: Issue with adding nullable values to IDictionary
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: unspecified
Hardware: Other Other
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-04-10 04:13 UTC by henrik.ilgen
Modified: 2015-04-10 04:29 UTC (History)
3 users (show)

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

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 henrik.ilgen 2015-04-10 04:13:04 UTC
The following code works just fine when run in a Windows Console Application:

    var failingJson = "{ \"something\": null }";
    var failingConverted = JsonConvert.DeserializeObject<Dictionary<string, decimal?>>(failingJson);

however, when run on an Android emulator using Xamarin.Forms, it produces an exception:

    System.ArgumentException: not of type: System.Nullable`1[System.Decimal]
    Parameter name: value

Stack trace:

    at System.Collections.Generic.Dictionary`2[System.String,System.Nullable`1[System.Decimal]].ToTValue (System.Object value) [0x00000] in :0
    at System.Collections.Generic.Dictionary`2[System.String,System.Nullable`1[System.Decimal]].System.Collections.IDictionary.set_Item (System.Object key, System.Object value) [0x00000] in :0
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary (IDictionary dictionary, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonDictionaryContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id) [0x00000] in :0

The very same project, run on the Windows Phone emulator, runs perfectly fine again.

Running JSON.NET 6.0.8, Xamarin.Forms, Xamarin.Android.Support.v4 19.0.2

According to Xamarin Forums user Miha Markic, the following code can also be used to reproduce the issue (see http://forums.xamarin.com/discussion/comment/114648/#Comment_114648):

       Dictionary<string, decimal?> dic = new Dictionary<string, decimal?>( );
        IDictionary d2 = dic;
        decimal? v = 1;
        d2["tubo"] = v;  // works
        dic["tubo3"] = null; // works
        d2["tubo2"] = null; // miserably fails converting null to decimal?

Note: I first raised this issue on JSON.NET's github, here: https://github.com/JamesNK/Newtonsoft.Json/issues/527
Comment 1 Marek Safar 2015-04-10 04:29:10 UTC
Already fixed in Mono 4.0