在先後端分離的業務開發中,咱們老是須要返回各類各樣的數據包格式,一個良好的 json 格式數據包是咱們一向奉行的原則,下面就利用 Json.Net 來作一個簡單具備跨平臺的序列化數據包實現類。git
public partial class JsonReturn : ContentResult { public int Code { get; protected set; } public string Message { get; protected set; } public Hashtable Data { get; protected set; } = new Hashtable(); public bool Success { get { return this.Code == 0; } } public JsonReturn(int code, string message) { this.Code = code; this.SetMessage(message); } public JsonReturn SetMessage(string value) { this.Message = value; return this; } public JsonReturn SetData(params object[] value) { this.Data.Clear(); return this.AppendData(value); } public JsonReturn AppendData(params object[] value) { if (value?.Length < 2) return this; for (int a = 0; a < value.Length; a += 2) { if (value[a] == null) continue; this.Data[value[a]] = a + 1 < value.Length ? value[a + 1] : null; } return this; } private void ToJson(ActionContext context) { this.ContentType = "text/json;charset=utf-8;"; this.Content = JsonConvert.SerializeObject(this); } public override Task ExecuteResultAsync(ActionContext context) { ToJson(context); return base.ExecuteResultAsync(context); } public override void ExecuteResult(ActionContext context) { ToJson(context); base.ExecuteResult(context); } /// <summary> /// 成功 0 /// </summary> public static JsonReturn 成功 { get { return new JsonReturn(0, "成功"); } } /// <summary> /// 失敗 500 /// </summary> public static JsonReturn 失敗 { get { return new JsonReturn(500, "失敗"); } } }
在 JsonReturn 類中,定義了一個存儲業務數據對象的 Hashtable 對象,在接口中能夠往該對象中寫入須要序列化的數據,並重寫了 ContentResult 的 ExecuteResultAsync 和 ExecuteResult 方法,在方法內實現 JsonResult 對象的序列化,最後提供了兩個靜態屬性方便調用;在 JsonReutrn 類中,最重要的是定義了成功和失敗的 Code ,默認 0 =成功,500=失敗,這樣就約定了全部客戶端都強制使用該協議,完成了標準的統一。github
1.3 在控制器中將此對象返回json
[HttpGet] public ActionResult Get() { UserInfo info = new UserInfo() { Age = 22, Gender = true, Name = "Ron.lang", RegTime = DateTime.Now }; return JsonReturn.成功.SetData("detail", info); }
{ "Code": 0, "Message": "成功", "Data": { "detail": { "Name": "Ron.lang", "Gender": true, "Age": 22, "RegTime": "2018-12-02T16:27:17.3289028+08:00" } } }
2.1 上面的結果還能夠接受,只是有一點小瑕疵,好比 bool 類型和字段名稱大小寫的問題,以及時間格式,都不是太友好,對於跨平臺來講,會存在一些問題,下面咱們改造一下,使得輸出的字段名稱所有消息,bool 類型轉換爲數字 0/1,時間轉換爲 Unix 格式;首先建立 3 個自定義 json 序列化類後端
2.2 LowercaseContractResolver.cs 轉換字段名稱爲小寫,該類很是簡單,僅有一行核心代碼前後端分離
public class LowercaseContractResolver : DefaultContractResolver { protected override string ResolvePropertyName(string propertyName) { return propertyName.ToLower(); } }
public class BooleanConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType == typeof(bool) || objectType == typeof(Nullable<bool>); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.Value == null) return null; return Convert.ToBoolean(reader.Value); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) writer.WriteNull(); else { UInt32 val = Convert.ToUInt32(Convert.ToBoolean(value)); writer.WriteValue(val); } } }
public class DateTimeConverter : DateTimeConverterBase { public static DateTime Greenwich_Mean_Time = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local); public override bool CanConvert(Type objectType) { return objectType == typeof(DateTime) || objectType == typeof(Nullable<DateTime>); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.Value == null) return null; if (CanConvert(objectType)) { if (string.IsNullOrEmpty(reader.Value.ToNullOrString())) return reader.Value; if (reader.Value is string) { if (DateTime.TryParse(reader.Value.ToString(), out DateTime dt)) return dt; else return reader.Value; } else return new DateTime(Greenwich_Mean_Time.Ticks + Convert.ToInt64(reader.Value) * 10000).ToLocalTime(); } else return reader.Value; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) writer.WriteNull(); else { long val = 0; if (value.GetType() == typeof(DateTime)) { DateTime dt = Convert.ToDateTime(value); val = (dt.ToUniversalTime().Ticks - Greenwich_Mean_Time.Ticks) / 10000; } else val = Convert.ToInt64(value); writer.WriteValue(val); } } }
public Startup(IConfiguration configuration, IHostingEnvironment env) { JsonConvert.DefaultSettings = () => { var st = new JsonSerializerSettings { Formatting = Formatting.Indented }; st.Converters.Add(new BooleanConverter()); st.Converters.Add(new DateTimeConverter()); st.ContractResolver = new LowercaseContractResolver(); return st; }; }
{ "code": 0, "message": "成功", "data": { "detail": { "name": "Ron.lang", "gender": 1, "age": 22, "regtime": 1543739815980 } } }
經過繼承 ContentResult 實現自定義的序列化數據包,這是剛需;爲了實現跨平臺的要求,咱們還自定義 JsonSettings 實現各類類型的自定義轉換,在實際項目開發中,這是很是有用的。ide
https://github.com/lianggx/EasyAspNetCoreDemo/tree/master/Ron.JsonTestthis