FormatJsonExtension.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Web;
  7. using System.Web.Script.Serialization;
  8. using System.IO;
  9. using System.Globalization;
  10. using Newtonsoft.Json;
  11. namespace Ant.Service.Common
  12. {
  13. /// <summary>
  14. /// 格式化json扩展
  15. /// </summary>
  16. public static class FormatJsonExtension
  17. {
  18. /// 普通序列化(不进行UI友好的json化)
  19. /// </summary>
  20. /// <param name="c">控制器</param>
  21. /// <param name="data">数据</param>
  22. /// <returns></returns>
  23. public static FormatJsonResult JsonFormat(this Controller c, object data)
  24. {
  25. FormatJsonResult result = new FormatJsonResult();
  26. result.NotLigerUIFriendlySerialize = true;
  27. result.Data = data;
  28. return result;
  29. }
  30. /// <summary>
  31. /// UI友好的json格式序列化
  32. /// </summary>
  33. /// <param name="c"></param>
  34. /// <param name="data"></param>
  35. /// <param name="IsError"></param>
  36. /// <param name="message"></param>
  37. /// <returns></returns>
  38. public static FormatJsonResult JsonFormat(this Controller c, object data, bool IsError, string message)
  39. {
  40. FormatJsonResult result = new FormatJsonResult();
  41. result.Data = data;
  42. result.Message = message;
  43. result.IsError = IsError;
  44. return result;
  45. }
  46. /// <summary>
  47. /// 根据操作和提供的数据判断执行状态
  48. /// </summary>
  49. /// <param name="c">控制器</param>
  50. /// <param name="data">数据</param>
  51. /// <param name="op">操作类型(增删改查,等等)</param>
  52. /// <returns></returns>
  53. public static FormatJsonResult JsonFormat(this Controller c, object data,SysOperate op)
  54. {
  55. if (!data.IsNullOrEmpty())
  56. {
  57. return JsonFormatSuccess(c, data, op.ToMessage(true));
  58. }
  59. return JsonFormatError(c,op.ToMessage(false));
  60. }
  61. /// <summary>
  62. /// 根据操作和提供的数据判断执行状态
  63. /// </summary>
  64. /// <param name="c">控制器</param>
  65. /// <param name="data">数据</param>
  66. /// <param name="status">数据</param>
  67. /// <param name="op">操作类型(增删改查,等等)</param>
  68. /// <returns></returns>
  69. public static FormatJsonResult JsonFormat(this Controller c, object data,bool status, SysOperate op)
  70. {
  71. if (status)
  72. {
  73. return JsonFormatSuccess(c, data, op.ToMessage(true));
  74. }
  75. return JsonFormatError(c, op.ToMessage(false));
  76. }
  77. /// <summary>
  78. /// 成功的json返回
  79. /// </summary>
  80. /// <param name="c"></param>
  81. /// <param name="data"></param>
  82. /// <param name="message"></param>
  83. /// <returns></returns>
  84. public static FormatJsonResult JsonFormatSuccess(this Controller c, object data,string message)
  85. {
  86. return JsonFormat(c, data, false, message);
  87. }
  88. /// <summary>
  89. /// 失败的json返回
  90. /// </summary>
  91. /// <param name="c"></param>
  92. /// <param name="message"></param>
  93. /// <returns></returns>
  94. public static FormatJsonResult JsonFormatError(this Controller c, string message)
  95. {
  96. return JsonFormat(c, null, true, message);
  97. }
  98. }
  99. /// <summary>
  100. /// JsonResult格式化扩展
  101. /// </summary>
  102. public class FormatJsonResult : ActionResult
  103. {
  104. private bool iserror = false;
  105. /// <summary>
  106. /// 是否产生错误
  107. /// </summary>
  108. public bool IsError
  109. {
  110. get { return iserror; }
  111. set { this.iserror = value; }
  112. }
  113. /// <summary>
  114. /// 错误信息,或者成功信息
  115. /// </summary>
  116. public string Message { get; set; }
  117. /// <summary>
  118. /// 成功可能时返回的数据
  119. /// </summary>
  120. public Object Data { get; set; }
  121. /// <summary>
  122. /// 正常序列化方式(为True则不进行UI友好的序列化)
  123. /// </summary>
  124. public bool NotLigerUIFriendlySerialize { get; set; }
  125. public override void ExecuteResult(ControllerContext context)
  126. {
  127. if (context == null)
  128. {
  129. throw new ArgumentNullException("context");
  130. }
  131. HttpResponseBase response = context.HttpContext.Response;
  132. response.ContentType = "application/json";
  133. StringWriter sw = new StringWriter();
  134. JsonSerializer serializer = JsonSerializer.Create(
  135. new JsonSerializerSettings
  136. {
  137. // Converters = new JsonConverter[] { new Newtonsoft.Json.Converters.IsoDateTimeConverter() },
  138. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  139. NullValueHandling = NullValueHandling.Ignore
  140. }
  141. );
  142. using (JsonWriter jsonWriter = new JsonTextWriter(sw))
  143. {
  144. jsonWriter.Formatting = Formatting.Indented;
  145. if (!NotLigerUIFriendlySerialize)
  146. serializer.Serialize(jsonWriter, this);
  147. else
  148. serializer.Serialize(jsonWriter, Data);
  149. }
  150. response.Write(sw.ToString());
  151. }
  152. }
  153. }