HttpAccessSetup.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using static System.Net.WebRequestMethods;
  9. namespace EasyDevCore.Remote.HttpAccess
  10. {
  11. /// <summary>
  12. /// Contract for serializing and deserializing objects.
  13. /// </summary>
  14. public interface ISerializer
  15. {
  16. /// <summary>
  17. /// Serializes an object to a string representation.
  18. /// </summary>
  19. string Serialize(object obj);
  20. /// <summary>
  21. /// Serializes the specified object.
  22. /// </summary>
  23. /// <typeparam name="T"></typeparam>
  24. /// <param name="obj">The object.</param>
  25. /// <returns></returns>
  26. string Serialize<T>(T obj);
  27. /// <summary>
  28. /// Deserializes an object from a string representation.
  29. /// </summary>
  30. T Deserialize<T>(string s);
  31. /// <summary>
  32. /// Deserializes an object from a stream representation.
  33. /// </summary>
  34. T Deserialize<T>(Stream stream);
  35. /// <summary>
  36. /// Deserializes the asynchronous.
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  39. /// <param name="stream">The stream.</param>
  40. /// <returns></returns>
  41. Task<T> DeserializeAsync<T>(Stream stream);
  42. }
  43. /// <summary>
  44. ///
  45. /// </summary>
  46. public class HttpAccessSetup
  47. {
  48. private static ISerializer _JsonSerializerRequest;
  49. private static ISerializer _UrlEncodedSerializerRequest;
  50. private static ISerializer _JsonSerializerResponse;
  51. /// <summary>
  52. /// Gets or sets the json serializer request.
  53. /// </summary>
  54. /// <value>
  55. /// The json serializer request.
  56. /// </value>
  57. public static ISerializer JsonSerializerRequest { get => (_JsonSerializerRequest ?? (_JsonSerializerRequest = new TextJsonSerializer())); set => _JsonSerializerRequest = value; }
  58. /// <summary>
  59. /// Gets or sets the URL encoded serializer.
  60. /// </summary>
  61. /// <value>
  62. /// The URL encoded serializer.
  63. /// </value>
  64. public static ISerializer UrlEncodedSerializerRequest { get => (_UrlEncodedSerializerRequest ?? (_UrlEncodedSerializerRequest = new UrlEncodedSerializer())); set => _UrlEncodedSerializerRequest = value; }
  65. /// <summary>
  66. /// Gets or sets the json serializer response.
  67. /// </summary>
  68. /// <value>
  69. /// The json serializer response.
  70. /// </value>
  71. public static ISerializer JsonSerializerResponse { get => (_JsonSerializerResponse ?? (_JsonSerializerResponse = new TextJsonSerializer())); set => _JsonSerializerResponse = value; }
  72. }
  73. }