using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Web.Script.Serialization;
using System.IO;
using System.Globalization;
using Newtonsoft.Json;
namespace Ant.Service.Common
{
///
/// 格式化json扩展
///
public static class FormatJsonExtension
{
/// 普通序列化(不进行UI友好的json化)
///
/// 控制器
/// 数据
///
public static FormatJsonResult JsonFormat(this Controller c, object data)
{
FormatJsonResult result = new FormatJsonResult();
result.NotLigerUIFriendlySerialize = true;
result.Data = data;
return result;
}
///
/// UI友好的json格式序列化
///
///
///
///
///
///
public static FormatJsonResult JsonFormat(this Controller c, object data, bool IsError, string message)
{
FormatJsonResult result = new FormatJsonResult();
result.Data = data;
result.Message = message;
result.IsError = IsError;
return result;
}
///
/// 根据操作和提供的数据判断执行状态
///
/// 控制器
/// 数据
/// 操作类型(增删改查,等等)
///
public static FormatJsonResult JsonFormat(this Controller c, object data,SysOperate op)
{
if (!data.IsNullOrEmpty())
{
return JsonFormatSuccess(c, data, op.ToMessage(true));
}
return JsonFormatError(c,op.ToMessage(false));
}
///
/// 根据操作和提供的数据判断执行状态
///
/// 控制器
/// 数据
/// 数据
/// 操作类型(增删改查,等等)
///
public static FormatJsonResult JsonFormat(this Controller c, object data,bool status, SysOperate op)
{
if (status)
{
return JsonFormatSuccess(c, data, op.ToMessage(true));
}
return JsonFormatError(c, op.ToMessage(false));
}
///
/// 成功的json返回
///
///
///
///
///
public static FormatJsonResult JsonFormatSuccess(this Controller c, object data,string message)
{
return JsonFormat(c, data, false, message);
}
///
/// 失败的json返回
///
///
///
///
public static FormatJsonResult JsonFormatError(this Controller c, string message)
{
return JsonFormat(c, null, true, message);
}
}
///
/// JsonResult格式化扩展
///
public class FormatJsonResult : ActionResult
{
private bool iserror = false;
///
/// 是否产生错误
///
public bool IsError
{
get { return iserror; }
set { this.iserror = value; }
}
///
/// 错误信息,或者成功信息
///
public string Message { get; set; }
///
/// 成功可能时返回的数据
///
public Object Data { get; set; }
///
/// 正常序列化方式(为True则不进行UI友好的序列化)
///
public bool NotLigerUIFriendlySerialize { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "application/json";
StringWriter sw = new StringWriter();
JsonSerializer serializer = JsonSerializer.Create(
new JsonSerializerSettings
{
// Converters = new JsonConverter[] { new Newtonsoft.Json.Converters.IsoDateTimeConverter() },
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
}
);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
if (!NotLigerUIFriendlySerialize)
serializer.Serialize(jsonWriter, this);
else
serializer.Serialize(jsonWriter, Data);
}
response.Write(sw.ToString());
}
}
}