Using dynamic objects really is much easier, I used to deserialize the response to a predefined class which was a waste of time.
Those methods helped me a lot to serialize and deserialize objects and strings.
public static T Deserialize<T>(string json)
{
var _Bytes = Encoding.Unicode.GetBytes(json);
using (MemoryStream _Stream = new MemoryStream(_Bytes))
{
var _Serializer = new DataContractJsonSerializer(typeof(T));
return (T)_Serializer.ReadObject(_Stream);
}
}
public static string Serialize(object instance)
{
using (MemoryStream _Stream = new MemoryStream())
{
var _Serializer = new DataContractJsonSerializer(instance.GetType());
_Serializer.WriteObject(_Stream, instance);
_Stream.Position = 0;
using (StreamReader _Reader = new StreamReader(_Stream))
{
return _Reader.ReadToEnd();
}
}
}
Thanks,
Ibraheem Osama Mohamed | My Blog |
@IbraheemOM |
My Website
(If my reply answers your question, please propose it as an answer)