Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Deserialization method to Async

I am trying to convert this method that deserializes an object into a string with Async/Await.

    public static T DeserializeObject<T>(string xml)
    {
        using (StringReader reader = new StringReader(xml))
        {
            using (XmlReader xmlReader = XmlReader.Create(reader))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                T theObject = (T)serializer.ReadObject(xmlReader);
                return theObject;
            }
        }
    }
like image 398
Terrence Avatar asked Aug 09 '12 12:08

Terrence


2 Answers

Most serialization APIs do not have async implementations, which means the only thing you can really do is wrap a sync method. For example:

public static Task<T> DeserializeObjectAsync<T>(string xml)
{
    using (StringReader reader = new StringReader(xml))
    {
        using (XmlReader xmlReader = XmlReader.Create(reader))
        {
            DataContractSerializer serializer =
                new DataContractSerializer(typeof(T));
            T theObject = (T)serializer.ReadObject(xmlReader);
            return Task.FromResult(theObject);
        }
    }
}

This isn't actually async - it just meets the required API. If you have the option, using ValueTask<T> is preferable in scenarios where the result may often be synchronous/

Either way, you should then be able to do something like:

var obj = await DeserializeObject<YourType>(someXml);
Debug.WriteLine(obj.Name); // etc

without needing to know whether the actual implementation was synchronous or asynchronous.

like image 145
Marc Gravell Avatar answered Oct 21 '22 09:10

Marc Gravell


A little sample, pretty primitive way:

    public delegate T Async<T>(string xml);

    public void Start<T>()
    {
        string xml = "<Person/>";
        Async<T> asyncDeserialization = DeserializeObject<T>;
        asyncDeserialization.BeginInvoke(xml, Callback<T>, asyncDeserialization);
    }

    private void Callback<T>(IAsyncResult ar)
    {
        Async<T> dlg = (Async<T>)ar.AsyncState;
        T item = dlg.EndInvoke(ar);
    }

    public T DeserializeObject<T>(string xml)
    {
        using (StringReader reader = new StringReader(xml))
        {
            using (XmlReader xmlReader = XmlReader.Create(reader))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                T theObject = (T)serializer.ReadObject(xmlReader);
                return theObject;
            }
        }
    }

you define a delegate and using it to Begin/End invoke using callbacks.

using the next versions of C# you can use the async keyword to get your code run asynchronously.

like image 44
Tamir Avatar answered Oct 21 '22 09:10

Tamir