c# json serialization

Two useful methods when comes to work with json format c#.

        public static T Deserialise(string json)
        {
            T obj = Activator.CreateInstance();
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                obj = (T)serializer.ReadObject(ms); 
                return obj;
            }
        }

        public static string Serialize(T obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                return Encoding.Default.GetString(ms.ToArray());
            }
        }

Advertisements

IE and $.getJSON

Recently I had a problem on IE because it cache my getJson call. The solution was to add the following line before call:

$.ajaxSetup({ cache: false });