Teams Bot 如何使用新的 System.Text.Json 库
我最近把 LuckyDraw的代碼升級到了 .net core 3.1,當然我也很想使用最新的微軟json庫,System.Text.Json這個庫的性能比之前Newtonsoft.Json速度更快,而且就我本人愛好來說,更加喜歡System.Text.Json的命名,之前一直覺得 JObject, JArray, JToken 這些名字不夠符合 c# 的 naming guideline。
微軟?這篇文章?很好的告訴大家如何將 Newtonsoft.Json 遷移到 System.Text.Json,但是如果你是用Bot SDK來開發(fā)teams bot,事情比你想象的復雜很多。
我們先來看一下bot sdk的sample code是怎么做的,打開EchoBot的代碼,找到Startup.cs文件,你可以看到這么一行:
public class Startup {...public void ConfigureServices(IServiceCollection services){services.AddControllers().AddNewtonsoftJson();...} }現(xiàn)在大家明白了把,bot samples雖然都已經(jīng)升級到了.net core 3.1,但是,它還是把mvc設置成使用Newtonsoft.Json。
那問題到底在哪里,為什么一定要使用Newtonsoft? 我們來看一下bot sdk源碼,看一下bot framework里最核心的Activity的代碼。
public partial class Activity {...[JsonProperty(PropertyName = "type")]public string Type { get; set; }[JsonProperty(PropertyName = "id")]public string Id { get; set; }[JsonProperty(PropertyName = "timestamp")]public System.DateTimeOffset? Timestamp { get; set; }[JsonProperty(PropertyName = "localTimestamp")]public System.DateTimeOffset? LocalTimestamp { get; set; }[JsonProperty(PropertyName = "localTimezone")]public string LocalTimezone { get; set; }[JsonProperty(PropertyName = "serviceUrl")]public string ServiceUrl { get; set; }[JsonProperty(PropertyName = "channelId")]public string ChannelId { get; set; }[JsonProperty(PropertyName = "from")]public ChannelAccount From { get; set; }... }可以看到每個property都有一個JsonProperty的attribute,這個attribute是在Newtonsoft.Json里定義的,當序列化的時候,會使用指定的name作為json里的屬性名字。當然在新的System.Text.Json里也有一個對應的attribute,叫JsonPropertyName,所以問題就來了,如果我們使用新的System.Text.Json來對一個activity對象進行serialize和deserialize,由于屬性 Type 上只有JsonProperty并沒有新的JsonPropertyName,serialize后,json就用了首字母大寫的{"Type":"blablabla"},如果是使用老的Newtonsoft.Json,那就是{"type":"blablabla"}。
當然不單單是JsonProperty這么一個問題,還有其他json序列化和反序列化的一些attribute也有類似問題,比如下面這兩個:
namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Templates {[JsonConverter(typeof(ActivityTemplateConverter))]public class ActivityTemplate : ITemplate<Activity>{...} } namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Actions {public class BeginDialog : BaseInvokeDialog{[JsonConstructor]public BeginDialog(...): base(dialogIdToCall, options){...}...} }正式由于上面這些問題,所以如果要繼續(xù)擁護在mvc里使用新的System.Text.Json,同時又要使用bot sdk來做開發(fā),那就必須在和bot sdk里一些對象打交道的時候使用老的Newtonsoft.Json。
比如以前可以這么寫:
public class MessagesController : ControllerBase {[HttpPost("messages")]public async Task<IActionResult> GetMessage([FromBody]Activity activity){...} }現(xiàn)在就要:
public class MessagesController : ControllerBase {[HttpPost("messages")]public async Task<IActionResult> GetMessage(){Activity activity;using (var streamReader = new StreamReader(Request.Body)){var bodyString = await streamReader.ReadToEndAsync();activity = JsonConvert.DeserializeObject<Activity>(bodyString);}...} }因為你不能再依賴于mvc來幫你deserialize出Activity對象,因為我們的mvc是使用新的System.Text.Json。
當我們要返回一個activity對象的時候,以前可以這樣:
[HttpPost("messages")] public async Task<IActionResult> GetMessage([FromBody]Activity activity) {Activity repliedActivity;...return Ok(repliedActivity); }現(xiàn)在就要:
[HttpPost("messages")] public async Task<IActionResult> GetMessage() {Activity repliedActivity;...return OkFromNewtonsoftJson(repliedActivity); }private IActionResult OkFromNewtonsoftJson(object value) {if (value == null){return NoContent();}var json = JsonConvert.SerializeObject(value);return Content(json, "application/json", Encoding.UTF8); }因為我們不能再靠mvc來幫你serialize一個Activity對象,必須手動使用Newtonsoft.Json來序列化。
希望bot sdk和其他sdk,能夠盡快的兼容新的Json庫,這樣才能使廣大開發(fā)者擁抱 System.Text.Json。
總結(jié)
以上是生活随笔為你收集整理的Teams Bot 如何使用新的 System.Text.Json 库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 练字在现代社会的意义还大不大,尤其是电脑
- 下一篇: 怎么练字才会有效果,多久才能有体现