AopDictionary.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using Aop.Api.Parser;
  5. using Newtonsoft.Json;
  6. namespace Aop.Api
  7. {
  8. /// <summary>
  9. /// 符合AOP习惯的纯字符串字典结构。
  10. /// </summary>
  11. public class AopDictionary : Dictionary<string, string>
  12. {
  13. private const string DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
  14. public AopDictionary() { }
  15. public AopDictionary(IDictionary<string, string> dictionary)
  16. : base(dictionary)
  17. { }
  18. public void AddAll(Dictionary<string, string> textParams)
  19. {
  20. foreach (var param in textParams)
  21. {
  22. this.Add(param.Key, param.Value);
  23. }
  24. }
  25. /// <summary>
  26. /// 添加一个新的键值对。空键或者空值的键值对将会被忽略。
  27. /// </summary>
  28. /// <param name="key">键名称</param>
  29. /// <param name="value">键对应的值,目前支持:string, int, long, double, bool, DateTime类型</param>
  30. public void Add(string key, object value)
  31. {
  32. string strValue;
  33. if (value == null)
  34. {
  35. strValue = null;
  36. }
  37. else if (value is string)
  38. {
  39. strValue = (string)value;
  40. }
  41. else if (value is Nullable<DateTime>)
  42. {
  43. Nullable<DateTime> dateTime = value as Nullable<DateTime>;
  44. strValue = dateTime.Value.ToString(DATE_TIME_FORMAT);
  45. }
  46. else if (value is Nullable<int>)
  47. {
  48. strValue = (value as Nullable<int>).Value.ToString();
  49. }
  50. else if (value is Nullable<long>)
  51. {
  52. strValue = (value as Nullable<long>).Value.ToString();
  53. }
  54. else if (value is Nullable<double>)
  55. {
  56. strValue = (value as Nullable<double>).Value.ToString();
  57. }
  58. else if (value is Nullable<bool>)
  59. {
  60. strValue = (value as Nullable<bool>).Value.ToString().ToLower();
  61. }
  62. else if (value is ICollection)
  63. {
  64. Aop.Api2.Parser.AopModelParser parser = new Aop.Api2.Parser.AopModelParser();
  65. object jo = parser.serializeArrayValue(value as ICollection);
  66. JsonSerializerSettings jsetting = new JsonSerializerSettings();
  67. jsetting.NullValueHandling = NullValueHandling.Ignore;
  68. strValue = JsonConvert.SerializeObject(jo, Formatting.None, jsetting);
  69. }
  70. else if (value is AopObject)
  71. {
  72. AopModelParser parser = new AopModelParser();
  73. object jo = parser.serializeAopObject(value as AopObject);
  74. JsonSerializerSettings jsetting = new JsonSerializerSettings();
  75. jsetting.NullValueHandling = NullValueHandling.Ignore;
  76. strValue = JsonConvert.SerializeObject(jo, Formatting.None, jsetting);
  77. }
  78. else
  79. {
  80. strValue = value.ToString();
  81. }
  82. this.Add(key, strValue);
  83. }
  84. public new void Add(string key, string value)
  85. {
  86. if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
  87. {
  88. base.Add(key, value);
  89. }
  90. }
  91. }
  92. }