Teams Bot 如何使用新的 System.Text.Json 库
我最近把 LuckyDraw的代碼升級(jí)到了 .net core 3.1,當(dāng)然我也很想使用最新的微軟json庫(kù),System.Text.Json這個(gè)庫(kù)的性能比之前Newtonsoft.Json速度更快,而且就我本人愛好來(lái)說(shuō),更加喜歡System.Text.Json的命名,之前一直覺得 JObject, JArray, JToken 這些名字不夠符合 c# 的 naming guideline。
微軟?這篇文章?很好的告訴大家如何將 Newtonsoft.Json 遷移到 System.Text.Json,但是如果你是用Bot SDK來(lái)開發(fā)teams bot,事情比你想象的復(fù)雜很多。
我們先來(lái)看一下bot sdk的sample code是怎么做的,打開EchoBot的代碼,找到Startup.cs文件,你可以看到這么一行:
public class Startup {...public void ConfigureServices(IServiceCollection services){services.AddControllers().AddNewtonsoftJson();...} }現(xiàn)在大家明白了把,bot samples雖然都已經(jīng)升級(jí)到了.net core 3.1,但是,它還是把mvc設(shè)置成使用Newtonsoft.Json。
那問(wèn)題到底在哪里,為什么一定要使用Newtonsoft? 我們來(lái)看一下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; }... }可以看到每個(gè)property都有一個(gè)JsonProperty的attribute,這個(gè)attribute是在Newtonsoft.Json里定義的,當(dāng)序列化的時(shí)候,會(huì)使用指定的name作為json里的屬性名字。當(dāng)然在新的System.Text.Json里也有一個(gè)對(duì)應(yīng)的attribute,叫JsonPropertyName,所以問(wèn)題就來(lái)了,如果我們使用新的System.Text.Json來(lái)對(duì)一個(gè)activity對(duì)象進(jìn)行serialize和deserialize,由于屬性 Type 上只有JsonProperty并沒有新的JsonPropertyName,serialize后,json就用了首字母大寫的{"Type":"blablabla"},如果是使用老的Newtonsoft.Json,那就是{"type":"blablabla"}。
當(dāng)然不單單是JsonProperty這么一個(gè)問(wèn)題,還有其他json序列化和反序列化的一些attribute也有類似問(wèn)題,比如下面這兩個(gè):
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){...}...} }正式由于上面這些問(wèn)題,所以如果要繼續(xù)擁護(hù)在mvc里使用新的System.Text.Json,同時(shí)又要使用bot sdk來(lái)做開發(fā),那就必須在和bot sdk里一些對(duì)象打交道的時(shí)候使用老的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);}...} }因?yàn)槟悴荒茉僖蕾囉趍vc來(lái)幫你deserialize出Activity對(duì)象,因?yàn)槲覀兊膍vc是使用新的System.Text.Json。
當(dāng)我們要返回一個(gè)activity對(duì)象的時(shí)候,以前可以這樣:
[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); }因?yàn)槲覀儾荒茉倏縨vc來(lái)幫你serialize一個(gè)Activity對(duì)象,必須手動(dòng)使用Newtonsoft.Json來(lái)序列化。
希望bot sdk和其他sdk,能夠盡快的兼容新的Json庫(kù),這樣才能使廣大開發(fā)者擁抱 System.Text.Json。
總結(jié)
以上是生活随笔為你收集整理的Teams Bot 如何使用新的 System.Text.Json 库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 练字在现代社会的意义还大不大,尤其是电脑
- 下一篇: 怎么练字才会有效果,多久才能有体现