Tuesday, December 24, 2013

JavaScriptSerializer issue with ExtensionDataObject


I wanted to pass a WCF object to my application and then I was trying to serialize the object to json using JavaScriptSerializer and then deserialize it for using on the client side, I got an error when I tried to deserialize the text, no parameterless constructor defined for type of System.Runtime.Serialization.ExtensionDataObject. After some research, I found that the ExtensionData property in my WCF object is causing that issue, javascript serializer had no idea about that property. I found two options to solve that issue.


1- Use DataContractJsonSerializer instead of JavaScriptSerializer

  public static class DataContractJsonSerializerUtil
    {
        public static string Serialize(object obj)
        {
            var jsonSerializer = new DataContractJsonSerializer(obj.GetType());
            string returnValue = "";
            using (var memoryStream = new MemoryStream())
            {
                using (var writer = JsonReaderWriterFactory.CreateJsonWriter(memoryStream))
                {
                    jsonSerializer.WriteObject(writer, obj);
                    writer.Flush();
                    returnValue = Encoding.UTF8.GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
                }
            }
            return returnValue;
        }
        public static T Deserialize<T>(string json)
        {
            T returnValue;
            using (var memoryStream = new MemoryStream())
            {
                byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
                memoryStream.Write(jsonBytes, 0, jsonBytes.Length);
                memoryStream.Seek(0, SeekOrigin.Begin);
                using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(memoryStream, Encoding.UTF8, XmlDictionaryReaderQuotas.Max, null))
                {
                    var serializer = new DataContractJsonSerializer(typeof(T));
                    returnValue = (T)serializer.ReadObject(jsonReader);

                }
            }
            return returnValue;
        }
    }
2- Register a converter for JavascriptSerializer

public static class JavaScriptSerializerUtil
    {
        public static string Serialize(object obj)
        {
            var s = new JavaScriptSerializer();
            return s.Serialize(obj);
        }
        public static T Deserialize<T>(string json)
        {
            var jss = new JavaScriptSerializer();
            jss.RegisterConverters(new[] { new MyJsCustomConverter() });
            var result = jss.Deserialize<T>(json);
            return result;
        }
    }
    public class MyJsCustomConverter : JavaScriptConverter
    {
        public override IEnumerable<Type> SupportedTypes
        {
            get
            {
                return new ReadOnlyCollection<Type>(new[] { typeof(ExtensionDataObject) });
            }
        }
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            return null;
        }
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            return null;
        }
    }

0 comments:

Post a Comment