UrlEncodedSerializer.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Text;
  5. using System.Web;
  6. namespace EasyDevCore.Remote.HttpAccess
  7. {
  8. /// <summary>
  9. /// ISerializer implementation that converts an object representing name/value pairs to a URL-encoded string.
  10. /// Default serializer used in calls to PostUrlEncodedAsync, etc.
  11. /// </summary>
  12. public class UrlEncodedSerializer : ISerializer
  13. {
  14. /// <summary>
  15. /// Serializes the specified object.
  16. /// </summary>
  17. /// <param name="obj">The object.</param>
  18. public string Serialize(object obj)
  19. {
  20. if (obj == null)
  21. return null;
  22. StringBuilder sb = new StringBuilder();
  23. PropertyInfo[] properties = obj.GetType().GetProperties();
  24. foreach (PropertyInfo p in properties)
  25. {
  26. object value = p.GetValue(obj, null);
  27. string stringValue = value == null ? string.Empty : value.ToString();
  28. string objectName = HttpUtility.UrlEncode(p.Name);
  29. if (stringValue.Length > 0)
  30. {
  31. stringValue = HttpUtility.UrlEncode(stringValue);
  32. objectName = HttpUtility.UrlEncode(objectName);
  33. if (sb.Length > 0)
  34. sb.Append("&");
  35. sb.Append(objectName).Append("=").Append(stringValue);
  36. }
  37. }
  38. return sb.ToString();
  39. }
  40. /// <summary>
  41. /// Serializes the specified object.
  42. /// </summary>
  43. /// <typeparam name="T"></typeparam>
  44. /// <param name="obj">The object.</param>
  45. /// <returns></returns>
  46. public string Serialize<T>(T obj)
  47. {
  48. if (obj == null)
  49. return null;
  50. return Serialize(obj);
  51. }
  52. /// <summary>
  53. /// Deserializes the specified s.
  54. /// </summary>
  55. /// <typeparam name="T"></typeparam>
  56. /// <param name="s">The s.</param>
  57. /// <exception cref="NotImplementedException">Deserializing to UrlEncoded not supported.</exception>
  58. public T Deserialize<T>(string s)
  59. {
  60. throw new NotImplementedException("Deserializing to UrlEncoded is not supported.");
  61. }
  62. /// <summary>
  63. /// Deserializes the specified stream.
  64. /// </summary>
  65. /// <typeparam name="T"></typeparam>
  66. /// <param name="stream">The stream.</param>
  67. /// <exception cref="NotImplementedException">Deserializing to UrlEncoded not supported.</exception>
  68. public T Deserialize<T>(Stream stream)
  69. {
  70. throw new NotImplementedException("Deserializing to UrlEncoded is not supported.");
  71. }
  72. /// <summary>
  73. /// Deserializes the asynchronous.
  74. /// </summary>
  75. /// <typeparam name="T"></typeparam>
  76. /// <param name="stream">The stream.</param>
  77. /// <returns></returns>
  78. /// <exception cref="System.NotImplementedException"></exception>
  79. public Task<T> DeserializeAsync<T>(Stream stream)
  80. {
  81. throw new NotImplementedException();
  82. }
  83. }
  84. }