Bug 17070 - Serialization fails when using DataContractJsonSerializerSettings
Summary: Serialization fails when using DataContractJsonSerializerSettings
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: General ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-01-06 13:24 UTC by Jon Goldberger [MSFT]
Modified: 2014-01-06 13:24 UTC (History)
1 user (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 for Bug 17070 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Jon Goldberger [MSFT] 2014-01-06 13:24:13 UTC
From forum post:


I use the following code in my shared code across platforms to serialize objects.

public static string SerializeToJson<T>(this T obj)
                using (MemoryStream stream = new MemoryStream())
                    DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true };
                    DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof (T), settings);
                    jsonSer.WriteObject(stream, obj);
                    stream.Position = 0;
                    StreamReader sr = new StreamReader(stream);
                    return sr.ReadToEnd();
            catch (Exception ex)
                Debug.WriteLine("Serialization FAILED: {0}", ex.ToString());
            return null;
If you try the code you will notice when debugging that from DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof (T), settings) to return sr.ReadToEnd(); which result to jsonSernull and ofcourse returns null.

If you remove the _settings _and try with DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof (T)); everything works fine but I want to use the simple dictionary format option in my Dictionary<string, string> property.

From me: 
I confirmed this behavior in a template Xam.iOS app. I found a workaround to avoid the exception, but the results of the serialization differ from those of .NET.

Setting UseSimpleDictionaryFormat = true on the DataContractJsonSerializer should yield results for a Dictionary<string, string> such as:
{"Key1":"Value1", "Key2":"Value2", ... } // No array brackets.

After using workaround to avoid exception, the output from the serializer in MonoTouch is:
[{"Key":"Key1","Value":"Value1"},{"Key":"Key2","Value":"Value2"}, ...}]

Workaround to resolve exception noted in forum post:
settings.RootName = "root";
settings.MaxItemsInObjectGraph = 7; // this was for a Dictionary<string, string> that I added 3 items to.