[Csharp] System.Text.Json 反序列化設定不區分屬性名稱大小寫

發表日期:
2021.10.20
/
分類:
Dev
在用了好一陣子的 Newtonsoft.Json 後,最近因為又開始開發 Web API,趁這機會來嘗試使用 .NET 內建的 System.Text.Json。 在官方文檔 如何從遷移 Newtonsoft.Json 至 System.Text.Json 中有

在用了好一陣子的 Newtonsoft.Json 後,最近因為又開始開發 Web API,趁這機會來嘗試使用 .NET 內建的 System.Text.Json。

在官方文檔 如何從遷移 Newtonsoft.Json 至 System.Text.Json 中有提到,System.Text.Json 用了更嚴格的語法以防止呼叫端對於功能存有無謂的猜測,最好的範例就是本篇要說明的最常用的反序列化設定「大小寫區分」。


如果使用的 API 都是自己設計通常不會出現名稱大小寫的問題。但我在開發 Identity Server 4 時,碰到 Token Endpoints 傳回的 Json 內的屬性名稱居然都是以小寫開頭,這樣的全小寫名稱會導致預設的 System.Text.Json 無法與正規的 C# 屬性配對,因為我們在設計屬性時都會以大寫為開頭。

public class IdentityResponse {
    public string Access_Token { get; set; }
    public int Expires_In { get; set; }
    public string Token_Type { get; set; }
    public string Scope { get; set; }
}

而 Identiy Server 4 給我的資料是這樣……

{
    "access_token": "eyJhb...qjj1w",
    "expires_in": 3600,
    "token_type": "Bearer",
    "scope": "player"
}

最後會導致預設的 System.Text.Json 會反序列化出一個 NULL 且擲回例外。

想要啟用將不區分大小寫功能的方法很簡單,只要在 JsonSerializer.Deserialize 的第二參數中建構一個 JsonSerializerOptions 並設定 PropertyNameCaseInsensitive 為 True 即可。

原本我們使用的的反序列語法如下:

JsonSerializer.Deserialize<GamePlayer>(
    jsonString
);

請修改成如此,即可正確反序列出正確的物件資料。

JsonSerializer.Deserialize<GamePlayer>(
    jsonString,
    new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
);

這陣子在學習使用 ASP.NET Core Web API 開發基於 HTTP 的弱連線遊戲伺服器,Json 的序列與反序列化必須熟練到用膝蓋都能實作……

comments powered by Disqus