using System; using System.IO; using System.Reflection; using System.Text; using System.Web; namespace EasyDevCore.Remote.HttpAccess { /// /// ISerializer implementation that converts an object representing name/value pairs to a URL-encoded string. /// Default serializer used in calls to PostUrlEncodedAsync, etc. /// public class UrlEncodedSerializer : ISerializer { /// /// Serializes the specified object. /// /// The object. public string Serialize(object obj) { if (obj == null) return null; StringBuilder sb = new StringBuilder(); PropertyInfo[] properties = obj.GetType().GetProperties(); foreach (PropertyInfo p in properties) { object value = p.GetValue(obj, null); string stringValue = value == null ? string.Empty : value.ToString(); string objectName = HttpUtility.UrlEncode(p.Name); if (stringValue.Length > 0) { stringValue = HttpUtility.UrlEncode(stringValue); objectName = HttpUtility.UrlEncode(objectName); if (sb.Length > 0) sb.Append("&"); sb.Append(objectName).Append("=").Append(stringValue); } } return sb.ToString(); } /// /// Serializes the specified object. /// /// /// The object. /// public string Serialize(T obj) { if (obj == null) return null; return Serialize(obj); } /// /// Deserializes the specified s. /// /// /// The s. /// Deserializing to UrlEncoded not supported. public T Deserialize(string s) { throw new NotImplementedException("Deserializing to UrlEncoded is not supported."); } /// /// Deserializes the specified stream. /// /// /// The stream. /// Deserializing to UrlEncoded not supported. public T Deserialize(Stream stream) { throw new NotImplementedException("Deserializing to UrlEncoded is not supported."); } /// /// Deserializes the asynchronous. /// /// /// The stream. /// /// public Task DeserializeAsync(Stream stream) { throw new NotImplementedException(); } } }