locked
Consuming REST Services in your Windows Store and Phone Applications RRS feed

  • General discussion

  • The following discusses a general approach to consuming REST Services in a Windows Phone and Store applications:

    Bret Bentzinger (MSFT) @awehellyeah

    Thursday, January 9, 2014 10:18 PM
    Moderator

All replies

  • 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)

    Monday, January 13, 2014 3:16 PM