Bug 17070 - Serialization fails when using DataContractJsonSerializerSettings
Summary: Serialization fails when using DataContractJsonSerializerSettings
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-01-06 13:24 UTC by Jon Goldberger [MSFT]
Modified: 2014-01-06 13:24 UTC (History)
1 user (show)

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


Attachments

Description Jon Goldberger [MSFT] 2014-01-06 13:24:13 UTC
From forum post:
http://forums.xamarin.com/discussion/11810/serialization-fails-when-using-datacontractjsonserializersettings#latest

Hi,

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

public static string SerializeToJson<T>(this T obj)
        {
            try
            {
                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.

Note You need to log in before you can comment on or make changes to this bug.