Serialization fails when using DataContractJsonSerializerSettings.

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 jsonSer null 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.

Regards.

Sign In or Register to comment.